容器,注解功能,

导读:本篇文章讲解 容器,注解功能,,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

容器功能

1:组件添加;@Configuration

1、配置类里面使用@Bean 标注在方法上给容器注册组件,默认也是单实例的
2、配置类本身就是一个组件

基本使用:Full模式和Lite模式

	boolean proxyBeanMethods() default true;

proxyBeanMethods:代理Bean 的方法

  • Full(proxyBeanMethods = true) 全模式
  • Lite(proxyBeanMethods = false) 轻量级模式
    组件依赖

最佳实战:

  • 配置类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断
  • 配置类组件之间有依赖关系,方法会被调用得到之前单实例组件,用Full模式
/*
1、配置类里面使用@Bean 标注在方法上给容器注册组件,默认也是单实例的
2、配置类本身就是一个组件
3、proxyBeanMethods:代理Bean 的方法
       Full(proxyBeanMethods = true) 全模式
       Lite(proxyBeanMethods = false) 轻量级模式
       组件依赖
 */

//告诉Bean这是一个配置类,=== 配置文件
@Configuration(proxyBeanMethods = true)
public class MyConfig {

    /**
     * 外部无论对注册到配置类中的这个组件调用多少次,获取的都是之前注册到容器中的单实例对象
     * @return
     */
    @Bean  //给容器中添加组件,以方法名作为组件的id,返回类型就是组件的类型,返回的值,就是组件在容器中的实例;
    public User test1(){
        User zhangsan = new User("zhangsan", 3);
        //user组件依赖了Cat 组件
        zhangsan.setCat(tomcatPet());

        return zhangsan;
    }

    @Bean("tom")
    public Cat tomcatPet(){
        return new Cat("tiechui");
    }

测试:

//本身就是一个组件

//程序的主入口
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        //1:返回我们的IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);

        //2:查看容器中的组件
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }

        //3:从容器中获取组件、
        Cat tom1 = run.getBean("tom", Cat.class);
        Cat tom2 = run.getBean("tom", Cat.class);

        //1、配置类里面使用@Bean 标注在方法上给容器注册组件,默认也是单实例的
        System.out.println("组件:"+(tom1 == tom2));

        //获取到的是代理对象com.baidu.demo.config.MyConfig$$EnhancerBySpringCGLIB$$95fd84ba@68809cc7
        MyConfig bean = run.getBean(MyConfig.class);
        System.out.println(bean);

        //如果@Configuration(proxyBeanMethods = true)代理对象调用方法,SpringBoot总会检查这个组件是否在容器中
        //如果有,保持组件的单实例
        User user1 = bean.test1();
        User user2 = bean.test1();
        System.out.println(user1 == user2);

        System.out.println("===============");
        User test1 = run.getBean("test1", User.class);
        Cat tom = run.getBean("tom", Cat.class);

        System.out.println(test1.getCat() == tom);


    }

2、@Bean、@Component、@Controller、@Service、@Repository

  • @Controller 控制层类,
  • @Service 业务层类,
  • @Repository 持久层类,
  • @Component 无法归类到前3种时就称为组件。
  • @Bean组件

3、@ComponentScan 、@Import

@Import({User.class, Matcher.class})
给容器中自动创建出这两个类型的组件,默认组件的名字就是全类名
在这里插入图片描述

4、Conditional (条件) 条件注解

有tom组件,所以加载。
@ConditionalOnBean(name = "tom")


//    @Bean("tom")
    public Cat tomcatPet(){
        return new Cat("tiechui");
    }

在这里插入图片描述
在这里插入图片描述

/*
1、配置类里面使用@Bean 标注在方法上给容器注册组件,默认也是单实例的
2、配置类本身就是一个组件
3、proxyBeanMethods:代理Bean 的方法
       Full(proxyBeanMethods = true) 全模式
       Lite(proxyBeanMethods = false) 轻量级模式
       组件依赖

 4、@Import({User.class, Matcher.class})
        给容器中自动创建出这两个类型的组件,默认组件的名字就是全类名
 */

@Import({User.class, Matcher.class})

//告诉Bean这是一个配置类,=== 配置文件
@Configuration(proxyBeanMethods = false)
//@ConditionalOnBean(name = "tom") //
@ConditionalOnMissingBean(name = "tom")
@EnableConfigurationProperties(Car.class) //1、开启Car配置绑定功能,把Car这个组件自动注入到容器中
public class MyConfig {

    /**
     * 外部无论对注册到配置类中的这个组件调用多少次,获取的都是之前注册到容器中的单实例对象
     *
     * @return
     */
    @Bean  //给容器中添加组件,以方法名作为组件的id,返回类型就是组件的类型,返回的值,就是组件在容器中的实例;
    public User test1() {
        User zhangsan = new User("zhangsan", 3);
        //user组件依赖了Cat 组件
        zhangsan.setCat(tomcatPet());

        return zhangsan;
    }

    @Bean("tom22")
    public Cat tomcatPet() {
        return new Cat("tiechui");
    }
}

//本身就是一个自组件
//程序的主入口
@SpringBootApplication()
public class DemoApplication {

    public static void main(String[] args) {
        //1:返回我们的IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);

        //2:查看容器中的组件
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }


        System.out.println("========");
        boolean tom3 = run.containsBean("tom");
        System.out.println("容器中Tom组件" + tom3);

        boolean test11 = run.containsBean("test1");
        System.out.println("容器中test1组件" + test11);

        boolean tom22 = run.containsBean("tom22");
        System.out.println("容器中test22组件" + tom22);
    }

}

在这里插入图片描述

5、配置绑定

配置绑定的两种方式:
1、@Component + @Configuration Properties 在当前类上配置;
2、@EnableConfigurationProperties + @Configuration Properties 要在配置类写;

第一种、@Component + @Configuration Properties 在当前类上配置;

@Data
@AllArgsConstructor
@NoArgsConstructor

/**
 * 只有在容器中,才会拥有Spring Boot的强大功能
 */
@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {

    private String brand;
    private Integer price;

}

配置文件


mycar.brand=BYD
mycar.price=100000

测试:

第二种、@EnableConfigurationProperties + @Configuration Properties 要在配置类写

@EnableConfigurationProperties(Car.class) //1、开启Car配置绑定功能,把Car这个组件自动注入到容器中
public class MyConfig {
	
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/71845.html

(0)
小半的头像小半

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!