• Spring注解的使用


    @Configuration标注配置类

    告诉Spring这个类是一个配置类,加在类上。

    @ComponentScan组件扫描

    定义包的扫描规则,使用如下:

    package com.atguigu.spring_annotation.config;
    
    import com.atguigu.spring_annotation.pojo.Person;
    import com.atguigu.spring_annotation.service.PerSonService;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.FilterType;
    import org.springframework.stereotype.Controller;
    import org.springframework.stereotype.Repository;
    
    //配置类=配置文件
    @Configuration//告诉Spring这是一个配置类
    /*
        includeFilters:包含哪些组件
        useDefaultFilters =true:会默认把所有包含@Component注解的类都进行扫描
        useDefaultFilters = false:禁用默认行为,只包含才会生效
    
        excludeFilters:不包含哪些组件
        type:按哪种类型进行过滤
        classes:为一个数组,里面为具体的过滤条件实体
     */
    @ComponentScan(value = "com.atguigu.spring_annotation",//要扫描的包
            includeFilters = {
                    //按照注解进行过滤
                    @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Repository.class, Controller.class}),
                    //按照给定的类型进行过滤
                    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {PerSonService.class}),
    
                    //FilterType.ASPECTJ:使用ASPECTJ表达式
                    //FilterType.REGEX:使用正则表达式
    
                    //使用自定义规则,classes中天自定义的规则类,需要实现TypeFilter接口
                    @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})
            },
            useDefaultFilters = false
    )
    public class MainConfig {
        //给容器中注册一个bean,类型为返回值类型,id默认是用方法名作为id,value用于指定id
        @Bean("person")
        public Person person() {
            Person person = new Person();
            person.setAge(20);
            person.setName("李四");
            return person;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    自定义过滤规则,FilterType.CUSTOM:

    package com.atguigu.spring_annotation.config;
    
    import com.atguigu.spring_annotation.pojo.Person;
    import com.atguigu.spring_annotation.service.PerSonService;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.FilterType;
    import org.springframework.stereotype.Controller;
    import org.springframework.stereotype.Repository;
    
    //配置类=配置文件
    @Configuration//告诉Spring这是一个配置类
    /*
        includeFilters:包含哪些组件
        useDefaultFilters =true:会默认把所有包含@Component注解的类都进行扫描
        useDefaultFilters = false:禁用默认行为,只包含才会生效
    
        excludeFilters:不包含哪些组件
        type:按哪种类型进行过滤
        classes:为一个数组,里面为具体的过滤条件实体
     */
    @ComponentScan(value = "com.atguigu.spring_annotation",//要扫描的包
            includeFilters = {
                    //按照注解进行过滤
                    @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Repository.class, Controller.class}),
                    //按照给定的类型进行过滤
                    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {PerSonService.class}),
    
                    //FilterType.ASPECTJ:使用ASPECTJ表达式
                    //FilterType.REGEX:使用正则表达式
    
                    //使用自定义规则,classes中天自定义的规则类,需要实现TypeFilter接口
                    @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})
            },
            useDefaultFilters = false
    )
    public class MainConfig {
        //给容器中注册一个bean,类型为返回值类型,id默认是用方法名作为id,value用于指定id
        @Bean("person")
        public Person person() {
            Person person = new Person();
            person.setAge(20);
            person.setName("李四");
            return person;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    加入IOC容器

    @Bean

    方法在@Configuration中

    //给容器中注册一个bean,类型为返回值类型,id默认是用方法名作为id,value用于指定id
        @Bean("person")
        public Person person() {
            Person person = new Person();
            person.setAge(20);
            person.setName("李四");
            return person;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    @Scope作用域

    调节Ioc容器中的作用域,通常用于控制单例或者多例。

    package com.atguigu.spring_annotation.config;
    
    import com.atguigu.spring_annotation.pojo.Person;
    import org.springframework.beans.factory.config.ConfigurableBeanFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Scope;
    
    @Configuration
    public class MainConfig2 {
        /*
        ConfigurableBeanFactory.SCOPE_PROTOTYPE, prototype:多实例,IOC启动时不会调用此方法,在获取实例时才会钓调用此方法创建对象,
        获取几次就会调用几次。
        ConfigurableBeanFactory.SCOPE_SINGLETON, singleton:单实例(默认值),IOC容器在启动时会调用方法创建实例到IOC容器中,
        每次要用时到IOC容器中拿。
        org.springframework.web.context.WebApplicationContext.SCOPE_REQUEST, request:同一个请求创建一个实例
        org.springframework.web.context.WebApplicationContext.SCOPE_SESSION, session 同一个session创建一个实例
         */
        @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
        @Bean("person")
        public Person person(){
            Person person = new Person();
            person.setAge(19);
            person.setName("jojo");
            return person;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    测试:

    /**
     * 测试Scope注解的作用
     */
    @Test
    public void testScope(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
        Person person1 = context.getBean("person", Person.class);
        Person person2 = context.getBean("person", Person.class);
        /*
        当Scope注解为“prototype”时,结果为false
        当Scope注解为“singleton”时,结果为true
         */
        System.out.println(person1==person2);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    @Lazy懒加载

    针对单实例bean,不随着IOC容器的创建而创建对象,等到第一次用的时候再创建对象。

    @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
    @Bean("person")
    @Lazy
    public Person person(){
        System.out.println("person实例化");
        Person person = new Person();
        person.setAge(19);
        person.setName("jojo");
        return person;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Bean的生命周期

    bean初始化——>初始化——>销毁的过程

    init-method与destroy-method属性

    容器管理生命周期,我们可以自定义初始化和销毁的方法;容器bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法。

    xml配置文件中指定初始化和销毁方法:

    init-method= ““

    destroy-method=“”

    创建对象的时间

    • 单实例在容器启动时创建
    • 多实例在获取对象时创建

    初始化:对象创建完成,并赋值好,调用初始化方法。

    销毁

    • 单实例在容器关闭时销毁
    • 容器不会管理多实例的bean,不会调用销毁方法

    Car:

    package com.atguigu.spring_annotation.pojo;
    
    public class Car {
        public Car() {
            System.out.println("Car constructor......");
        }
    
        public void init(){
            System.out.println("car-->init");
        }
    
        public void destroy(){
            System.out.println("car-->destroy");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    注入IOC容器并标明初始化和销毁方法:

    package com.atguigu.spring_annotation.config;
    
    import com.atguigu.spring_annotation.pojo.Car;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class MainConfigOfLifeCycle {
        @Bean(initMethod = "init",destroyMethod = "destroy")
        public Car car() {
            return new Car();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    测试:

    package com.atguigu.spring_annotation.test;
    
    import com.atguigu.spring_annotation.config.MainConfigOfLifeCycle;
    import org.junit.Test;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class IOCTest_LifeCycle {
        @Test
        public void test01(){
            //以单实例bean为例
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
            //1、Car constructor......
            //2、car-->init
            //3、容器创建完成...
            System.out.println("容器创建完成...");
            //关闭容器
            //4、car-->destroy
            context.close();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    实现InitiallizingBean与DisposableBean接口

    还有另一种方法:通过让Bean实现InitiallizingBean(定义初始化逻辑),以及DisposableBean(定义销毁逻辑)

    Cat:

    package com.atguigu.spring_annotation.pojo;
    
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    
    public class Cat implements InitializingBean, DisposableBean {
        public Cat() {
            System.out.println("cat-->constructor");
        }
    
        @Override
        public void destroy() throws Exception {
            System.out.println("cat-->destroy");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("cat-->init");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    配置类:

    package com.atguigu.spring_annotation.config;
    
    import com.atguigu.spring_annotation.pojo.Car;
    import com.atguigu.spring_annotation.pojo.Cat;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class MainConfigOfLifeCycle {
    
        @Bean
        public Cat cat(){
            return new Cat();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    测试:

     @Test
        public void test02(){
            //以单实例bean为例
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
            //1、cat-->constructor......
            //2、cat-->init
            //3、容器创建完成...
            System.out.println("容器创建完成...");
            //关闭容器
            //4、cat-->destroy
            context.close();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    @PostConstruct与@PreDestroy注解

    @PostConstruct:在bean创建完成并且属性赋值完成;来执行初始化方法。

    @PreDestroy:在容器销毁bean之前通知我们进行清理工作。

    package com.atguigu.spring_annotation.pojo;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    
    public class Dog {
        public Dog() {
            System.out.println("dog-->constructor");
        }
    
        @PostConstruct
        public void init() {
            System.out.println("dog-->init");
        }
    
        @PreDestroy
        public void destroy() {
            System.out.println("dog-->destroy");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    BeanPostProcessor接口:bean的后置处理器

    postProcessBeforeInitialization:在bean初始化之前进行一些处理工作。

    postProcessAfterInitialization:在bean初始化之后进行一些处理工作。

    package com.atguigu.spring_annotation.pojo;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.stereotype.Component;
    
    /**
     * 后置处理器:初始化前后进行处理工作
     * 将后置处理器加入到IOC容器中
     */
    @Component
    public class MyBeanPostProcessor implements BeanPostProcessor {
        @Override
        public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
            System.out.println("postProcessBeforeInitialization..." + s + "=>" + o);
            return o;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
            System.out.println("postProcessAfterInitialization..." + s + "=>" + o);
            return o;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    注意:需要开启组件扫描

    底层原理:遍历得到容器中所有的BeanPostProcessor,挨个执行beforeInitiallization,一旦返回null,跳出for循环,不会执行后面的BeanPostProcessor.beforeInitiallization。

    Spring底层对BeanPostProcessor的使用:bean赋值,注入其他组件,@Autowired、生命周期注解功能,@Async…

    @Controller

    标注为Controller层组件

    package com.atguigu.spring_annotation.controller;
    
    import org.springframework.stereotype.Controller;
    
    @Controller
    public class PersonController {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    @Service

    标注为Service层组件

    package com.atguigu.spring_annotation.service;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class PerSonService {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    @Repository

    标注为Service层组件

    package com.atguigu.spring_annotation.dao;
    
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class PersonDao {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    @Conditional条件判断

    按照一定的条件进行判断,满足条件给容器中注册bean。放在类上面表示满足条件这个类中的所有bean注册才会生效,放在方法上面表示满足条件这个方法的bean注册才会生效。

    创建Bean并使用@Condition注解:

     /**
         * @Condition 按照一定的条件进行判断,满足条件给容器中注册bean
         * 如果系统是windows,给容器注册bill
         * 如果系统是linux,给容器注册linus
         * @return
         */
        @Conditional({WindowsCondition.class})
        @Bean("bill")
        public Person person01() {
            return new Person("Bill Gates", 66);
        }
    
        @Conditional({LinuxCondition.class})
        @Bean("linus")
        public Person person02() {
            return new Person("Linus", 50);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    实现Condition接口:

    package com.atguigu.spring_annotation.condition;
    
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.beans.factory.support.BeanDefinitionRegistry;
    import org.springframework.context.annotation.Condition;
    import org.springframework.context.annotation.ConditionContext;
    import org.springframework.core.env.Environment;
    import org.springframework.core.type.AnnotatedTypeMetadata;
    
    //评判系统是否是windows
    public class WindowsCondition implements Condition {
    
        /**
         * 判断是否是linux系统
         * @param context 判断条件能使用的上下文
         * @param metadata 注释信息
         * @return
         */
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            //能获取到ioc使用的beanFactory
            ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
            //获取类加载器
            ClassLoader classLoader = context.getClassLoader();
            //获取环境信息
            Environment environment = context.getEnvironment();
            //获取到bean定义的注册类
            BeanDefinitionRegistry registry = context.getRegistry();
            String systemName = environment.getProperty("os.name");
            if(systemName.contains("Windows")){
                return true;
            }
            return false;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    package com.atguigu.spring_annotation.condition;
    
    import org.springframework.context.annotation.Condition;
    import org.springframework.context.annotation.ConditionContext;
    import org.springframework.core.env.Environment;
    import org.springframework.core.type.AnnotatedTypeMetadata;
    
    //判断系统是否是linux
    public class LinuxCondition implements Condition {
    
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            Environment environment = context.getEnvironment();
            String systemName = environment.getProperty("os.name");
            if (systemName.contains("Linux")) {
                return true;
            }
            return false;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    测试:

    @Test
    public void test03(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
        String[] names = context.getBeanNamesForType(Person.class);
        for (String s : names){
            System.out.println(s);//bill
        }
    
        //获取系统名字
        ConfigurableEnvironment environment = context.getEnvironment();
        String system = environment.getProperty("os.name");
        System.out.println(system);//Windows 10
    
        Map<String, Person> types = context.getBeansOfType(Person.class);
        System.out.println(types);//bill=Person(name=Bill Gates, age=66)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    组件导入

    @Import

    @Import可以快速给容器中导入一个组件,容器一起动会自动注册这个组件,id默认为全类名。

    第一种用法:

    package com.atguigu.spring_annotation.config;
    
    import com.atguigu.spring_annotation.pojo.Color;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    
    @Import({Color.class})//快速导入组件,id默认为组件全类名,可以写多个
    @Configuration
    public class MainConfig2 {
      
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    ImportSelector

    返回需要导入的组件的全类名数组

    package com.atguigu.spring_annotation.config;
    
    import com.atguigu.spring_annotation.condition.MyImportSelector;
    import com.atguigu.spring_annotation.pojo.Color;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    
    @Import({Color.class, MyImportSelector.class})//快速导入组件,id默认为组件全类名,可以写多个
    @Configuration
    public class MainConfig2 {
       
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    package com.atguigu.spring_annotation.condition;
    
    import org.springframework.context.annotation.ImportSelector;
    import org.springframework.core.type.AnnotationMetadata;
    
    public class MyImportSelector implements ImportSelector {
        /**
         * 自定义逻辑需要返回的组件
         *
         * @param importingClassMetadata 当前标注@Import注解的类的所有注解信息
         * @return 导入到容器中的组件全类名
         */
        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            return new String[]{"com.atguigu.spring_annotation.pojo.Blue", "com.atguigu.spring_annotation.pojo.Yellow"};
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    ImportBeanDefinitionRegistrar

    package com.atguigu.spring_annotation.config;
    
    import com.atguigu.spring_annotation.condition.MyImportBeanDefinitionRegistrar;
    import com.atguigu.spring_annotation.condition.MyImportSelector;
    import com.atguigu.spring_annotation.pojo.Color;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    
    @Import({Color.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})//快速导入组件,id默认为组件全类名,可以写多个
    @Configuration
    public class MainConfig2 {
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    package com.atguigu.spring_annotation.condition;
    
    import com.atguigu.spring_annotation.pojo.Red;
    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;
    
    public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    
        /**
         * 把所有需要添加到容器中的bean:调用BeanDefinitionRegistry.registerBeanDefinition手动注册
         * @param importingClassMetadata 当前类的注解信息
         * @param registry BeanDefinition注册类
         *
         */
        @Override
        public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
            boolean containsRed = registry.containsBeanDefinition("red");
            //如果没有red这个组件
            if(!containsRed){
                RootBeanDefinition red = new RootBeanDefinition(Red.class);
                //自定义组件名字
                registry.registerBeanDefinition("red",red);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    FactoryBean()(工厂Bean)

    实现接口:

    package com.atguigu.spring_annotation.pojo;
    
    import org.springframework.beans.factory.FactoryBean;
    
    public class ColorFactoryBean implements FactoryBean<Color> {
    
        /**
         * @return 返回一个Color对象,这个对象会添加到容器中
         * @throws Exception
         */
        @Override
        public Color getObject() throws Exception {
            return new Color();
        }
    
        /**
         * @return 返回的类型
         */
        @Override
        public Class<?> getObjectType() {
            return Color.class;
        }
    
        /**
         * 控制是否为单例
         *
         * @return 如果返回true,表示是单例,只会在容器中保存一份,返回false表示不是单例
         */
        @Override
        public boolean isSingleton() {
            return true;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    注册Bean:

    package com.atguigu.spring_annotation.config;
    
    import com.atguigu.spring_annotation.pojo.ColorFactoryBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class MainConfig2 {
        @Bean
        public ColorFactoryBean colorFactoryBean(){
            return new ColorFactoryBean();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    测试:

    @Test
    public void testColorFactoryBean(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig2.class);
        Object bean1 = context.getBean("colorFactoryBean");
        //工厂Bean获取的是getObject创建的对象
        System.out.println("bean的类型"+bean1.getClass());//bean的类型class com.atguigu.spring_annotation.pojo.Color
    
        Object bean2 = context.getBean("colorFactoryBean");
        //因为实现FactoryBean接口的方法isSingleton()返回的是true,表示为单例模式,所有为是同一个对象
        System.out.println(bean1 == bean2);//true
    
        //加上&可以获取工厂本身
        Object bean3 = context.getBean("&colorFactoryBean");
        System.out.println(bean3.getClass());//class com.atguigu.spring_annotation.pojo.ColorFactoryBean
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    @Value赋值

    package com.atguigu.spring_annotation.pojo;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.springframework.beans.factory.annotation.Value;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Person {
        /*
        使用@Value赋值:
        1、基本数值
        2、可以写SpEl:#{}
        3、可以使用${}:取出资源文件【properties】中的值(在运行环境变量中的值),
        需要在配置类中使用@PropertySource加载资源文件
         */
        @Value("张三")
        private String name;
        @Value("#{20-2}")
        private Integer age;
        @Value("${person.nikeName}")
        private String NikeName;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    package com.atguigu.spring_annotation.config;
    
    import com.atguigu.spring_annotation.pojo.Person;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    @Configuration
    @PropertySource({"classpath:person.properties"})//导入资源文件
    public class MainConfigOfPropertyValues {
        @Bean
        public Person person(){
            return new Person();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    @PropertySource读取资源文件

    @Configuration
    @PropertySource({"classpath:jdbc.properties"})
    public class MainConfigOfProfile {
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    自动装配

    @Autowired

    这个注解是Spring定义的,可以用在构造器、参数,方法、属性,推荐使用。

    • 标注在方法、构造器、参数,方法、属性都是从容器中获取参数的值,默认不写@Autowired
    • 标注在方法位置:@Bean+方法参数,参数从容器中获取。
    • 标注在构造器的位置:如果组件只有一个有参构造器,这个有参构造器的@Autowired可以省略,参数位置的组件还是可以从容器中获取。
    package com.atguigu.spring_annotation.pojo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    @Component
    public class Boos {
        //会从容器中找到cat对象并注入到形参
        public Boos(@Autowired Cat cat) {//@Autowired 可以省略
            System.out.println(cat);
            System.out.println("Boss-->constructor");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    @Autowired:

    • 默认按照类型去容器中找对应的组件,context.getBean(PersonDao.class);

    • 如果找到多个相同类型的组件,再将属性的名称作为组件的id去容器中找,context.getBean(“personDaoName”);

    • @Qualifier:指定注入组件的名称,而不是属性名。

    没有装配默认一定要将属性赋值好,没有就会报错,但是可以用requied=false属性设置为不必须的。

    package com.atguigu.spring_annotation.service;
    
    import com.atguigu.spring_annotation.dao.PersonDao;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Service;
    
    @Service
    public class PerSonService {
        @Qualifier("personDao2")
        @Autowired(required = false)
        private PersonDao personDao;
    
        public void print() {
            System.out.println(personDao);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    @Primary首选注入的对象

    @Primary加入在Bean上后,在使用@Autowired时默认首选使用这个Bean进行装配。

    package com.atguigu.spring_annotation.dao;
    
    import org.springframework.context.annotation.Primary;
    import org.springframework.stereotype.Repository;
    
    @Primary
    @Repository
    public class PersonDao {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    也可以用在方法上。

    package com.atguigu.spring_annotation.config;
    
    import com.atguigu.spring_annotation.dao.PersonDao;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    
    @Configuration
    @ComponentScan("com.atguigu.spring_annotation")
    public class MainConfigOfAutowired {
        @Primary
        @Bean
        public PersonDao personDao2(){
            return new PersonDao();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    如果使用了@Qualifier明确指定了,那么该注解就失效了。

    @Resource

    这个注解是Java规范的注解,可以和@AutoWired一样实现自动装配功能,默认是按照组件的名称进行装配,可以在注解中填入name=“注解名称”指定要注入的Bean名字,不支持@Primary、require=false功能

    package com.atguigu.spring_annotation.service;
    
    import com.atguigu.spring_annotation.dao.PersonDao;
    import org.springframework.stereotype.Service;
    
    import javax.annotation.Resource;
    
    @Service
    public class PerSonService {
    //    @Autowired(required = false)
        @Resource(name = "personDao2")
        private PersonDao personDao;
    
        public void print() {
            System.out.println(personDao);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    @Inject

    这个注解是Java规范的注解,需要导入Inject的依赖。

    
    <dependency>
        <groupId>javax.injectgroupId>
        <artifactId>javax.injectartifactId>
        <version>1version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    它的功能与@AutoWired一致,没有require=false的功能。

    -Aware注入Spring底层组件

    自定义组件想要使用Spring容器底层的一些组件(ApplicationContext、BeanFactory等)时,需要实现XXXAware,在创建对象的时候,会调用接口规定的方法注入相关组件。

    XXXAware的功能是使用XXXAwareProcessor处理的。

    package com.atguigu.spring_annotation.pojo;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.context.EmbeddedValueResolverAware;
    import org.springframework.stereotype.Component;
    import org.springframework.util.StringValueResolver;
    
    @Component
    public class Red implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware {
        private ApplicationContext applicationContext;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            System.out.println("传入的IOC:" + applicationContext);
            this.applicationContext = applicationContext;
        }
    
        @Override
        public void setBeanName(String s) {
            System.out.println("当前bean的名字" + s);
        }
    
        @Override
        public void setEmbeddedValueResolver(StringValueResolver resolver) {
            String value = resolver.resolveStringValue("你好 ${os.name} ,我是#{20*14}");
            System.out.println("解析的字符串:" + value);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    @Profile配置环境

    @Profile是Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能。

    以下用模拟数据源切换数据源:

    1. 导入两个c3p0和mysql驱动
    
            <dependency>
                <groupId>c3p0groupId>
                <artifactId>c3p0artifactId>
                <version>0.9.1.2version>
            dependency>
            
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>8.0.28version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    1. 使用@Profle注解
    package com.atguigu.spring_annotation.config;
    
    import com.atguigu.spring_annotation.pojo.Yellow;
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.EmbeddedValueResolverAware;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.util.StringValueResolver;
    
    import javax.sql.DataSource;
    import java.beans.PropertyVetoException;
    
    /**
     * @Profile:指定组件在哪个环境下才能被注册到容器中,不指定的情况下,任何情况都能注册这个组件
     * 1、加了环境标识的bean,只有环境被激活的时候才能注册到容器中,默认是default加入容器中
     * 2、写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效
     * 3、没有标注环境表示的bean不受影响,依旧会加载
     */
    @Profile("test")//当develop环境时,因为类标注了“test“,所以下面的@Profile("develop")也是不能加载的
    @Configuration
    @PropertySource({"classpath:jdbc.properties"})
    public class MainConfigOfProfile implements EmbeddedValueResolverAware {
        @Value("${db.user}")
        private String user;
        @Value("${db.driverClass}")
        private String driverClass;
    
        private StringValueResolver valueResolver;
    
        @Profile("test")
        @Bean
        public Yellow yellow(){
            return new Yellow();
        }
    
        //测试的数据源
        @Profile("test")
        @Bean("testDatasource")
        public DataSource testDatasource(@Value("${db.password}") String password) throws PropertyVetoException {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(user);
            dataSource.setPassword(password);
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false");
            dataSource.setDriverClass(driverClass);
            return dataSource;
        }
    
        //开发的数据源
        @Profile("develop")
        @Bean("developDatasource")
        public DataSource developDatasource(@Value("${db.password}") String password) throws PropertyVetoException {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(user);
            dataSource.setPassword(password);
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false");
            dataSource.setDriverClass(driverClass);
            return dataSource;
        }
    
        //部署的数据源
        @Profile("deploy")
        @Bean("deployDatasource")
        public DataSource deployDatasource() throws PropertyVetoException {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(user);
    
            //通过valueResolver解析字符串
            String password = valueResolver.resolveStringValue("${db.password}");
            dataSource.setPassword(password);
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false");
            dataSource.setDriverClass(driverClass);
            return dataSource;
        }
    
        @Override
        public void setEmbeddedValueResolver(StringValueResolver resolver) {
            this.valueResolver = resolver;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    1. 测试
    package com.atguigu.spring_annotation.test;
    
    import com.atguigu.spring_annotation.config.MainConfigOfProfile;
    import com.atguigu.spring_annotation.pojo.Yellow;
    import org.junit.Test;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    import javax.sql.DataSource;
    
    public class TestProfile {
    
    
        @Test
        public void test() {
            //1、创建一个applicationContext
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
            //2、设置需要激活的环境
            context.getEnvironment().setActiveProfiles("test","develop");
            //3、注册主机配置
            context.register(MainConfigOfProfile.class);
            //4、启动刷新容器
            context.refresh();
    
            //根据类型获取bean的名字
            String[] names = context.getBeanNamesForType(DataSource.class);
            for(String s : names){
                System.out.println(s);
                /*
                testDatasource
                developDatasource
                 */
            }
    
            //同样标注了@Profile("test")的Yellow也加入了容器
            Yellow yellow = context.getBean(Yellow.class);
            System.out.println(yellow);//com.atguigu.spring_annotation.pojo.Yellow@1afd44cb
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    【无标题】
    C语言——指针初阶详解
    机器人中的数值优化|【六】线性共轭梯度法,牛顿共轭梯度法
    UG\NX二次开发 一个分割曲线的工具
    若依前端ajax连接后端多文件上传接口,并传值
    SpringCloud-gateway编码实现路由策略的自动刷新,动态路由
    SwiftUI ☞ @State 相关问题
    MySQL 主从同步(读写分离)
    PX4使用P900数传
    Algorithms practice:Basic Calculator 224
  • 原文地址:https://blog.csdn.net/weixin_46245201/article/details/127478280