@Configuration与@Component作为配置类的区别详解

作者:弟中弟中弟 时间:2023-03-09 19:50:15 

@Configuration注解的类:


/**
* @Description 测试用的配置类
* @Author 弟中弟
* @CreateTime 2019/6/18 14:35
*/
@Configuration
public class MyBeanConfig {
@Bean
public Country country(){
 return new Country();
}
@Bean
public UserInfo userInfo(){
 return new UserInfo(country());
}
}

@Component注解的类:


/**
* @Description 测试用的配置类
* @Author 弟中弟
* @CreateTime 2019/6/18 14:36
*/
@Component
public class MyBeanConfig {
@Bean
public Country country(){
 return new Country();
}
@Bean
public UserInfo userInfo(){
 return new UserInfo(country());
}
}

测试:


@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoTest {

@Autowired
 private Country country;

@Autowired
 private UserInfo userInfo;

@Test
 public void myTest() {
   boolean result = userInfo.getCountry() == country;
   System.out.println(result ? "同一个country" : "不同的country");
 }

}

如果是@Configuration打印出来的则是同一个country,@Component则是不同的country,这是为什么呢?


@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
 @AliasFor(
   annotation = Component.class
 )
 String value() default "";
}

你点开@Configuration会发现其实他也是被@Component修饰的,因此context:component-scan/ 或者 @ComponentScan都能处理@Configuration注解的类。

@Configuration标记的类必须符合下面的要求:

配置类必须以类的形式提供(不能是工厂方法返回的实例),允许通过生成子类在运行时增强(cglib * )。

配置类不能是 final 类(没法 * )。

配置注解通常为了通过 @Bean 注解生成 Spring 容器管理的类,

配置类必须是非本地的(即不能在方法中声明,不能是 private)。

任何嵌套配置类都必须声明为static。

@Bean 方法可能不会反过来创建进一步的配置类(也就是返回的 bean 如果带有

@Configuration,也不会被特殊处理,只会作为普通的 bean)。

但是spring容器在启动时有个专门处理@Configuration的类,会对@Configuration修饰的类cglib * 进行增强,这也是@Configuration为什么需要符合上面的要求中的部分原因,那具体会增强什么呢?

这里是个人整理的思路 如果有错请指点

userInfo()中调用了country(),因为是方法那必然country()生成新的new contry(),所以 * 增加就会对其进行判断如果userInfo中调用的方法还有@Bean修饰,那就会直接调用spring容器中的country实例,不再调用country(),那必然是一个对象了,因为spring容器中的bean默认是单例。不理解比如xml配置的bean


<bean id="country" class="com.hhh.demo.Country" scope="singleton"/>

这里scope默认是单例。

以上是个人理解,详情源码的分析请看https://www.jb51.net/article/153430.htm

但是如果我就想用@Component,那没有@Component的类没有 * 咋办呢?


/**
* @Description 测试用的配置类
* @Author 弟中弟
* @CreateTime 2019/6/18 14:36
*/
@Component
public class MyBeanConfig {
@Autowired
private Country country;
@Bean
public Country country(){
 return new Country();
}
@Bean
public UserInfo userInfo(){
 return new UserInfo(country);
}
}

这样就保证是同一个Country实例了

来源:https://juejin.im/post/5d087cebe51d457759648706

标签:@Configuration,@Component,区别
0
投稿

猜你喜欢

  • Android版本更新实例详解

    2023-08-05 21:54:54
  • 用C#获取硬盘序列号,CPU序列号,网卡MAC地址的源码

    2022-01-04 01:19:59
  • springcloud feign传输List的坑及解决

    2023-06-20 18:31:57
  • Android 使用Picasso加载网络图片等比例缩放的实现方法

    2023-08-29 15:36:32
  • Spring boot的上传图片功能实例详解

    2022-10-09 09:52:00
  • SpringCloud之熔断器Hystrix的实现

    2021-09-21 01:39:26
  • springboot全局异常处理代码实例

    2023-02-05 20:41:36
  • Flutter Widget开发之Focus组件图文详解

    2023-06-21 03:47:41
  • 一文搞懂Mybatis-plus的分页查询操作

    2023-11-25 10:23:17
  • Java 10 局部变量类型推断浅析

    2023-11-25 06:24:13
  • 关于Spring Boot项目的 log4j2 核弹漏洞问题(一行代码配置搞定)

    2022-08-26 03:04:20
  • Spring Boot修改启动端口的方法

    2022-02-10 05:49:55
  • Android笔记之:CM9源码下载与编译的应用

    2022-09-23 05:40:14
  • Java编程将汉字转Unicode码代码示例

    2023-11-09 17:33:39
  • JavaWeb实现文件上传与下载的方法

    2023-12-23 04:42:56
  • Android百度地图应用之基本地图功能实现

    2022-11-20 07:01:41
  • Java中final与继承操作实例分析

    2023-09-14 08:56:02
  • 基于Spring-Security自定义登陆错误提示信息

    2021-09-20 17:33:40
  • 最优雅地整合 Spring & Spring MVC & MyBatis 搭建 Java 企业级应用(附源码)

    2023-09-26 18:27:57
  • 解决Springboot项目启动后自动创建多表关联的数据库与表的方案

    2023-11-24 01:11:27
  • asp之家 软件编程 m.aspxhome.com