- @Bean表示一个用于配置和初始化一个由SpringIoc容器管理的新对象的方法,类似于XML配置文件的
<bean/>
- 可以在Spring的@Component注解的类中使用@Bean注解任何方法(仅仅是可以)
- 上一点中,通常使用的是@Configuration
Bean
@configuration
public class AppConfig {
@Bean(name = "myFoo")
public Foo foo() {
return new Foo();
}
}
- init-method
- destroy-method
public class Foo{
public void init() {
}
}
public class Bar {
public void cleanup() {
}
}
@Configuration
public class AppConfig {
@Bean(initMethod = "init")
public Foo foo() {
return new Foo();
}
@Bean(destroyMethod = "cleanup")
public Bar bar() {
return new Bar();
}
}