Spring注解驱动开发之十——@Resource、@Inject 自动注入、Aware接口、@Profile环境配置
本文包含以下内容:
@Resource、@Inject 自动注入
@Autowired标注方法、构造器、参数
实现Aware 获得Spring 的底层组件
通过@Profile配置不同的环境
1.@Resource、@Inject 自动注入
@Resourceprivate BookDao bookDao;
@Resource(name="bookDao2")private BookDao bookDao;
<!-- https://mvnrepository.com/artifact/javax.inject/javax.inject --><dependency><groupId>javax.inject</groupId><artifactId>javax.inject</artifactId><version>1</version></dependency>
@Injectprivate BookDao bookDao;
("bookDao2")public BookDao bookDao(){BookDao bookDao = new BookDao();bookDao.setLable("2");return bookDao;}
2.@Autowired标注方法、构造器、参数
@Componentpublic class Car {public Car(){System.out.println("car constructor...");}public void init(){System.out.println("car ... init...");}public void detory(){System.out.println("car ... detory...");}}
//默认加在ioc容器中的组件,容器启动会调用无参构造器创建对象,再进行初始化赋值等操作public class Boss {private Car car;}
@Autowiredpublic void setCar(Car car) {this.car = car;}
Boss boss = applicationContext.getBean(Boss.class);System.out.println(boss);Car car = applicationContext.getBean(Car.class);System.out.println(car);
public Boss(Car car){this.car = car;System.out.println("Boss...有参构造器");}
public Boss( Car car){this.car = car;System.out.println("Boss...有参构造器");}
public Boss(Car car){this.car = car;System.out.println("Boss...有参构造器");}
public Color color(Car car){Color color = new Color();color.setCar(car);return color;}
public Color color( Car car){Color color = new Color();color.setCar(car);return color;}
Color color = applicationContext.getBean(Color.class);System.out.println(color);System.out.println(applicationContext);applicationContext.close();
3.实现Aware 获得Spring 的底层组件
Marker superinterface indicating that a bean is eligible to be notified by the Spring container of a particular framework object through a callback-style method.
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {// TODO Auto-generated method stubSystem.out.println("传入的ioc:"+applicationContext);this.applicationContext = applicationContext;}
@Overridepublic void setBeanName(String name) {// TODO Auto-generated method stubSystem.out.println("当前bean的名字:"+name);}
@Overridepublic void setEmbeddedValueResolver(StringValueResolver resolver) {// TODO Auto-generated method stubString resolveStringValue = resolver.resolveStringValue("你好 ${os.name} 我是 #{20*18}");System.out.println("解析的字符串:"+resolveStringValue);}
4.通过@Profile配置不同的环境
<dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.44</version></dependency>
db.user=rootdb.password=123456db.driverClass=com.mysql.jdbc.Driver
("classpath:/dbconfig.properties")public class MainConfigOfProfile implements EmbeddedValueResolverAware{("${db.user}")private String user;private String driverClass;public void setEmbeddedValueResolver(StringValueResolver resolver) {// TODO Auto-generated method stubdriverClass = resolver.resolveStringValue("${db.driverClass}");}}
public DataSource dataSourceTest(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;}public DataSource dataSourceDev(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;}public DataSource dataSourceProd(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;}
public class IOCTest_Profile {//1、使用命令行动态参数: 在虚拟机参数位置加载 -Dspring.profiles.active=test//2、代码的方式激活某种环境;@Testpublic void test01(){AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfProfile.class);String[] namesForType = applicationContext.getBeanNamesForType(DataSource.class);for (String string : namesForType) {System.out.println(string);}}}
-Dspring.profiles.active=test
AnnotationConfigApplicationContext applicationContext =new AnnotationConfigApplicationContext();//1、创建一个applicationContext//2、设置需要激活的环境applicationContext.getEnvironment().setActiveProfiles("dev");//3、注册主配置类applicationContext.register(MainConfigOfProfile.class);//4、启动刷新容器applicationContext.refresh();
-END-
可以关注我的公众号,免费获取价值1980元学习资料
点击“在看”,学多少都不会忘~
本文分享自微信公众号 - 阿聪的全栈之路(gh_ffab7c84fb0c)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。













