@Autowired
(spring的依赖注入注解) 1.默认优先按照类型去IOC容器中获取对应的组件
application.getBean(Apple.class);
2.如果找到多个相同类型的组件,默认再将属性名作为组件的id,去IOC容器中查找;
@Autowired @Qualifier("apple") Apple apple;
//首先组件上使用@Primary 在配置类中的默认首选该类型的组件 //当在其他bean中装配apple时,首选使用@Primary注解配置的bean组件 @Configuration public class MySpringIocConfig{ @Bean @Primary public Apple apple(){ return new Apple(); } @Bean public Apple apple(){ return new Apple(); } }
4.@Autowired还能够在方法,构造器,参数等处使用
@Resource @Inject
(JSRjava标准注解) @Resource (JSR250)
功能和@Autowired相似,默认按照组件名称进行装配;
不支持@Primary的功能,不支持@Autowired(required=true)
@Inject (JSR330)
需要导入相关依赖 javax.inject;
不支持@Autowired(required=true)
自定义组件想要使用spring底层的组件,如ApplicationContext,BeanFactory,XXX等
自定义组件,只需要实现xxxAware接口
在创建bean的时候,会自动调用接口的相关方法,注入相关组件;类似与回调函数
@Component public class PojoApplicationContextAwareImpl implements ApplicationContextAware,EnvironmentAware,EmbeddedValueResolverAware { // 获取全局信息 private ApplicationContext applicationContext; // 获取运行时变量信息 private Environment environment; // 获取值解析器 获取配置文件信息 private StringValueResolver valueResolver; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext=applicationContext; } @Override public void setEnvironment(Environment environment) { this.environment=environment; } @Override public void setEmbeddedValueResolver(StringValueResolver resolver) { this.valueResolver=resolver; } }
编写配置类and配置文件
//配置类 @Configuration @PropertySource("classpath:/db.properties") public class MySpringConfig { @Value("${db.username}") private String username; @Value("${db.password}") private String password; @Value("${db.driverClass}") private String driverClass; @Profile("dev") @Bean("devDataSource") public DataSource dataSourceDev(){ HikariDataSource hikariDataSource = new HikariDataSource(); hikariDataSource.setUsername(username); hikariDataSource.setPassword(password); hikariDataSource.setDriverClassName(driverClass); hikariDataSource.setJdbcUrl("jdbc:mysql:///devDb?useUnicode=true&characterEncoding=UTF-8"); return hikariDataSource; } @Profile("test") @Bean("testDatasource") public DataSource dataSourceTest(){ HikariDataSource hikariDataSource = new HikariDataSource(); hikariDataSource.setUsername(username); hikariDataSource.setPassword(password); hikariDataSource.setDriverClassName(driverClass); hikariDataSource.setJdbcUrl("jdbc:mysql:///testDb?useUnicode=true&characterEncoding=UTF-8"); return hikariDataSource; } @Profile("prod") @Bean("prodDatasource") public DataSource dataSourceProd(){ HikariDataSource hikariDataSource = new HikariDataSource(); hikariDataSource.setUsername(username); hikariDataSource.setPassword(password); hikariDataSource.setDriverClassName(driverClass); hikariDataSource.setJdbcUrl("jdbc:mysql:///prodDb?useUnicode=true&characterEncoding=UTF-8"); return hikariDataSource; } } //配置文件db.properties db.username=root db.password=root db.driverClass=com.mysql.cj.jdbc.Driver
编写测试类
@Test public void test(){ AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext(); //添加活动的profile applicationContext.getEnvironment().setActiveProfiles("dev"); applicationContext.register(MySpringConfig.class); applicationContext.refresh(); String[] beanNamesForType = applicationContext.getBeanNamesForType(DataSource.class); for (String a:beanNamesForType) { System.out.println(a); } }