首页 > 其他分享 >Spring Boot Full @Configuration vs ‘lite‘ @Bean mode?

Spring Boot Full @Configuration vs ‘lite‘ @Bean mode?

时间:2022-10-30 20:10:25浏览次数:60  
标签:Full methods Spring Boot class Bean mode lite method


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 will be considered ‘lite’, with a different primary purpose of the containing class and an @Bean method just 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 simple 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 an @Bean method should therefore not invoke other @Bean methods; each such method is literally just 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 (i.e. the containing class may nevertheless be final etc).

In common scenarios, @Bean methods are to be declared within @Configuration classes, ensuring that ‘full’ mode is always used and that cross-method references will therefore get redirected to the container’s lifecycle management. This will prevent 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.


标签:Full,methods,Spring,Boot,class,Bean,mode,lite,method
From: https://blog.51cto.com/xichenguan/5807761

相关文章