Spring利用依赖注入(DI)完成对IOC容器中各组件的依赖关系赋值
1. @Autowired:自动注入,主要由AutowiredAnnotationBeanPostProcessor 解析完成自动装配功能
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的名字;
2. @Autowired : 可标注在构造器、参数、方法、属性,都是从容器中获取参数组件的值进行装配
1)[标注在方法位置]:@Bean+方法参数;参数从容器中获取;默认不写@Autowired效果是一样的;都能自动装配
2)[标在构造器上]:如果组件只有一个有参构造器,这个有参构造器的@Autowired可以省略,参数位置的组件还是可以自动从容器中获取
3)放在参数位置:
3. Spring 还支持使用 @Resource(JSR250) 和 @Inject(JSR330)【java规范的注解】
@Resource:
可以和@Autowired一样实现自动装配功能;默认是按照组件名称进行装配的;
没有能支持@Primary功能没有支持@Autowired(reqiured=false);
@Inject:
需要导入javax.inject的包,和Autowired的功能一样。没有required=false的功能;
@Autowired: 是Spring定义的; @Resource @Inject 都是java规范
4. 自定义组件想要使用Spring容器底层的一些组件(ApplicationContext,BeanFactory,xxx)
自定义组件实现xxxAware;在创建对象的时候,会调用接口规定的方法注入相关组件,重点 Aware 接口;
把Spring底层一些组件注入到自定义的Bean中;
xxxAware:功能使用xxxAwareProcessor 实现;
ApplicationContextAware ==> ApplicationContextAwareProcessor;
demo代码如下:
@Configuration
@ComponentScan({"com.atguigu.service", "com.atguigu.dao", "com.atguigu.controller", "com.atguigu.bean"})
public class MainConifgOfAutowired {
@Primary
@Bean("bookDao2")
public BookDao bookDao() {
BookDao bookDao = new BookDao();
bookDao.setLable("2");
return bookDao;
}
/**
* @param car
* @return
* @Bean标注的方法创建对象的时候,方法参数的值从容器中获取
*/
@Bean
// 一般此处 @Autowired 可省略
public Color color(@Autowired Car car) {
Color color = new Color();
color.setCar(car);
return color;
}
}
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 name) {
System.out.println("当前bean的名字:" + name);
}
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
String resolveStringValue = resolver.resolveStringValue("你好 ${os.name} 我是 #{20*18}");
System.out.println("解析的字符串:" + resolveStringValue);
}
}
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 + "]";
}
}
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class Car implements ApplicationContextAware, InitializingBean, DisposableBean {
private ApplicationContext applicationContext;
public Car() {
System.out.println("car constructor...");
}
public void init() {
System.out.println("car ... init...");
}
public void detory() {
System.out.println("car ... detory...");
}
@PostConstruct
public void postConstruct() {
System.out.println("car ... @PostConstruct...");
}
@PreDestroy
public void preDestroy() {
System.out.println("car ... @PreDestroy...");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void destroy() throws Exception {
System.out.println("car ... destroy()...");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("car ... afterPropertiesSet()...");
}
}
// 默认加在ioc容器中的组件,容器启动会调用无参构造器创建对象,再进行初始化赋值等操作
@Component
public class Boss {
private Car car;
// 构造器要用的组件,都是从容器中获取
public Boss(Car car) {
this.car = car;
System.out.println("Boss...有参构造器");
}
public Car getCar() {
return car;
}
// @Autowired
// 标注在方法,Spring容器创建当前对象,就会调用方法,完成赋值;
// 方法使用的参数,自定义类型的值从ioc容器中获取
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "Boss [car=" + car + "]";
}
}
@Repository
public class BookDao {
private String lable = "1";
public String getLable() {
return lable;
}
public void setLable(String lable) {
this.lable = lable;
}
@Override
public String toString() {
return "BookDao [lable=" + lable + "]";
}
}
public class IOCTest_Autowired {
@Test
public void test01() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConifgOfAutowired.class);
BookService bookService = applicationContext.getBean(BookService.class);
System.out.println(bookService);
//BookDao bean = applicationContext.getBean(BookDao.class);
//System.out.println(bean);
Boss boss = applicationContext.getBean(Boss.class);
System.out.println(boss);
Car car = applicationContext.getBean(Car.class);
System.out.println(car);
Color color = applicationContext.getBean(Color.class);
System.out.println(color);
System.out.println(applicationContext);
applicationContext.close();
}
}
5. Profile 根据环境注册 bean,Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能
@Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件
1)加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中。默认是default环境
2)写在配置类上,只有是指定的环境的时候,整个配置类里面的所有配置才能开始生效
3)没有标注环境标识的bean在,任何环境下都是加载的;
一般在 开发环境、测试环境、生产环境 分别有三个不同的数据源 dev、stg、prd,此时就能够很好的使用这个功能
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;
@PropertySource("classpath:/dbconfig.properties")
@Configuration
public class MainConfigOfProfile implements EmbeddedValueResolverAware {
@Value("${db.user}")
private String user;
private StringValueResolver valueResolver;
private String driverClass;
@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;
}
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.valueResolver = resolver;
driverClass = valueResolver.resolveStringValue("${db.driverClass}");
}
}
dbconfig.properties
db.user=root
db.password=123456
db.driverClass=com.mysql.jdbc.Driver
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();
String[] namesForType = applicationContext.getBeanNamesForType(DataSource.class);
for (String string : namesForType) {
System.out.println(string);
}
Yellow bean = applicationContext.getBean(Yellow.class);
System.out.println(bean);
applicationContext.close();
}
}