@Configuration注解与@Bean

@Configuration这个注解可以加在类上,让这个类的功能等同于一个bean xml配置文件
会默认创建一个无参对象在spring容器中
@Bean注解用在方法上,表示通过方法来定义一个bean,默认将方法名称作为bean名称,将方法返回值作为bean对象,注册到spring容器中

/**
 * @Configuration注解将对象加入spring容器,相当于bean.xml
 * 默认会将configactionBeanDemo对象配置到spring容器中
 */
@Configuration
public class ConfigactionBeanDemo {
    /**
     * @Bean类似于bean xml配置文件中的bean元素其中的元素也和配置文件相似
     * @return
     */
//    bean名称为方法默认值:configactionBeanDemo1
    @Bean
    public ConfigactionBeanDemo configactionBeanDemo1 () {
        return new ConfigactionBeanDemo();
    }
    //bean名称通过value指定了:configactionBeanDemo2
    @Bean("configactionBeanDemo2")
    public ConfigactionBeanDemo configactionBeanDemo2 () {
        return new ConfigactionBeanDemo();
    }
    //configactionBeanDemo:could not be registered. A bean with that name has already been defined  and overriding is disabled.
    //bean名称为:configactionBeanDemo,2个别名:[configactionBeanAlias1,configactionBeanAlias2]
    @Bean({"configactionBeanDemo3","configactionBeanAlias1","configactionBeanAlias2"})
    public ConfigactionBeanDemo configactionBeanDemo3 () {
        return new ConfigactionBeanDemo();
    }

}
AnnotationConfigApplicationContext来加载@Configuration修饰的类

去掉@Configuration会怎样?

内容和ConfigBean类一样,只是将@Configuration注解去掉了

public class ConfigBean1 {

    //bean名称为方法默认值:user1
    @Bean
    public User user1() {
        return new User();
    }

    //bean名称通过value指定了:user2Bean
    @Bean("user2Bean")
    public User user2() {
        return new User();
    }

    //bean名称为:user3Bean,2个别名:[user3BeanAlias1,user3BeanAlias2]
    @Bean({"user3Bean", "user3BeanAlias1", "user3BeanAlias2"})
    public User user3() {
        return new User();
    }

}

输出结果:

bean名称:org.springframework.context.annotation.internalConfigurationAnnotationProcessor,别名:[],bean对象:org.springframework.context.annotation.ConfigurationClassPostProcessor@333291e3
bean名称:org.springframework.context.annotation.internalAutowiredAnnotationProcessor,别名:[],bean对象:org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@479d31f3
bean名称:org.springframework.context.annotation.internalCommonAnnotationProcessor,别名:[],bean对象:org.springframework.context.annotation.CommonAnnotationBeanPostProcessor@40ef3420
bean名称:org.springframework.context.event.internalEventListenerProcessor,别名:[],bean对象:org.springframework.context.event.EventListenerMethodProcessor@498d318c
bean名称:org.springframework.context.event.internalEventListenerFactory,别名:[],bean对象:org.springframework.context.event.DefaultEventListenerFactory@6e171cd7
bean名称:configBean1,别名:[],bean对象:com.javacode2018.lesson001.demo20.ConfigBean1@402bba4f
bean名称:user1,别名:[],bean对象:com.javacode2018.lesson001.demo20.User@795cd85e
bean名称:user2Bean,别名:[],bean对象:com.javacode2018.lesson001.demo20.User@59fd97a8
bean名称:user3Bean,别名:[user3BeanAlias2, user3BeanAlias1],bean对象:com.javacode2018.lesson001.demo20.User@f5ac9e4

可以看出有没有@Configuration注解,@Bean都会起效,都会将@Bean修饰的方法作为bean注册到容器中

通常情况下,bean之间是有依赖关系的,使用依赖关系看看有没有Configuraction注解的区别

ServiceA

public class ServiceA {
}

ServiceB

public class ServiceB {
    private ServiceA serviceA;

    public ServiceB(ServiceA serviceA) {
        this.serviceA = serviceA;
    }

    @Override
    public String toString() {
        return "ServiceB{" +
                "serviceA=" + serviceA +
                '}';
    }
}

ServiceB依赖于ServiceA,ServiceB通过构造器注入ServiceA。

来个@Configuration类管理上面对象。

@Configuration
public class ConfigBean2 {

    @Bean
    public ServiceA serviceA() {
        System.out.println("调用serviceA()方法"); //@0
        return new ServiceA();
    }

    @Bean
    ServiceB serviceB1() {
        System.out.println("调用serviceB1()方法");
        ServiceA serviceA = this.serviceA(); //@1
        return new ServiceB(serviceA);
    }

    @Bean
    ServiceB serviceB2() {
        System.out.println("调用serviceB2()方法");
        ServiceA serviceA = this.serviceA(); //@2
        return new ServiceB(serviceA);
    }

}

上面通过@Bean注解,向容器中注册了3个bean
注意@1和@2,通过this.serviceA()获取需要注入的ServiceA对象。
上面每个方法第一行都输出了一行日志。

@Test
public void test3() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigBean2.class);
    for (String beanName : context.getBeanDefinitionNames()) {
        //别名
        String[] aliases = context.getAliases(beanName);
        System.out.println(String.format("bean名称:%s,别名:%s,bean对象:%s",
                beanName,
                Arrays.asList(aliases),
                context.getBean(beanName)));
    }
}

输出结果

调用serviceA()方法
调用serviceB1()方法
调用serviceB2()方法
bean名称:configBean2,别名:[],bean对象:com.javacode2018.lesson001.demo20.ConfigBean2$$EnhancerBySpringCGLIB$$ffa0178@77f1baf5
bean名称:serviceA,别名:[],bean对象:com.javacode2018.lesson001.demo20.ServiceA@41a2befb
bean名称:serviceB1,别名:[],bean对象:ServiceB{serviceA=com.javacode2018.lesson001.demo20.ServiceA@41a2befb}
bean名称:serviceB2,别名:[],bean对象:ServiceB{serviceA=com.javacode2018.lesson001.demo20.ServiceA@41a2befb}

总结

  1. 前三行可以看出,被@Bean修饰的方法都只被调用了一次,这个很关键
  2. 最后三行中可以看出都是同一个ServiceA对象,都是ServiceA@41a2befb这个实例
  3. @Configuration注解修饰的类,会被spring通过cglib做增强处理,通过cglib会生成一个代理对象,代理会拦截所有被@Bean注解修饰的方法,可以确保一些bean是单例的
  4. 不管@Bean所在的类上是否有@Configuration注解,都可以将@Bean修饰的方法作为一个bean注册到spring容器中
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值