当前位置: 首页 > news >正文

筑巢网站建设云vps怎么搭建网站

筑巢网站建设,云vps怎么搭建网站,ps个人主页设计,wordpress栏目设置到导航一、单例模式(Singleton Pattern) 定义:确保一个类只有一个实例,并提供全局访问点 Spring中的应用:Spring默认将Bean配置为单例模式 案例: Component public class MySingletonBean {// Spring 默认将其…

一、单例模式(Singleton Pattern)

  • 定义:确保一个类只有一个实例,并提供全局访问点

  • Spring中的应用:Spring默认将Bean配置为单例模式

  • 案例:

@Component
public class MySingletonBean {// Spring 默认将其管理为单例
}
  • 在spring容器中,MySingletonBean只会有一个实例

二、工厂模式(Factory Pattern)

  • 定义:定义一个创建对象的接口,由子类决定实例化哪个类

  • spring中的应用:BeanFactory和ApplicationContext是工厂模式的实现

  • 案例:

@Configuration
public class AppConfig {@Beanpublic MyBean myBean() {return new MyBean();}
}
  • AppConfig是一个工厂类,myBean()方法负责创建MyBean实例

三、原型模式(Prototype Pattern)

  • 定义:通过复制现有对象来创建新对象

  • Spring中的应用:通过@Scope("prototype")配置Bean为原型模式

  • 案例

@Component
@Scope("prototype")
public class MyPrototypeBean {// 每次获取时都会创建一个新实例
}

四、模板方法模式(Template Method Pattern)

  • 定义:定义一个算法的骨架,将某些步骤延迟到子类中实现

  • Spring中的应用:JdbcTemplate、RestTemplate等

  • 案例:

@Autowired
private JdbcTemplate jdbcTemplate;
​
public void queryData() {String sql = "SELECT * FROM users";jdbcTemplate.query(sql, (rs, rowNum) -> {System.out.println(rs.getString("username"));return null;});
}

五、适配器模式(Adapter Pattern)

  • 定义:将一个类的接口转换成客户端期望的另一个接口

  • spring中的应用:Spring MVC中的HandlerAdapter

  • 案例:

@Controller
public class MyController {@RequestMapping("/hello")public String hello() {return "Hello, World!";}
}

六、装饰者模式(Decorator Pattern)

  • 定义:动态地为对象添加额外的职责

  • spring中的应用:spring AOP中的代理

  • 案例:

@Aspect
@Component
public class LoggingAspect {@Before("execution(* com.example.service.*.*(..))")public void logBefore(JoinPoint joinPoint) {System.out.println("Before method: " + joinPoint.getSignature().getName());}
}
  • AOP通过装饰者模式为方法添加日志功能

七、观察者模式(Observer Pattern)

  • 定义:定义对象间的一对多依赖关系,当一个对象状态改变时,所有依赖对象都会收到通知

  • spring容器中的应用:Spring的事件机制

  • 案例:

@Component
public class MyEventListener implements ApplicationListener<MyEvent> {@Overridepublic void onApplicationEvent(MyEvent event) {System.out.println("Event received: " + event.getMessage());}
}
​
@Component
public class MyEventPublisher {@Autowiredprivate ApplicationEventPublisher publisher;
​public void publishEvent(String message) {publisher.publishEvent(new MyEvent(this, message));}
}
  • MyEventPublisher发布事件,MyEventListener监听并处理

八、代理模式(Proxy Pattern)

  • 定义:为其他对象提供一个代理以控制对这个对象的访问

  • Spring中的应用:SpringAOP和动态代理

  • 案例:

@Service
public class MyService {public void doSomething() {System.out.println("Doing something...");}
}
​
@Aspect
@Component
public class MyAspect {@Around("execution(* com.example.service.MyService.*(..))")public Object around(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("Before method");Object result = joinPoint.proceed();System.out.println("After method");return result;}
}
  • AOP通过代理模式为MyService的方法添加额外逻辑

九、组合模式(Composite Pattern)

  • 定义:将对象组合成树形结构以表示"部分-整体"的层次结构

  • spring中的应用:Spring Security的过滤器链

  • 案例:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/public/**").permitAll().antMatchers("/private/**").authenticated();}
}
  • Spring Security将多个过滤器组合成一个过滤器链

十、策略模式(Strategy Pattern)

  • 定义:定义一系列算法,将它们封装起来,并使它们可以互相替换

  • Spring中的应用:Spring的资源加载策略(ResourceLoader)

  • 案例:

@Autowired
private ResourceLoader resourceLoader;
​
public void loadResource() {Resource resource = resourceLoader.getResource("classpath:data.txt");System.out.println("Resource loaded: " + resource.exists());
}
  • ResourceLoader根据不同的资源加载策略加载资源(如文件系统、类路径等)。

http://www.yayakq.cn/news/429909/

相关文章:

  • 怎么做百度推广网站做网站有用吗
  • 网站制作目的专门做建筑设计图库的网站设计
  • 电商网站开发计划书网站设计深圳联系电话?
  • 邯郸网站优化90设计app
  • 网站建设客户去哪找广西人才网
  • 中国建设银行网站慢网站地图在线生成器
  • 整页图片广告网站源码网站建设与管理专业人才调研
  • 深圳的网站wordpress映射到外网
  • 巴市建网站做视频比较好的理财网站
  • 开网络公司做网站挣钱么做外贸一般做什么
  • 什么网站是做汽车装饰配件的主要的cms系统有哪些
  • 沧州开发网站多少钱做网站的虚拟机怎么用
  • 厦门集美网站建设国内手机搜索引擎十大排行
  • 移动互联网网站建设建设企业网站需要什么呢
  • 谈谈对网站建设的认识网页剪辑app
  • seo网站分析网络优化公司
  • ftp怎么连接网站空间网站建设行业衰落
  • 建什么类型个人网站比较好wordpress 播放音乐
  • 做斗图网站上海发布官网首页
  • 怎么做网站维护英语培训机构前十名
  • 做网站拿来卖黄村网站开发公司
  • 门户网站做公众号的好处嘉兴网站建设费用
  • 成都红酒网站建设吉林省建设工程质量监督站网站
  • 合肥做双语外贸网站四川省建设厅注册中心网站
  • 控制网站的大量访问国内产女装一线二线品牌知乎
  • 哈尔滨网站建设科技公司做网站一年大概的盈利
  • 湛江建站公司模板做企业官网的流程
  • 企业网站公众号新乡网站建设服务
  • 深圳做网站需要多少钱影视会员代理平台网站
  • 网站页面关键词优化开网站建设公司怎么样