@Configuration和@Component注解的区别

简介: 1.@Configuration和@Component注解的源码如下(1)Configuration注解源码如下:

@Configuration和@Component注解的区别



1.@Configuration和@Component注解的源码如下


(1)Configuration注解源码如下:


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


(2)Component注解源码如下:


@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {
    String value() default "";
}


(3)通过源码可以看出, @Configuration 注解本质上还是 @Component,只不过Configuration注解多了

boolean proxyBeanMethods() default true;这一行内容。


①@Configuration 中所有带 @Bean 注解的方法都会被动态代理(cglib),因此调用该方法返回的都是同一个实例 ②@Conponent 修饰的类不会被代理,每实例化一次就会创建一个新的对象。


2.Configuration注解的使用细节


(1)配置类里面使用@Bean标注在方法上给容器注册组件,默认是单实例的


(2)配置类本身也是组件


(3)proxyBeanMethods:代理bean的方法

Full(proxyBeanMethods = true)【保证每个@Bean方法被调用多少次返回的组件都是单实例的】

Lite(proxyBeanMethods = false)【每个@Bean方法被调用多少次返回的组件都是新创建的】

组件依赖必须使用Full模式默认。其他默认是否Lite模式


(4)实例


@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig {
    @Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
    public User user(){
        User zhangsan = new User("zhangsan", 18);
        //user组件依赖了Pet组件
        zhangsan.setPet(tomcatPet());
        return zhangsan;
    }
    @Bean("tom")
    public Pet tomcatPet(){
        return new Pet("tomcat");
     } }


(5)最佳实战


①配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断


②配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式

相关文章
|
3月前
|
XML Java 数据格式
@Configuration配置类注解的理解
@Configuration配置类注解的理解
@Configuration配置类注解的理解
|
4月前
|
Java 数据库连接 API
SpringBoot【问题 01】借助@PostConstruct解决使用@Component注解的类用@Resource注入Mapper接口为null的问题(原因解析+解决方法)
SpringBoot【问题 01】借助@PostConstruct解决使用@Component注解的类用@Resource注入Mapper接口为null的问题(原因解析+解决方法)
85 0
|
4月前
|
XML 前端开发 Java
SpringMVC中context:annotation-config与mvc:annotation-driven和context:component-scan区别详解
SpringMVC中context:annotation-config与mvc:annotation-driven和context:component-scan区别详解
35 0
|
9月前
|
XML SpringCloudAlibaba Java
Spring注解配置:@Configuration 和 @Component 区别及原理详解
随着`Spring Boot`的盛行,注解配置式开发受到了大家的青睐,从此告别了基于`Spring`开发的繁琐`XML`配置。这里先来提纲挈领的了解一下`Spring`内部对于配置注解的定义,如`@Component、@Configuration、@Bean、@Import`等注解,从功能上来讲,这些注解所负责的功能的确不相同,但是
178 1
记一个SpringBoot中属性注入失败的问题Consider defining a bean of type ''' in your configuration...
记一个SpringBoot中属性注入失败的问题Consider defining a bean of type ''' in your configuration...
228 0
Zp
|
XML Java 数据格式
@Component注解的作用
@Component注解的作用
Zp
227 0
@Component注解的作用
|
缓存 Java Spring
配置类为什么要添加@Configuration注解?(2)
配置类为什么要添加@Configuration注解?(2)
132 0
配置类为什么要添加@Configuration注解?(2)
|
Java Spring 容器
配置类为什么要添加@Configuration注解?(1)
配置类为什么要添加@Configuration注解?(1)
117 0
配置类为什么要添加@Configuration注解?(1)
记一个SpringBoot中属性注入失败的问题Consider defining a bean of type ''' in your configuration...
记一个SpringBoot中属性注入失败的问题Consider defining a bean of type ''' in your configuration...
147 0
|
Java Spring 容器
@Configuration注解
@Configuration注解
115 0