Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: @Bean can still work without @Configuration

Tags:

spring

From https://projects.spring.io/spring-framework/ I have a spring framework hellpworld program. I delete the annotation @Configuration. However the program could still run as before. Why? What is @Configuration role here?

like image 331
guo Avatar asked Oct 26 '16 07:10

guo


Video Answer


1 Answers

You can still mark the class with @Component for @Bean instances to be available to program. when you do that it is called lite mode. In this mode you can not use 'inter-bean references' , means referring other instance via methods.

On the the other hand @Bean with class @Configuration is wrapped in side cglib wrapper where any calls to this bean methods can be intercepted and bean instances can be return from context. Means you can use 'inter-bean references'.

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html

  1. @Bean Methods in @Configuration Classes

Typically, @Bean methods are declared within @Configuration classes. In this case, bean methods may reference other @Bean methods in the same class by calling them directly. This ensures that references between beans are strongly typed and navigable. Such so-called 'inter-bean references' are guaranteed to respect scoping and AOP semantics, just like getBean() lookups would. These are the semantics known from the original 'Spring JavaConfig' project which require CGLIB subclassing of each such configuration class at runtime. As a consequence, @Configuration classes and their factory methods must not be marked as final or private in this mode.

  • @Bean Lite Mode

@Bean methods may also be declared within classes that are not annotated with @Configuration. For example, bean methods may be declared in a @Component class or even in a plain old class. In such cases, a @Bean method will get processed in a so-called 'lite' mode.

Bean methods in lite mode will be treated as plain factory methods by the container (similar to factory-method declarations in XML), with scoping and lifecycle callbacks properly applied. The containing class remains unmodified in this case, and there are no unusual constraints for the containing class or the factory methods.

In contrast to the semantics for bean methods in @Configuration classes, 'inter-bean references' are not supported in lite mode. Instead, when one @Bean-method invokes another @Bean-method in lite mode, the invocation is a standard Java method invocation; Spring does not intercept the invocation via a CGLIB proxy.

And, @Configuration gives you the ability use many other features with conjunction of other annotations

Importing other config @Import(DatabaseConfig.class)

resource import @PropertySource("classpath:config.properties")

enable component scan @ComponentScan(basePackages = { "com.sample.*" })

marking profile @Profile("production")

To enable features @Enablexxxx

http://docs.spring.io/spring-framework/docs/4.0.4.RELEASE/javadoc-api/org/springframework/context/annotation/Configuration.html

like image 113
kuhajeyan Avatar answered Oct 21 '22 02:10

kuhajeyan