昨天,一位朋友跟我反馈,在面试中被问到注解@Configuration
@Configuration
和 @Component
的区别。
一句话概括就是 @Configuration
中所有带 @Bean
注解的方法都会被动态代理,因此调用该方法返回的都是同一个实例。
理解:调用@Configuration
类中的@Bean注解的方法,返回的是同一个示例;而调用@Component
类中的@Bean
和
@Component
的区别。理解:调用
一句话概括就是
@Configuration
中所有带@Bean
注解的方法都会被动态代理,因此调用该方法返回的都是同一个实例。
@Configuration
类中的@Bean注解的方法,返回的是同一个示例;而调用@Component
类中的@Bean
注解的方法,返回的是一个新的实例。🎜“🎜🎜注意:上面说的调用,而不是从spring容器中获取! 见最下面的示例 1 及 示例 2🎜🎜🎜下面看看实现的细节。🎜@Configuration 注解
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Configuration { String value() default ""; }
从定义来看, @Configuration
注解本质上还是@Component
,因此 <context :component-scan></context>
或者 @ ComponentScan
全面处理@Configuration
注解的类。@Configuration
注解本质上还是@Component
,因此 <component-scan></component-scan>
或者 @ComponentScan
都能处理@Configuration
注解的类。
@Configuration
@Configuration
标记的类别必须符合下面的要求:🎜配置类必须以类的形式提供(不能是工厂方法返回的实例),允许通过生成子类在运行时增强(cglib 动态代理)。 配置类不能是final 类(没法动态代理)。 配置注解通常为了通过 @Bean
注解生成 Spring 容器管理的类,配置类必须是非本地的(即不能在方法中声明,不能是 private)。 任何嵌套配置类都必须声明为static。 @Bean
方法可能不会反过来创建进一步的配置类(也就是返回的 bean 如果带有@Configuration
,也不会被特殊处理,只会作为普通的 bean)。
@Bean 注解方法执行策略
先给一个简单的示例代码:
@Configuration public class MyBeanConfig { @Bean public Country country(){ return new Country(); } @Bean public UserInfo userInfo(){ return new UserInfo(country()); } }
相信大多数人第一次看到上面 userInfo()
中调用 country()
时,会认为这里的 Country和上面 @Bean
方法返回的 Country 可能不是同一个对象,因此可能会通过下面的方式来替代这种方式:
@Autowired
private Country country;
实际上不需要这么做(后面会给出需要这样做的场景),直接调用country()
方法返回的是同一个实例。
@Component 注解
@Component
注解并没有通过 cglib 来代理@Bean
方法的调用,因此像下面这样配置时,就是两个不同的 country
。
@Component public class MyBeanConfig { @Bean public Country country(){ return new Country(); } @Bean public UserInfo userInfo(){ return new UserInfo(country()); } }
有些特殊情况下,我们不希望 MyBeanConfig
被代理(代理后会变成WebMvcConfig$$EnhancerBySpringCGLIB$$8bef3235293
)时,就得用 @Component
,这种情况下,上面的写法就需要改成下面这样:
@Component public class MyBeanConfig { @Autowired private Country country; @Bean public Country country(){ return new Country(); } @Bean public UserInfo userInfo(){ return new UserInfo(country); } }
这种方式可以保证使用的同一个 Country 实例。
示例 1:调用@Configuration类中的@Bean注解的方法,返回的是同一个示例
第一个bean类
package com.xl.test.logtest.utils; public class Child { private String name = "the child"; public String getName() { return name; } public void setName(String name) { this.name = name; } }
第二个bean类
package com.xl.test.logtest.utils; public class Woman { private String name = "the woman"; private Child child; public String getName() { return name; } public void setName(String name) { this.name = name; } public Child getChild() { return child; } public void setChild(Child child) { this.child = child; } }
@Configuration
类
package com.xl.test.logtest.utils; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; @Configuration //@Component public class Human { @Bean public Woman getWomanBean() { Woman woman = new Woman(); woman.setChild(getChildBean()); // 直接调用@Bean注解的方法方法getChildBean() return woman; } @Bean public Child getChildBean() { return new Child(); } }
测试类 I
本测试类为一个配置类,这样启动项目是就可以看到测试效果的,更加快捷;也可以使用其他方式测试见下面的测试类 II
package com.xl.test.logtest.utils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; @Configuration public class Man { @Autowired public Man(Woman wn, Child child) { System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); System.out.println(wn.getChild() == child ? "是同一个对象":"不是同一个对象"); } }
启动项目,查看输出结果:

