Full @Configurationlite @Bean mode 是 Spring Java Config 中两个非常有意思的概念。

先来看一下官方文档关于这两者的相关内容:

The @Bean methods in a regular Spring component are processed differently than their counterparts inside a Spring @Configuration class. The difference is that @Component classes are not enhanced with CGLIB to intercept the invocation of methods and fields. CGLIB proxying is the means by which invoking methods or fields within @Bean methods in @Configuration classes creates bean metadata references to collaborating objects. Such methods are not invoked with normal Java semantics but rather go through the container in order to provide the usual lifecycle management and proxying of Spring beans, even when referring to other beans through programmatic calls to @Bean methods. In contrast, invoking a method or field in a @Bean method within a plain @Component class has standard Java semantics, with no special CGLIB processing or other constraints applying.

机器翻译结果:常规Spring组件中的@Bean方法与Spring @Configuration类中的对应方法处理方式不同。不同之处在于,@Component类没有使用CGLIB增强来拦截方法和字段的调用。CGLIB代理是通过调用@Configuration类中的@Bean方法中的方法或字段来创建对协作对象的bean元数据引用的方法。这些方法不是用普通的Java语义调用的,而是通过容器来提供Spring bean通常的生命周期管理和代理,即使在通过对@Bean方法的编程调用引用其他bean时也是如此。相反,在普通@Component类中调用@Bean方法中的方法或字段具有标准的Java语义,不需要使用特殊的CGLIB处理或其他约束。

When @Bean methods are declared within classes that are not annotated with @Configuration, they are referred to as being processed in a “lite” mode. Bean methods declared in a @Component or even in a plain old class are considered to be “lite”, with a different primary purpose of the containing class and a @Bean method being a sort of bonus there. For example, service components may expose management views to the container through an additional @Bean method on each applicable component class. In such scenarios, @Bean methods are a general-purpose factory method mechanism. Unlike full @Configuration, lite @Bean methods cannot declare inter-bean dependencies. Instead, they operate on their containing component’s internal state and, optionally, on arguments that they may declare. Such a @Bean method should therefore not invoke other @Bean methods. Each such method is literally only a factory method for a particular bean reference, without any special runtime semantics. The positive side-effect here is that no CGLIB subclassing has to be applied at runtime, so there are no limitations in terms of class design (that is, the containing class may be final and so forth). In common scenarios, @Bean methods are to be declared within @Configuration classes, ensuring that “full” mode is always used and that cross-method references therefore get redirected to the container’s lifecycle management. This prevents the same @Bean method from accidentally being invoked through a regular Java call, which helps to reduce subtle bugs that can be hard to track down when operating in “lite” mode.

机器翻译:当@Bean方法在没有使用@Configuration注释的类中声明时,它们被称为在“lite”模式下处理。在@Component中声明的Bean方法,甚至在普通的旧类中声明的Bean方法,都被认为是“lite”,包含类的主要目的不同,而@Bean方法在这里是一种附加功能。例如,服务组件可以通过每个适用组件类上的附加@Bean方法向容器公开管理视图。在这种情况下,@Bean方法是一种通用的工厂方法机制。与完整的@Configuration不同,lite @Bean方法不能声明bean之间的依赖关系。相反,它们对包含它们的组件的内部状态进行操作,并可选地对它们可能声明的参数进行操作。因此,这样的@Bean方法不应该调用其他@Bean方法。每个这样的方法实际上只是一个特定bean引用的工厂方法,没有任何特殊的运行时语义。这里的积极副作用是在运行时不需要应用CGLIB子类,所以在类设计方面没有限制(也就是说,包含的类可能是final类,等等)。在常见的场景中,@Bean方法将在@Configuration类中声明,以确保始终使用“full”模式,并因此将交叉方法引用重定向到容器的生命周期管理。这可以防止通过常规Java调用意外调用相同的@Bean方法,这有助于减少在“lite”模式下操作时难以跟踪的细微bug。


相关定义

  • lite @Bean mode :当@Bean方法在没有使用@Configuration注解的类中声明时称之为lite @Bean mode
  • Full @Configuration:如果@Bean方法在使用@Configuration注解的类中声明时称之为Full @Configuration

有何区别

总结一句话,Full @Configuration中的@Bean方法会被CGLIB所代理,而 lite @Bean mode中的@Bean方法不会被CGLIB代理。

举个例子

有一普通Java类,现在分别使用Full @Configurationlite @Bean mode这两种模式注册到Spring容器中。

public class User {
public User() {
System.out.println("user create... hashCode :" + this.hashCode());
}
}

Full @Configuration

配置类:

@Configuration
public class AppConfig { @Bean
public User user() {
return new User();
} @Bean
public String name(User user) {
System.out.println(user.hashCode());
System.out.println(user().hashCode());
return "123";
}
}

主程序:

public class FullMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
User user = context.getBean(User.class);
System.out.println(user.hashCode());
context.close();
}
}

运行结果:

user create... hashCode :1709537756
1709537756
1709537756
1709537756

分析:@Bean是在使用了@Configuration注解的类上被声明的,属于Full @Configuration@Configuration类中的@Bean地方会被CGLIB进行代理。Spring会拦截该方法的执行,在默认单例情况下,容器中只有一个Bean,所以我们多次调用user()方法,获取的都是同一个对象。

注意:被CGLIB的方法是不能被声明为privatefinal,因为CGLIB是通过生成子类来实现代理的,privatefinal方法是不能被子类Override的,也就是说,Full @Configuration模式下,@Bean的方法是不能不能被声明为privatefinal,不然在启动时Spring会直接报错。

lite @Bean mode

配置类:

