zoukankan      html  css  js  c++  java
  • Spring注解驱动开发(三)-----自动装配

    自动装配

    概念

    Spring利用依赖注入(DI),完成对IOC容器中中各个组件的依赖关系赋值。

    @Autowired-----自动注入

    1、默认优先按照类型去容器中找对应的组件

    applicationContext.getBean(BookDao.class);

    找到就赋值

    2、如果找到多个相同类型的组件,再将属性的名称作为组件的id去容器中查找

    applicationContext.getBean("bookDao");

    3、@Qualifier("bookDao")

    使用@Qualifier指定需要装配的组件的id,而不是使用属性名。

    4、自动装配默认一定要将属性赋值好,没有就会报错

    可以使用@Autowired(required=false);

    5、@Primary

    让Spring进行自动装配的时候,默认使用首选的bean;也可以继续使用@Qualifier指定需要装配的bean的名字。

    注入示例:

    BookService{
        @Autowired
        BookDao bookDao;
    }

    @Resource & @Inject-----Spring支持使用@Resource(JSR250)和@Inject(JSR330)[java规范的注解]

    @Resource

    可以和@Autowired一样实现自动装配功能;默认是按照组件名称进行装配的;没有能支持@Primary功能没有支持@Autowired(reqiured=false);

    @Inject

    需要导入javax.inject的包,和Autowired的功能一样。没有required=false的功能;

    注:@Autowired:Spring定义的; @Resource、@Inject都是java规范

    package com.atguigu.service;
    
    
    import javax.annotation.Resource;
    import javax.inject.Inject;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Service;
    
    import com.atguigu.dao.BookDao;
    
    
    @Service
    public class BookService {
        
        //@Qualifier("bookDao")
        //@Autowired(required=false)
        //@Resource(name="bookDao2")
        @Inject
        private BookDao bookDao;
        
        public void print(){
            System.out.println(bookDao);
        }
    
        @Override
        public String toString() {
            return "BookService [bookDao=" + bookDao + "]";
        }
    }

    原理:AutowiredAnnotationBeanPostProcessor:解析完成自动装配功能;

    方法、构造器位置的自动装配-----@Autowired

    [标注在方法位置]-----@Bean+方法参数;参数从容器中获取;默认不写@Autowired效果是一样的;都能自动装配

    package com.atguigu.bean;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    //默认加在ioc容器中的组件,容器启动会调用无参构造器创建对象,再进行初始化赋值等操作
    @Component
    public class Boss {
            
        private Car car;
     public Car getCar() {
            return car;
        }
    
        @Autowired 
        //标注在方法,Spring容器创建当前对象,就会调用方法,完成赋值;
        //方法使用的参数,自定义类型的值从ioc容器中获取
        public void setCar(Car car) {
            this.car = car;
        }
    
        @Override
        public String toString() {
            return "Boss [car=" + car + "]";
        }
    }

    [标在构造器上]-----如果组件只有一个有参构造器,这个有参构造器的@Autowired可以省略,参数位置的组件还是可以自动从容器中获取

    package com.atguigu.bean;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    //默认加在ioc容器中的组件,容器启动会调用无参构造器创建对象,再进行初始化赋值等操作
    @Component
    public class Boss {
            
        private Car car;
             
        //构造器要用的组件,都是从容器中获取
        @Autowired
        public Boss(Car car){
            this.car = car;
            System.out.println("Boss...有参构造器");
        }
        
        public Car getCar() {
            return car;
        }
    
        public void setCar(Car car) {
            this.car = car;
        }
    
        @Override
        public String toString() {
            return "Boss [car=" + car + "]";
        }
    }

    Aware注入spring底层组件以及原理 

    自定义组件想要使用Spring容器底层的一些组件(ApplicationContext,BeanFactory,xxx);自定义组件实现xxxAware;在创建对象的时候,会调用接口规定的方法注入相关组件;Aware;把Spring底层一些组件注入到自定义的Bean中; 

    规则:xxxAware:功能使用xxxProcessor

      ApplicationContextAware==》ApplicationContextAwareProcessor;

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

    正常的企业级项目一般都会有开发环境、测试环境、生产环境;数据源:(/A)(/B)(/C);

    @Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件

    示例:不同环境使用不同数据源,不用更改代码-----数据源使用c3p0、mysql数据库

    1、dbconfig.properties

    db.user=root
    db.password=123456
    db.driverClass=com.mysql.jdbc.Driver

    2、MainConfigOfProfile

    package com.atguigu.config;
    
    
    import javax.sql.DataSource;
    
    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 com.atguigu.bean.Yellow;
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    /**
     * @Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件
     * 
     * 1)、加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境
     * 2)、写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效
     * 3)、没有标注环境标识的bean在,任何环境下都是加载的;
     */
    @PropertySource("classpath:/dbconfig.properties")
    @Configuration
    public class MainConfigOfProfile implements EmbeddedValueResolverAware{
        
        @Value("${db.user}")
        private String user;
        
        private StringValueResolver valueResolver;
        
        private String  driverClass;
            
        //这个bean没有环境标识,因此任何环境下都是加载的;如果设置了环境,那么只有在对应环境下才会被加载    
        @Bean
        public Yellow yellow(){
            return new Yellow();
        }
        
        @Profile("test")
        @Bean("testDataSource")
        public DataSource dataSourceTest(@Value("${db.password}")String pwd) throws Exception{
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(user);
            dataSource.setPassword(pwd);
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
            dataSource.setDriverClass(driverClass);
            return dataSource;
        }
            
        @Profile("dev")
        @Bean("devDataSource")
        public DataSource dataSourceDev(@Value("${db.password}")String pwd) throws Exception{
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(user);
            dataSource.setPassword(pwd);
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/ssm_crud");
            dataSource.setDriverClass(driverClass);
            return dataSource;
        }
        
        @Profile("prod")
        @Bean("prodDataSource")
        public DataSource dataSourceProd(@Value("${db.password}")String pwd) throws Exception{
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            dataSource.setUser(user);
            dataSource.setPassword(pwd);
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/scw_0515");        
            dataSource.setDriverClass(driverClass);
            return dataSource;
        }
    
        //这个是spring提供的值解析器,通过StringValueResolver可以直接从配置文件中解析${db.driverClass}
        @Override
        public void setEmbeddedValueResolver(StringValueResolver resolver) {
            this.valueResolver = resolver;
            driverClass = valueResolver.resolveStringValue("${db.driverClass}");
        }
    
    }

    3、测试类-----IOCTest_Profile

    package com.atguigu.test;
    
    import javax.sql.DataSource;
    
    import org.junit.Test;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    import com.atguigu.bean.Boss;
    import com.atguigu.bean.Car;
    import com.atguigu.bean.Color;
    import com.atguigu.bean.Red;
    import com.atguigu.bean.Yellow;
    import com.atguigu.config.MainConfigOfProfile;
    import com.atguigu.config.MainConifgOfAutowired;
    import com.atguigu.dao.BookDao;
    import com.atguigu.service.BookService;
    
    public class IOCTest_Profile {
        
        //1、使用命令行动态参数: 在虚拟机参数位置加载 -Dspring.profiles.active=test
        //2、代码的方式激活某种环境;
        @Test
        public void test01(){
            AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
            //1、创建一个applicationContext
            //2、设置需要激活的环境
            applicationContext.getEnvironment().setActiveProfiles("dev");
            //3、注册主配置类
            applicationContext.register(MainConfigOfProfile.class);
            //4、启动刷新容器
            applicationContext.refresh();
        }
    
    }

    注:

    1、类上的@Profile大于方法中的@Profile;-----如果当前环境为"dev",而配置类上@Profile为test,那么相当于整个类作废。

    2、如何更改运行环境?-----a、直接通过-D spring.profiles.active=dev   b、通过applicaitonContext去设置

  • 相关阅读:
    安卓第一夜 第一个应用
    为什么要学习Linux
    Android的历史与花边
    来玩Play框架07 静态文件
    来玩Play框架06 用户验证
    来玩Play框架05 数据库
    来玩Play框架04 表单
    来玩Play框架03 模板
    来玩Play框架02 响应
    来玩Play框架01 简介
  • 原文地址:https://www.cnblogs.com/alimayun/p/11105329.html
Copyright © 2011-2022 走看看