一句话,@Configuration中所有被@Bean注解的方法都会被动态代理,所以调用这个方法会返回同一个实例。理解:在@Configuration类中调用@Bean注解方法返回相同的例子;调用@Component类中的@Bean注解方法返回一个新的实例。注意:上面所说的调用不是从spring容器中获取的!请参阅底部示例1和示例2以了解实现细节。@Configuration注解:@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic@interfaceConfiguration{Stringvalue()default"";}从定义上看,@Configuration注解本质上就是@Component,所以或@ComponentScan都可以处理@Configuration注解类。带有@Configuration标记的类必须满足以下要求:配置类必须以类的形式提供(不是工厂方法返回的实例),允许在运行时通过生成子类(cglib动态代理)进行增强。配置类不能是最终类(没有动态代理)。配置注解通常为了通过@Bean注解生成Spring容器管理的类,配置类必须是非本地的(即不能在方法中声明,也不能是private)。任何嵌套的配置类都必须声明为静态的。@Bean方法可能不会依次创建进一步的配置类(也就是说,如果返回的bean有@Configuration,它不会被特殊对待,它只会作为一个普通的bean使用)。@Bean注解方法执行策略推荐一个开源免费的SpringBoot最全教程:https://github.com/javastacks/spring-boot-best-practice先给个简单的示例代码:@ConfigurationpublicclassMyBeanConfig{@Beanpublic国家country(){returnnewCountry();}@BeanpublicUserInfouserInfo(){returnnewUserInfo(country());}}相信大部分人都是第一次看到上面userInfo()中调用country(),会认为这里的Country可能和上面@Bean方法返回的Country不是同一个对象,所以这个方法可以用下面的方法代替:@AutowiredprivateCountrycountry;其实这不是必须的(后面场景会给出要求),直接调用country()方法返回同一个实例。@Component注解@Component注解并没有通过cglib代理调用@Bean方法,所以像下面这样配置的时候,就是两个不同的国家。@ComponentpublicclassMyBeanConfig{@BeanpublicCountrycountry(){returnnewCountry();}@BeanpublicUserInfouserInfo(){returnnewUserInfo(country());}}在某些特殊情况下,我们不希望MyBeanConfig被代理(代理后会变成WebMvcConfig$$EnhancerBySpringCGLIB$$8bef3235293),就必须使用@Component。这种情况下,需要将上面的写法改成如下:@ComponentpublicclassMyBeanConfig{@AutowiredprivateCountrycountry;@BeanpublicCountrycountry(){returnnewCountry();}@BeanpublicUserInfouserInfo(){returnnewUserInfo(country);}}这个方法可以保证使用同一个Country实例。示例1:调用@Configuration类中的@Bean注解方法,返回相同的示例。第一个bean类包com.xl.test.logtest.utils;publicclassChild{privateStringname="thechild";publicStringgetName(){返回名称;}publicvoidsetName(Stringname){this.name=name;}}第二个bean类包com.xl.test.logtest.utils;publicclassWoman{privateStringname="thewoman";私人儿童儿童;publicStringgetName(){返回名称;}publicvoidsetName(Stringname){this.name=name;}publicChildgetChild(){返回孩子;}publicvoidsetChild(Childchild){this.child=child;}}@Configuration类包com.xl.test.logtest.utils;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.stereotype.Component;@Configuration//@ComponentpublicclassHuman{@BeanpublicWomangetWomanBean(){Womanwoman=newWoman();woman.setChild(getChildBean());//直接调用@Bean注解的方法getChildBe一个()返回女人;}@BeanpublicChildgetChildBean(){返回新的Child();}}测试类一这个测试类是一个配置类,所以在启动项目的时候就可以看到测试效果,比较快;也可以使用其他方法测试看下面的测试类IIpackagecom.xl.test.logtest.utils;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassMan{@AutowiredpublicMan(Womanwn,Childchild){System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>”);System.out.println(wn.getChild()==child?"是同一个对象":"不是同一个对象");}}启动项目,查看输出结果:testclassIIpackagecom.xl.test.logtest.controller;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;导入org.springframework.web.bind.annotation.RestController;导入com.xl。test.logtest.utils.Child;importcom.xl.test.logtest.utils.Woman;@RestControllerpublicclassLogTestController{@AutowiredWomanwoman;@Autowired孩子孩子;@GetMapping("/log")publicStringlog(){返回woman.getChild()==child?“是同一个对象”:“不是同一个对象”;}}浏览器访问项目并查看结果;输入localhost:8080/log示例2:在@Component类中调用@Bean注解的方法返回一个新的实例要测试代码,只需将@Configuration更改为@Component!其他不变packagecom.xl.test.logtest.utils;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.stereotype.Component;//@Configuration@ComponentpublicclassHuman{@BeanpublicWomangetWomanBean(){Womanwoman=newWoman();woman.setChild(getChildBean());//直接调用@Bean注解方法methodgetChildBean()returnwoman;}@BeanpublicChildgetChildBean(){返回新的Child();}}测试:控制台和浏览器显示,均符合预期!原文:blog.csdn.net/qq_29025955/article/details/128818957近期热点文章推荐:1.1000+Java面试题及答案(2022最新版)2.厉害了!Java协程来了。..3.SpringBoot2.x教程,太全面了!4.不要用爆破爆满画面,试试装饰者模式,这才是优雅的方式!!5.《Java开发手册(嵩山版)》最新发布,赶快下载吧!感觉不错,别忘了点赞+转发!