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

网站建设与开发做什么网上销售推广方案

网站建设与开发做什么,网上销售推广方案,b2b黄页大全,北京住房和城乡建设网官网文章目录 前言一、Component与Configuration区别二、Configuration的解析三、生成CGLIB动态代理四、目标方法的调用4.1、BeanMethodInterceptor 总结 前言 Configuration是Spring提供的注解,其作用是将该类标识为一个Java 配置类,用来替代传统的 XML 配置…

文章目录

  • 前言
  • 一、@Component与@Configuration区别
  • 二、@Configuration的解析
  • 三、生成CGLIB动态代理
  • 四、目标方法的调用
    • 4.1、BeanMethodInterceptor
  • 总结


前言

  @Configuration是Spring提供的注解,其作用是将该类标识为一个Java 配置类,用来替代传统的 XML 配置文件:

@Configuration
public class AppConfig {@Beanpublic MyService myService() {return new MyService();}
}

  上述代码等价于传统 XML 中:

<bean id="myService" class="com.example.MyService"/>

  与标注了@Component注解的配置类相比,区别在于Spring 在解析@Configuration 类时,会使用 CGLIB 生成子类代理,重写其中所有@Bean方法。


一、@Component与@Configuration区别

  @Configuration案例工程:

public class MainApp {public static void main(String[] args) {// 创建应用上下文AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);AppConfig appConfig = context.getBean(AppConfig.class);System.out.println("------ 执行 AppConfig 中的 testBeanMethodCall ------");appConfig.testBeanMethodCall(); // 使用的是代理对象,返回同一个 Beancontext.close();}
}class HelloService {}@Configuration
class AppConfig {@Beanpublic HelloService helloService() {return new HelloService();}public void testBeanMethodCall() {HelloService s1 = helloService();HelloService s2 = helloService();System.out.println("AppConfig -> helloService() s1 == s2 ? " + (s1 == s2));}
}

  运行结果:

------ 执行 AppConfig 中的 testBeanMethodCall ------
AppConfig -> helloService() s1 == s2 ? true

  @Component案例工程:

public class MainApp {public static void main(String[] args) {// 创建应用上下文AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);AppConfig appConfig = context.getBean(AppConfig.class);System.out.println("------ 执行 AppConfig 中的 testBeanMethodCall ------");appConfig.testBeanMethodCall(); // 使用的是代理对象,返回同一个 Beancontext.close();}
}class HelloService {}@Component
class AppConfig {@Beanpublic HelloService helloService() {return new HelloService();}public void testBeanMethodCall() {HelloService s1 = helloService();HelloService s2 = helloService();System.out.println("AppConfig -> helloService() s1 == s2 ? " + (s1 == s2));}
}

  运行结果:

------ 执行 AppConfig 中的 testBeanMethodCall ------
AppConfig -> helloService() s1 == s2 ? false


  通过最终的运行结果可以看出,被@Configuration修饰的配置类中,获取多次被@Bean注解标注的类,**获取到的都是相同的实例。**而被@Component修饰的配置类中,获取多次被@Bean注解标注的类,每次获取到的都是新的实例。

二、@Configuration的解析

  @Configuration的解析,同样在ConfigurationClassPostProcessor后置处理器中:
在这里插入图片描述
  如果当前配置类上,有@Configuration注解,那么会根据proxyBeanMethods的值,走不同的分支,proxyBeanMethods@Configuration注解的属性,默认为true:
在这里插入图片描述
  在配置类上加入了@Configuration注解,并且没有显式地标注proxyBeanMethods为false,则会将配置类的bean定义中的attributes的value设置为full,表示当前的配置类是一个完整的配置类:
在这里插入图片描述
在这里插入图片描述
  上述的过程是@Configuration的解析阶段

三、生成CGLIB动态代理

  ConfigurationClassPostProcessor后置处理器,实现了BeanDefinitionRegistryPostProcessor,而BeanDefinitionRegistryPostProcessor又继承了普通的bean工厂后处理器BeanFactoryPostProcessor
在这里插入图片描述
  在invokeBeanFactoryPostProcessorsinvokeBeanFactoryPostProcessors方法中,会调用ConfigurationClassPostProcessor重写的父类BeanFactoryPostProcessorpostProcessBeanFactory方法:
在这里插入图片描述

  收集所有bean定义的attributes的value为full的bean成map。key是bean的名称,value是对应的bean定义,在上面的案例工程中对应的就是AppConfig类。

att
  然后进行遍历,生成CGLIB动态代理
在这里插入图片描述
在这里插入图片描述
  这里设置的拦截器链,是定义在ConfigurationClassEnhancer中的一个属性:
在这里插入图片描述

private static final Callback[] CALLBACKS = new Callback[] {new BeanMethodInterceptor(),            // 拦截 @Bean 方法调用,核心代理逻辑new BeanFactoryAwareMethodInterceptor(),// 拦截实现 BeanFactoryAware 的方法NoOp.INSTANCE                          // 无操作回调,默认行为
};

  最后将AppConfig的类型设置为代理类型。

在这里插入图片描述

四、目标方法的调用

  在调用案例工程的testBeanMethodCall时,会走过BeanMethodInterceptor拦截器的intercept方法。
在这里插入图片描述

4.1、BeanMethodInterceptor

  如果容器是第一次调用@Bean的目标方法,就真正执行 @Bean 方法体:
在这里插入图片描述
  否则,直接从容器中获取已有的 bean:
在这里插入图片描述
在这里插入图片描述
关键代码,保证每次从容器中拿到的都是同一个单例实例

总结

特性@Configuration@Component
是否会被 Spring 扫描注册为 Bean
是否支持 @Bean 方法注册 Bean支持(推荐)支持(不推荐)
@Bean 方法是否被代理增强会(返回容器单例对象)不会(每次 new 新对象)
是否能作为配置中心使用推荐用于声明多个 @Bean不推荐作为配置类使用
http://www.dtcms.com/wzjs/434046.html

相关文章:

  • 自适应网站ui做几套班级优化大师app
  • 深圳开发小程序seo链接优化建议
  • 台州企业网站搭建价格南京seo外包
  • 松原网站制作网络策划
  • 北京手机网站建设外包注册网站查询
  • 传奇官方网站北京网站营销与推广
  • 网站建设技术网站发稿软文公司
  • 入侵织梦网站竞价托管推广多少钱
  • 网站怎么做详情页nba排名榜
  • 网站名称如何设置谷歌seo怎么优化
  • 用html网站建设过程百度大数据搜索引擎
  • 做网站的属于什么专业?网络营销推广方案设计
  • 网站报价书南山网站seo
  • 做蔬菜的网站有哪些页面关键词优化
  • 什么网站做任务的q币网站排名优化怎么做
  • 网站源代码下载工具网络营销计划书怎么写
  • 怎么做定位钓鱼网站百度竞价怎么做开户需要多少钱
  • 做3d兼职网站网络营销品牌
  • 上海市卫生健康委员会宁波seo关键词排名
  • 雪白丰腴做美妇网站百度免费发布信息网站
  • 五月天做网站百度站长工具使用方法
  • 建站工具免费站长seo查询
  • 淄博网站建设企业企业qq怎么申请
  • 有源码个人网站怎么建立百度快速优化推广
  • 网站策划案例河南网站顾问
  • 装饰公司营销网站建设百度联盟
  • hbuilder可以做网站嘛广东seo教程
  • 做网站平台公司有哪些cpu游戏优化加速软件
  • 怎样在谷歌做网站模板自助建站
  • 镇江市网站建设学电脑培训班