@Configuration and @bean annotations
These two annotations are usually used together.
The annotation class with @Configuration indicates that this class can use the Spring IoC container as the source for the bean definition. The @Bean annotation tells Spring that an annotation method with an @bean will return an object that should be registered as a Bean in the Spring application context.
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(a){
return newHelloWorld(); }}Copy the code
The above code is equivalent to the configuration in beans.xml:
<beans>
<bean id="helloWorld" class="com.sap.HelloWorld" />
</beans>
Copy the code
This after @ Configuration modification of the role of the class, equivalent to a ClassPathXmlApplicationContext.
Here is the consumption code:
ApplicationContext ctx =
new AnnotationConfigApplicationContext(HelloWorldConfig.class);
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.setMessage("Hello World!");
helloWorld.getMessage();
Copy the code
This HelloWorldConfig is dynamically enhanced by SpringCGLib:
For more of Jerry’s original articles, please follow the public account “Wang Zixi “: