自动装配:
spring利用依赖注入和DI完成对IOC容器中各个组件的依赖关系赋值。自动装配的优点有:
  • 自动装配可以大大地减少属性和构造器参数的指派。
  • 自动装配也可以在解析对象时更新配置。

自动装配的方式有很多,其中包含spring的注解以及java自带的注解下面来看一看这些自动装配方式的区别

1.@Autowired(Spring规范)

@Autowired 在Spring2.5引入,可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。
无需再通过传统的在bean的xml文件中进行bean的注入配置。而是使用注解,系统自动为你注入,即隐式配置。@Autowired是根据类型进行标注的,如需要按照名称进行装配,则需要配合@Qualifier使用

1).默认优先按照类型去容器中找对应的组件annotationConfigApplicationContext.getBean(BookDao.class),找到就赋值

2).若有多个相同类型的组件,再将属性名称作为组件的id去容器中查找

3).使用@Qualifier("bookDao")来指定需要装配的组件id而不是根据属性

4).自动装配,默认一定要属性赋值好,否则会报错,使用@Autowired(required=false)可以避免报错

5).@Primary("bookDao2")让Spring进行自动装配时,在没有明确用@Qualifier指定的情况下默认使用优先首选的bean

默认规则(不用写Autowired都能实现自动装配):

1.@Autowired 可以标注在方法,有参构造,参数,spring容器创建当前对象,就会调用该方法,完成参数赋值,需要的参数从容器中获取,完成自动装配

2.@Bean 标注的方法创建对象的时候,方法参数的值从容器中获取

2.@Resource(JSR规范)

可以和@Autowired一样实现自动装配,默认按照组件名称进行装配,没有支持@Qualifier和@Primary的功能

3.@Inject(JSR规范)

可以和@Autowired一样实现自动装配,使用时需要导入javax.inject依赖,可以支持@Qualifier和@Primary的功能,不支持require=false

 4.Awar注入Spring底层组件&原理

自定义组件要使用spring底层的一些组件(ApplicationContext,BeanFactory),需要实现xxxAware

在创建对象的时候,会调用接口规定的方法注入相关组件:Aware,把Spring底层一些组件注入到自定义的bean中

xxxAware,功能使用xxxProcessor(后置处理器)完成 ApplicationContextAware=>ApplicationContextProcessor

@Component
public class Green implements ApplicationContextAware,BeanNameAware,EmbeddedValueResolverAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("applicationContext:"+applicationContext);
this.applicationContext = applicationContext;
} @Override
public void setBeanName(String s) { //获取当前bean的名称
System.out.println("当前bean的名字"+s);
} @Override
public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) { //解析字符串的方法
String s = stringValueResolver.resolveStringValue("你好${os.name} 我是#{10*19}");
System.out.println("解析的字符串:"+s); }
}

@Profile的使用

Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能,实际开发中,分为开发环境,测试环境,生产环境,@Profile 可以指定组件在哪个环境下才能被注册到容器中,加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中,默认是default。

切换环境:

1.使用命令行动态参数:在虚拟机参数位置加载-Dspring.profiles.active=test

2.使用无参构造器切换环境

@Profile("test")
@PropertySource("classpath:dbconfgig.properties")
@Configuration
public class ProfileConfig implements EmbeddedValueResolverAware {
@Value("${db.user}")
private String username; private StringValueResolver valueResolver;//值解析器
@Profile("test")
@Bean("testDataSource")
public DataSource dataSourceTest(@Value("${db.password}") String password) throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser(username); //属性中直接获取配置文件中的值 comboPooledDataSource.setPassword(password); //参数中获取配置文件的值
comboPooledDataSource.setUser("jdbc:mysql://localhost:3306/test");
String driverClass = valueResolver.resolveStringValue("${db.driverClass}"); //使用值解析器获取配置文件中的值
comboPooledDataSource.setDriverClass(driverClass);
return comboPooledDataSource;
}
@Profile("product")
@Bean("proDataSource")
public DataSource dataSourceProduct(@Value("${db.password}") String password) throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser(username);
comboPooledDataSource.setPassword(password);
comboPooledDataSource.setUser("jdbc:mysql://localhost:3306/product");
String driverClass = valueResolver.resolveStringValue("${db.driverClass}");
comboPooledDataSource.setDriverClass(driverClass);
return comboPooledDataSource;
}
@Profile("dev")
@Bean("devDataSource")
public DataSource dataSourceDevelop(@Value("${db.password}") String password) throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser(username);
comboPooledDataSource.setPassword(password);
comboPooledDataSource.setUser("jdbc:mysql://localhost:3306/develop");
String driverClass = valueResolver.resolveStringValue("${db.driverClass}");
comboPooledDataSource.setDriverClass(driverClass);
return comboPooledDataSource;
} @Profile("test")
@Bean
public Yellow yellow(){
return new Yellow();
}
@Override
public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
this.valueResolver = stringValueResolver;
}
}
  @Test
