SpringBoot-底层注解-@configuration

底层注解:@configuration

先前说明
用在Config类中,用于添加组件,类似于传统的配置xml文件,Config下还有一个@Bean注解用来注册组件

具体的写法:

@Bean   //给容器中添加组件。以方法名作为组件的id,返回类型就是组件类型,方法返回的对象就是组件在容器中的实例
    public User user01(){
        return new User("zhangsan",18);
    }

传统的容器中添加组件,通过xml添加,Spring进行xml配置的方法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="com.gis507.boot.bean.User">
        <property name="name" value="zhangsan"></property>
        <property name="age" value="18"></property>
    </bean>

    <bean id="cat" class="com.gis507.boot.bean.Pet">
        <property name="name" value="tom"></property>
    </bean>

</beans>

在SpringBoot中不使用这种方法添加,SpringBoot往容器中添加组件

使用注解的方式添加,@Configuration

具体实现:

  1. 创建一个类:MyConfig.java

  2. 在类上标注一个注解@Configuration,告诉SpringBoot这是一个配置类 == 配置文件,这个类就类似于一个配置文件

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

    import com.gis507.boot.bean.Pet;
    import com.gis507.boot.bean.User;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration  //告诉SpringBoot这是一个配置类 == 配置文件
    public class MyConfig {
        @Bean   //给容器中添加组件。以方法名作为组件的id,返回类型就是组件类型,方法返回的对象就是组件在容器中的实例
        public User user01(){
            	return new User("zhangsan",18);
        }
    @Bean
        public Pet tomcatPet(){
            	return new Pet("tomcat");
        }
    }
    

    除此之外还可以根据@Bean(“user”)对组件进行重命名

    @Bean("user")
    
  4. 验证容器中的组件

    可以使用之前编写的组件查看代码进行查看是否有user01和tomcatPet组件

    @SpringBootApplication
    public class MainApplication {
        public static void main(String[] args) {
            //1、返回IOC容器
            ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
            //2、查看容器中的组件
            String[] names = run.getBeanDefinitionNames();
            //3、打印出来
            for (String name : names){
                System.out.println(name);
            }
        }
    }
    

在这里插入图片描述

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

在SpringBoot2后的版本里@Configuration注解多了一个注解默认属性

boolean proxyBeanMethods() default true;

proxyBeanMethods:代理bean方法,(是否代理)true:外部无论对配置类的这个组件调用多少次获取的都是之前注册容器中的单实例对象

如果@Configuration(proxyBeanMethods=true)代理对象调用方法,SpringBoot总会检查这个组件是否在容器中有,保持组件单实例,如果调成false,就不会单实例

Full(proxyBeanMethods = true) 全模式

Full(proxyBeanMethods = fasle) 轻量级模式

模式推荐:

如果给容器中仅仅是添加组件,别的类并不依赖这些组件,可以使用轻量级模式

如果容器中的组件被依赖,使用全模式

MyConfig.java类
import com.gis507.boot.bean.Pet;
import com.gis507.boot.bean.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置类里面使用@Bean标注在方法上给容器注册组件,默认是单实例的
 * 配置类也是组件
 * proxyBeanMethods:true:外部无论对配置类的这个组件调用多少次获取的都是之前注册容器中的单实例对象
 * Full(proxyBeanMethods = true)
 * Full(proxyBeanMethods = fasle)   推荐仅仅添加组件,没有其他类的组件依赖时候使用
 */
@Configuration(proxyBeanMethods = true)  //告诉SpringBoot这是一个配置类 == 配置文件
public class MyConfig {
    @Bean   //给容器中添加组件。以方法名作为组件的id,返回类型就是组件类型,方法返回的对象就是组件在容器中的实例
    public User user01(){
        return new User("zhangsan",18);
    }
    @Bean
    public Pet tomcatPet(){
        return new Pet("tomcat");
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牧码文

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值