There is a @Bean
annotation in Spring 3.0. It allows to define a Spring bean directly in a Java code. While browsing Spring reference I found two different ways of using this annotation - inside class annotated with @Configuration
and inside class which doesn't have this annotation.
This section contains following piece of code:
@Component
public class FactoryMethodComponent {
@Bean @Qualifier("public")
public TestBean publicInstance() {
return new TestBean("publicInstance");
}
// omitted irrelevant method
}
And here we could see a very similar piece of code, but now @Configuration
is in the place:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
Former section of reference contains following explaination:
The @Bean methods in a 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 @Configuration classes @Bean methods create bean metadata references to collaborating objects. Methods are not invoked with normal Java semantics. In contrast, calling a method or field within a @Component classes @Bean method has standard Java semantics.
But CGLIB is a kind of internal stuff which application developer shouldn't be aware of (in a ideal world, of course). As I understand in both cases Spring invokes method annotated with @Bean
to cr开发者_JAVA技巧eate Spring bean, in both cases these instances are injected to collaborators.
So my question is what is the difference for me as an application developer between this two cases?
The difference is that with @Configuration
you can call one @Bean
method from another and get a fully initialized instance, as follows:
public class Foo {
@Value("Hello, world!")
public String value;
}
@Configuration
public class Config {
@Bean
public Foo createFoo() {
Foo foo = new Foo();
System.out.println(foo.value); // Prints null - foo not initialized yet
return foo;
}
@Bean
public Bar createBar() {
Foo foo = createFoo();
System.out.println(foo.value); // Prints Hello, world! - foo have been initialized by the interceptor
return new Bar(foo);
}
}
@Bean [instance method] inside @Component - One method with @Bean instance call other method @Bean instance , then it would be simple java semantics call i.e. Object won't returned by Spring container , It would be normal return from java instance factory method,because Component class don't extends CGLIB.
@Bean [instance method] inside @Configuration - In this case , spring container would be returning reference to exisiting object. It won't be normal java sematic call.
@Bean on static method inside Configuration & Component Class - In this case , @Bean method would never be intercepted by the container neither in Configuration class nor in Component Sterotype class.
精彩评论