测试类 II
package com.xl.test.logtest.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.xl.test.logtest.utils.Child; import com.xl.test.logtest.utils.Woman; @RestController public class LogTestController { @Autowired Woman woman ; @Autowired Child child; @GetMapping("/log") public String log() { return woman.getChild() == child ? "是同一个对象":"不是同一个对象"; } }
浏览器访问项目,查看结果;输入localhost:8080/log

示例 2 :调用@Component类中的@Bean注解的方法,返回的是一个新的实例。
测试代码,只需要将@Configuration
改为@Component
即可!其他的均不变
package com.xl.test.logtest.utils; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; //@Configuration @Component public class Human { @Bean public Woman getWomanBean() { Woman woman = new Woman(); woman.setChild(getChildBean()); // 直接调用@Bean注解的方法方法getChildBean() return woman; } @Bean public Child getChildBean() { return new Child(); } }
测试 :

控制台和浏览器展示,均符合预期!
最后,如果你也需要修改简历,需要模拟面试的。
以上是面试官:@Configuration 和 @Component 的区别的详细内容。更多信息请关注PHP中文网其他相关文章!

ByteCodeachievesPlatFormIndenceByByByByByByExecutedBoviratualMachine(VM),允许CodetorunonanyplatformwithTheApprepreprepvm.Forexample,Javabytecodecodecodecodecanrunonanydevicewithajvm

Java不能做到100%的平台独立性,但其平台独立性通过JVM和字节码实现,确保代码在不同平台上运行。具体实现包括:1.编译成字节码;2.JVM的解释执行;3.标准库的一致性。然而,JVM实现差异、操作系统和硬件差异以及第三方库的兼容性可能影响其平台独立性。

Java通过“一次编写,到处运行”实现平台独立性,提升代码可维护性:1.代码重用性高,减少重复开发;2.维护成本低,只需一处修改;3.团队协作效率高,方便知识共享。

在新平台上创建JVM面临的主要挑战包括硬件兼容性、操作系统兼容性和性能优化。1.硬件兼容性:需要确保JVM能正确使用新平台的处理器指令集,如RISC-V。2.操作系统兼容性:JVM需正确调用新平台的系统API,如Linux。3.性能优化:需进行性能测试和调优,调整垃圾回收策略以适应新平台的内存特性。

javafxeffectife addressEddressEndressInconSiscies uningies uningusing inaplatform-agnosticsCenegraphandCssStyling.1)itabstractsplactsplatsplatsplatsplatformsthercensthascenegenceenceNaSceneGraph,确保ConsistSistEntertRenderingRenderingRenderingRenderingAccomWindows,MacOs,MacOS,MacOS,andlinux.2)

JVM的工作原理是将Java代码转换为机器码并管理资源。1)类加载:加载.class文件到内存。2)运行时数据区:管理内存区域。3)执行引擎:解释或编译执行字节码。4)本地方法接口:通过JNI与操作系统交互。

JVM使Java实现跨平台运行。1)JVM加载、验证和执行字节码。2)JVM的工作包括类加载、字节码验证、解释执行和内存管理。3)JVM支持高级功能如动态类加载和反射。

Java应用可通过以下步骤在不同操作系统上运行:1)使用File或Paths类处理文件路径;2)通过System.getenv()设置和获取环境变量;3)利用Maven或Gradle管理依赖并测试。Java的跨平台能力依赖于JVM的抽象层,但仍需手动处理某些操作系统特定的功能。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3 英文版
推荐:为Win版本,支持代码提示!