public void test02(){
//1.创建一个application无参构造(不能使用有参构造,要自己写)
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//2.设置需要激活的环境
applicationContext.getEnvironment().setActiveProfiles("test","dev");
//3.注册主配置类
applicationContext.register(ProfileConfig.class);
//4.启动刷新容器
applicationContext.refresh(); String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class);
for (String bean: beanNamesForType){
System.out.println(bean);
} Yellow bean = applicationContext.getBean(Yellow.class);
System.out.println(bean);
}

IOC总结

1.组件添加: 重点掌握@Condition以及@Import注解

Spring注解开发系列Ⅴ --- 自动装配&Profile的更多相关文章

  1. Spring注解开发系列专栏

    这个系列主要是讲Spring注解的使用,可以为后面SpringBoot的学习带来一定的帮助.我觉得从Spring直接过度到SpringBoot还是有点快,还是得需要一个演变的过程.从Spring开发, ...

  2. Spring注解开发系列Ⅵ --- AOP&事务

    注解开发 --- AOP AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,横向重复,纵向抽取.详细的AO ...

  3. Spring注解开发系列VIII --- SpringMVC

    SpringMVC是三层架构中的控制层部分,有过JavaWEB开发经验的同学一定很熟悉它的使用了.这边有我之前整理的SpringMVC相关的链接: 1.SpringMVC入门 2.SpringMVC进 ...

  4. Spring注解开发系列VII --- Servlet3.0

    Servlet3.0简介 Servlet 3.0 作为 Java EE 6 规范体系中一员,随着 Java EE 6 规范一起发布.该版本在前一版本(Servlet 2.5)的基础上提供了若干新特性用 ...

  5. Spring注解开发系列Ⅰ--- 组件注册(上)

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  6. Spring注解开发系列Ⅱ --- 组件注册(下)

    1.@Import注册组件 @Import主要功能是通过导入的方式实现把实例加入springIOC容器中, /** * 给容器注册组件 * 1.包扫描+组件标注注解(@Controller,@Serv ...

  7. Spring注解开发系列Ⅲ --- 生命周期

    Bean的生命周期 Spring Bean 的生命周期在整个 Spring 中占有很重要的位置,掌握这些可以加深对 Spring 的理解. 首先看下生命周期图: 再谈生命周期之前有一点需要先明确: S ...

  8. Spring注解开发系列Ⅳ --- 属性赋值

    在Spring框架中,属性的注入我们有多种方式,我们可以通过构造方法注入,可以通过set方法注入,也可以通过p名称空间注入,方式多种多样,对于复杂的数据类型比如对象.数组.List集合.map集合.P ...

  9. Spring注解开发系列Ⅸ --- 异步请求

    一. Servlet中的异步请求 在Servlet 3.0之前,Servlet采用Thread-Per-Request的方式处理请求,即每一次Http请求都由某一个线程从头到尾负责处理.如果要处理一些 ...

随机推荐

  1. mysql主从同步--读写分离。

    1.mysql 安装参考 https://www.cnblogs.com/ttzzyy/p/9063737.html 2. 主mysql,从mysql 指定配置文件启动 mysqld --defaul ...

  2. JAVA字节码文件之结构

    开发工具:IEDA.JDK1.8.WinHex 一.字节码文件结构 源代码 package com.jalja.java.bytecode; /** * @Auther: XL * @Date: 20 ...

  3. 「算法竞赛进阶指南」0x01 最短Hamilton路径 解题报告

    题目在这里啊题目在这里~ Hamilton路径:将所有点都遍历刚好一次的路径 思路: 数据范围比较小(1~20),所以我们可以考虑暴力中的枚举 数组f[i][j]​ i的二进制表示选取了哪些点 j表示 ...

  4. PPP协议 PAP认证

       

  5. ipaclient 4.5.4 requires jinja2, which is not installed. rtslib-fb 2.1.63 has requirement pyudev>=0.16.1, but you'll have pyudev 0.15 which is incompatible. ipapython 4.5.4 has requirement dnspython>=

  6. 解决阿里云专有网络ftp无法远程链接

    配置好ftp后本机测试可用但无法远程连接 网络上找了很多方法,配置防火墙出入站规则均无效 提交阿里云工单,给出解决方法,测试后可用

  7. .Net 面试题整理(一)

    1.C# 的三大特性? 封装.继承.多态 2.简述 private. protected. public. internal 修饰符的访问权限. private : 私有成员, 在类的内部才可以访问. ...

  8. MySQL插入操作

    说明:value的值可以为数据,DEFAULT,NULL,expr 含有ATUO_INCREMENT的列可以插入DEFAULT.NULL,或者不插入记录来实现自动增长. 插入记录的三种方法:①可以同时 ...

  9. cogs 247. 售票系统 线段树

    247. 售票系统 ★★☆   输入文件:railway.in   输出文件:railway.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 某次列车途经C个城市,城市 ...

  10. cogs 186. [USACO Oct08] 牧场旅行 树链剖分 LCA

    186. [USACO Oct08] 牧场旅行 ★★☆   输入文件:pwalk.in   输出文件:pwalk.out   逐字节对比时间限制:1 s   内存限制:128 MB n个被自然地编号为 ...