• spring注解第04课 @Import


    1.beans

    package com.atguigu.bean;
    
    public class Blue {
        
        public Blue(){
            System.out.println("blue...constructor");
        }
        
        public void init(){
            System.out.println("blue...init...");
        }
        
        public void detory(){
            System.out.println("blue...detory...");
        }
    }
    package com.atguigu.bean;
    
    public class Yellow {
    
    }
    package com.atguigu.bean;
    
    public class Color {
        
        private Car car;
    
        public Car getCar() {
            return car;
        }
    
        public void setCar(Car car) {
            this.car = car;
        }
    
        @Override
        public String toString() {
            return "Color [car=" + car + "]";
        }
        
        
    
    }

    2.引入@Import

    package com.atguigu.config;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;import com.atguigu.bean.Blue;
    import com.atguigu.bean.Color;import com.atguigu.bean.Red;import com.atguigu.condition.MyImportSelector;
    //类中组件统一设置。满足当前条件,这个类中配置的所有bean注册才能生效;
    @Conditional({WindowsCondition.class})
    @Configuration
    @Import({Color.class,Red.class,MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
    //@Import导入组件,id默认是组件的全类名
    public class MainConfig2 {
        /**
         * 给容器中注册组件;
         * 1)、包扫描+组件标注注解(@Controller/@Service/@Repository/@Component)[自己写的类]
         * 2)、@Bean[导入的第三方包里面的组件]
         * 3)、@Import[快速给容器中导入一个组件] 默认引入的bean的名称是类的全路径名
         *         1)、@Import(要导入到容器中的组件);容器中就会自动注册这个组件,id默认是全类名
         *         2)、ImportSelector:返回需要导入的组件的全类名数组;
         *         3)、ImportBeanDefinitionRegistrar:手动注册bean到容器中,可以避免全路径名
         */
    }

    3.自定义ImportSelector

    package com.atguigu.condition;
    
    import org.springframework.context.annotation.ImportSelector;
    import org.springframework.core.type.AnnotationMetadata;
    
    //自定义逻辑返回需要导入的组件
    public class MyImportSelector implements ImportSelector {
    
        //返回值,就是到导入到容器中的组件全类名
        //AnnotationMetadata:当前标注@Import注解的类的所有注解信息
        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            // TODO Auto-generated method stub
            //importingClassMetadata
            //方法不要返回null值
            return new String[]{"com.atguigu.bean.Blue","com.atguigu.bean.Yellow"};
        }
    
    }

    4.自定义ImportBeanDefinitionRegistrar,手动指定bean的名称,避免全路径名

    package com.atguigu.condition;
    
    import org.springframework.beans.factory.support.BeanDefinitionRegistry;
    import org.springframework.beans.factory.support.RootBeanDefinition;
    import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
    import org.springframework.core.type.AnnotationMetadata;
    
    import com.atguigu.bean.RainBow;
    
    public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    
        /**
         * AnnotationMetadata:当前类的注解信息
         * BeanDefinitionRegistry:BeanDefinition注册类;
         *         把所有需要添加到容器中的bean;调用
         *         BeanDefinitionRegistry.registerBeanDefinition手工注册进来
         */
        @Override
        public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
            
            boolean definition = registry.containsBeanDefinition("com.atguigu.bean.Red");
            boolean definition2 = registry.containsBeanDefinition("com.atguigu.bean.Blue");
            if(definition && definition2){
                //指定Bean定义信息;(Bean的类型,Bean。。。)
                RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);
                //注册一个Bean,指定bean名
                registry.registerBeanDefinition("rainBow", beanDefinition);
            }
        }
    
    }

    最正点的详解:

    http://blog.51cto.com/4247649/2118354

  • 相关阅读:
    re模块 与正则表达式之间的关系 一.....
    计算机硬件组成
    随便
    linux crontab定时
    linux shell命令记录
    tomcat意外退出 A valid shutdown command was received via the shutdown port. Stopping the Server instance
    mongodb设置用户
    mongodb的mongo.conf文件 启动参数
    mysql设置不区分表名大小写
    mongodb报Write failed with error code 17280 and error message 'WiredTigerIndex::insert: key too large
  • 原文地址:https://www.cnblogs.com/guchunchao/p/9691965.html
Copyright © 2020-2023  润新知