@Bean
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {
@AliasFor("name")
String[] value() default {};
@AliasFor("value")
String[] name() default {};
Autowire autowire() default Autowire.NO;
String initMethod() default "";
String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;
}
1.@Bean的元注解
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}):注解作用在方法上或者注解上
@Retention(RetentionPolicy.RUNTIME):保留策略——>运行时保留
@Documented:表明这个注解应该被 javadoc工具记录. 默认情况下,javadoc是不包括注解的.
2.@Bean的属性
value() , name():互为别名,为bean起一个名字,可以通过名字从spring容器中拿到bean对象。如果不设置,默认为@Bean修饰的方法名。
autowire():
public enum Autowire {
NO(AutowireCapableBeanFactory.AUTOWIRE_NO),//不设置装配方式
BY_NAME(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME),//按照名称装配
BY_TYPE(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE);//按照类型装配
指定 bean 的装配方式, 根据名称和类型装配,一般不设置,采用默认即可。
initMethod():指定对象初始化时调用的方法
destroyMethod():指定对象销毁时调用的方法
代码演示
实体类
package org.example.entity;
public class Order {
public Order() {
System.out.println("construct invoke");
}
public void init(){
System.out.println("order init");
}
public void destory(){
System.out.println("order destory");
}
}
配置类
package org.example.config;
import org.example.entity.Order;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringAnnoConfig {
@Bean(name = "orderId",initMethod = "init", destroyMethod = "destory")
public Order order(){
return new Order();
}
}
测试类
@Test
public void testBean(){
//初始化容器
System.out.println("初始化容器");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringAnnoConfig.class);
//关闭容器
System.out.println("关闭容器");
context.close();
}
测试结果
image.png
通过测试结果可以看出,initMethod在对象构造方法执行时候就会调用,destroyMethod 在容器摧毁的时候会调用。
现在我们再看看当前容器里bean对应的名称是什么
配置类
package org.example.config;
import org.example.entity.Order;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringAnnoConfig {
@Bean(name = "orderId")
public Order order(){
return new Order();
}
}
测试类
@Test
public void testBean(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringAnnoConfig.class);
Arrays.stream(context.getBeanNamesForType(Order.class)).forEach(c->{
System.out.println(c);
});
}
测试结果
image.png
通过结果可以看出bean的名称就是我们手动的name属性
这时候小伙伴们肯定在想,如果我不指定bean的名称呢?
配置类
package org.example.config;
import org.example.entity.Order;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SpringAnnoConfig {
@Bean
public Order order(){
return new Order();
}
}
测试类
@Test
public void testBean(){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringAnnoConfig.class);
Arrays.stream(context.getBeanNamesForType(Order.class)).forEach(c->{
System.out.println(c);
});
}
测试结果
image.png
通过测试结果可以看到,如果我们不手动指定name,就按照方法名作为bean的名称。