springboot学习:自动装配

1、@Autowired

自动注入
默认优先按照类型去容器找对应的组件:applicationContext.getBean(Dao.class);
如果找到多个相同类型的组件,再将属性的名称作为组件的id去容器查找
applicationContext.getBean(“dao”);
自动装配默认一定要将属性赋值好,没有就会报错
可以使用@Autowired(required = false)来避免

  • MainConfigOfAutoWired类
@Configuration
@ComponentScan({"com.dav.dao","com.dav.service","com.dav.controller"})
public class MainConfigOfAutoWired {
}
  • IOCTest_Autowired类
public class IOCTest_Autowired {
	@Test
	public void test1() {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfigOfAutoWired.class);
		System.out.println("容器初始化");
		Service service = context.getBean(Service.class);
		System.out.println(service.toString());
		Dao dao = context.getBean(Dao.class);
		System.out.println(dao.toString());
		context.close();
	}
}

在这里插入图片描述

  • Controller 类
public class Controller {
	@Autowired
	private Service service;
}
  • Service 类
@Service
public class Service {
	@Autowired
	private Dao dao;	
}
  • Dao 类
@Repository
public class Dao {
}

2、@Qualifier

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

  • MainConfigOfAutoWired
@Configuration
@ComponentScan({"com.dav.dao","com.dav.service","com.dav.controller"})
public class MainConfigOfAutoWired {
	@Bean("dao2")
	public Dao dao() {
		Dao dao2 = new Dao();
		dao2.setId(2);
		return dao2;
	}
}

在这里插入图片描述

  • Service
public class Service {
	@Qualifier("dao2")
	@Autowired
	private Dao dao;
	public void print() {
		System.out.println(dao);
	}
	@Override
	public String toString() {
		return "Service [dao=" + dao + "]";
	}
}
  • Dao
@Repository
public class Dao {
	private int id = 1;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	@Override
	public String toString() {
		return "Dao [id=" + id + "]";
	}
}

3、@Primary

让spring进行自动装配的时候,默认首选的bean
也可以继续使用@Qualifier注定需要装配的bean

  • Dao
@Repository
@Primary
// 注入时默认该bean
public class Dao {
	private int id = 1;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	@Override
	public String toString() {
		return "Dao [id=" + id + "]";
	}
}
  • Service
@org.springframework.stereotype.Service
public class Service {
	// 指定注入dao2
//	@Qualifier("dao2")
	@Autowired
	private Dao dao;
	public void print() {
		System.out.println(dao);
	}
	@Override
	public String toString() {
		return "Service [dao=" + dao + "]";
	}
}

在这里插入图片描述

4、@Resource(JSR250)和@Inject(JSR330)[java规范注解]

@Resource:和@Autowired一样
@InJect:需要导入Javax.Inject包,和@Autowired一样,没有rquired=false的功能

  • TestDao 类
@Repository
public class TestDao {
}
  • Service 类
public class Service {
	@Inject
	private Dao dao;
	@Resource
	private TestDao testDao;
	public void print() {
		System.out.println(dao);
	}
	@Override
	public String toString() {
		return "Service [dao=" + dao + ", testDao=" + testDao + "]";
	}	
}

在这里插入图片描述

5、@Profile

Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;
@Profile:指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件
1)、加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中,默认是default环境
2)、写在配置类上,只有是指定的环境的时候,整个配置类才能生效
3)、没有标注环境标识的bean在任何环境下都是加载的

  • MainConfigOfPorfile 类
@Configuration
public class MainConfigOfPorfile {
	@Profile("test")
	@Bean("testDataSource")
	public DataSource dataSourceTest () throws PropertyVetoException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setUser("root");
		dataSource.setPassword("liaowei123");
		dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
		dataSource.setDriverClass("com.mysql.jdbc.Driver");
		return dataSource;
	}
	@Profile("dev")
	@Bean("devDataSource")
	public DataSource dataSourceDev () throws PropertyVetoException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setUser("root");
		dataSource.setPassword("liaowei123");
		dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
		dataSource.setDriverClass("com.mysql.jdbc.Driver");
		return dataSource;
	}
	@Profile("uat")
	@Bean("uatDataSource")
	public DataSource dataSourceUat () throws PropertyVetoException {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setUser("root");
		dataSource.setPassword("liaowei123");
		dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
		dataSource.setDriverClass("com.mysql.jdbc.Driver");
		return dataSource;
	}
}
5.1、使用命令行参数
  • IOCTest_Profile 类
@Test
public void test1() {
	AnnotationConfigApplicationContext context = 
			new AnnotationConfigApplicationContext(MainConfigOfPorfile.class);

	System.out.println("容器初始化");	
	String[] names = context.getBeanNamesForType(DataSource.class);
	for(String name : names) {
		System.out.println(name);
	}
	context.close();
}

-Dspring.profiles.active=dev(环境标识)
在这里插入图片描述
在这里插入图片描述

5.2、使用代码激活环境
  • IOCTest_Profile 类
@Test
public void test1() {
	AnnotationConfigApplicationContext context = 
			new AnnotationConfigApplicationContext();
	// 1、创建容器对象
	// 2、设置环境
	// 3、注册主配置类
	// 4、启动刷新容器
	context.getEnvironment().setActiveProfiles("test", "dev");
	context.register(MainConfigOfPorfile.class);
	context.refresh();
	System.out.println("容器初始化");	
	String[] names = context.getBeanNamesForType(DataSource.class);
	for(String name : names) {
		System.out.println(name);
	}
	context.close();
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值