@Component
public class LiteAppConfig { @Bean
public User user() {
System.out.println("user() 方法执行");
return new User();
} @Bean
public String name(User user) {
System.out.println("name(User user) 方法执行");
System.out.println(user.hashCode());
System.out.println("再次调用user()方法: " + user().hashCode());
return "123";
}
}

主程序:

public class LiteMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(LiteAppConfig.class);
User user = context.getBean(User.class);
System.out.println(user.hashCode());
context.close();
}
}

运行结果:

user() 方法执行
user create... hashCode :254413710
name(User user) 方法执行
254413710
user() 方法执行
user create... hashCode :1793329556
再次调用user()方法: 1793329556
254413710

分析:在lite @Bean mode模式下, @Bean方法不会被CGLIB代理,所以多次调用user()会生成多个user对象。

使用建议

为了防止出现一些奇怪的问题,建议都使用Full @Configuration


欢迎关注个人公众号:

你真的懂Spring Java Config 吗?Full @Configuration vs lite @Bean mode的更多相关文章

  1. spring java config 初探

    Java Config 注解 spring java config作为同xml配置形式的另一种表达形式,使用的场景越来越多,在新版本的spring boot中 大量使用,今天我们来看下用到的主要注解有 ...

  2. Spring面试底层原理的那些问题,你是不是真的懂Spring?

    1.什么是 Spring 框架?Spring 框架有哪些主要模块?Spring 框架是一个为 Java 应用程序的开发提供了综合.广泛的基础性支持的 Java 平台.Spring帮助开发者解决了开发中 ...

  3. spring java config配置搭建工程资料收集(网文)

    https://blog.csdn.net/poorcoder_/article/details/70231779 https://github.com/lovelyCoder/springsecur ...

  4. 终于搞懂Spring中Scope为Request和Session的Bean了

    之前只是很模糊的知道其意思,在request scope中,每个request创建一个新的bean,在session scope中,同一session中的bean都是一样的 但是不知道怎么用代码去验证 ...

  5. Spring boot添加配置类@Configuration并初始化@Bean,@Resource和@Autowired都为null

    大写加黑,找了好久@Resource和@Autowired都依赖不到创建的bean的原因:@Bean的方法名即是创建的Bean名称 import org.activiti.engine.Process ...

  6. Spring注解@Configuration和Java Config

    1.从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中.但是,仍然允许使用经典的XML方式来定义bea ...

  7. 真懂Spring的@Configuration配置类?你可能自我感觉太良好

    当大潮退去,才知道谁在裸泳.关注公众号[BAT的乌托邦]开启专栏式学习,拒绝浅尝辄止.本文 https://www.yourbatman.cn 已收录,里面一并有Spring技术栈.MyBatis.中 ...

  8. Spring 4 and MyBatis Java Config

    TL;DR With the Java Config enhancements in Spring 4, you no longer need xml to configure MyBatis for ...

  9. Spring Security4实例(Java config版)——ajax登录,自定义验证

    本文源码请看这里 相关文章: Spring Security4实例(Java config 版) -- Remember-Me 首先添加起步依赖(如果不是springboot项目,自行切换为Sprin ...

随机推荐

  1. 蚂蚁SOFA系列(1) - 聊聊SOFA的模块化

    作者:404,转载请注明出处.欢迎关注公众号:404P. SOFA是蚂蚁自研的一套金融级分布式中间件,目前正在逐步向业界开源.SOFA的全称有两个,最早是Service Oriented Fabric ...

  2. eql框架。

    在刚进入公司的时候,在service层的框架用的是eql,是公司内的大佬封装的,作为一个小白,真的是折磨.公司内没有任何的文档,只能靠着自己一步一步的摸索. 后来用习惯了,发现这个框架确实有自己的独到 ...

  3. 【Spring】容器刷新(refresh)流程解析

    一.概述 二.prepareRefresh() 三.obtainFreshBeanFactory() 四.prepareBeanFactory(beanFactory); 五.postProcessB ...

  4. Java并发包下的几个API

    并发包 (计数器)CountDownLatch (屏障)CyclicBarrier (计数信号量)Semaphore 案例: 需求: 代码: 并发包 (计数器)CountDownLatch ​Coun ...

  5. yolo进化史之yolov2

    yolov1和当时最好的目标检测系统相比,有很多缺点.比如和Fast R-CNN相比,定位错误更多.和基于区域选择的目标检测方法相比,recall也比较低.yolov2的目标即在保证分类准确度的情况下 ...

  6. vue2.0 创建项目

    准备 安装淘宝 npm镜像 npm install -g cnpm --registry=https://registry.npm.taobao.org 添加系统变量path的内容 因为cnpm会被安 ...

  7. Winform中使用FastReport实现简单的自定义PDF导出

    场景 FastReport安装包下载.安装.去除使用限制以及工具箱中添加控件: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  8. 摩托罗拉GP68对讲机官方说明书下载,包含使用说明压音表和电路结构等

    摩托罗拉GP68对讲机官方说明书,包含使用说明和电路结构等 1.使用说明书 链接: https://pan.baidu.com/s/1fhXXaBp-MSuQs9Sv3v_Crg 提取码: mc3e ...

  9. 为什么使用B+Tree索引?

    什么是索引? 索引是一种数据结构,具体表现在查找算法上. 索引目的 提高查询效率 [类比字典和借书] 如果要查"mysql"这个单词,我们肯定需要定位到m字母,然后从下往下找到y字 ...

  10. SpringBoot起飞系列-入门(一)

    一.SpringBoot简介 1.1 什么是SpringBoot 说到spring系列,可能大家都很熟悉,spring.springmvc,美之名曰:spring全家桶,那么springboot其实也 ...