你真的懂Spring Java Config 吗?Full @Configuration vs lite @Bean mode
full @configuration
和lite @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 @configuration
和 lite @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的方法是不能被声明为private
和final
,因为cglib是通过生成子类来实现代理的,private
和final
方法是不能被子类override的,也就是说,full @configuration模式下,@bean
的方法是不能不能被声明为private
和final
,不然在启动时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
。
欢迎关注个人公众号:
上一篇: 服装面料加工中英双语网站搭建模板
下一篇: 你见过的最全面的 Python 重点