文章目录
SpringMoreAnnotaiton(一)
- Conditional
Conditional注解
对Bean的创建进行约束,当只有Condition实现类返回true时才能创建
PorscheCar类
public class PorscheCar implements Car{
@Override
public String toString() {
return "PorscheCar{}";
}
}
CarFactory类实现Condition接口
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class CarFactory implements Condition{
//以下作用,当存在CarFactory实例化对象时才能创建@Conditional注解的Bean
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
try {
Object carFactory = beanFactory.getBean("carFactory");
}catch (Exception ex){
return false;
}
return true;
}
}
JavaConfig配置Bean
@Configuration
public class SpringJavaConfig02 {
/*@Bean
public CarFactory carFactory(){
CarFactory carFactory = new CarFactory();
return carFactory;
}*/
@Bean
//配置Condition
@Conditional(CarFactory.class)
public Car porscheCar(){
return new PorscheCar();
}
}
测试类
@Test
public void test06(){
AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(SpringJavaConfig02.class);
System.out.println(acac.getBean("porscheCar", PorscheCar.class));
}
配置文件中放开CarFactoryBean的创建后
Primary、Qualifier注解
解决:一个接口或父类存在多个子类时,Spring不知道自动注入那个到特定对象中
Primary
Primary注解表示Spring应当优先考虑被注解的对象注入对象中
Car接口
public interface Car {
}
Car实现类 PorscheCar、BMWCar
@Component
public class PorscheCar implements Car{
@Override
public String toString() {
return "PorscheCar{}";
}
}
@Component
public class BMWCar implements Car{
@Override
public String toString() {
return "BMWCar{}";
}
}
FourSStores类
@Component
public class FourSStores {
@Autowired
private Car car;
@Override
public String toString() {
return "FourSStores{" +
"car=" + car +
'}';
}
}
扫描注解配置类
@Configuration
@ComponentScan(value = "com.test.springdome02")
public class SpringConfig {
}
测试类
@Test
public void test07(){
AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(SpringConfig.class);
System.out.println(acac.getBean("fourSStores", FourSStores.class));
}
给BMWCar增加Primary注解
@Component
@Primary
public class BMWCar implements Car{}
Qualifier
Qualifier指明应当注入指定对象
请移步spring注解Autowired下Qualifier
Import、ImportResource
当一个Spring配置文件过多时需要拆分,或者多人开发(每个人都有自己的配置的Bean)时使用以上注解
以上注解可以将Spring的多个配置进行组合
多个JavaConfig配置进行组合
BMWCar,PorscheCar两个类
//不存在Component注解,通过在SpringConfig配置中使用@Bean注解创建
public class BMWCar implements Car {
@Override
public String toString() {
return "BMWCar{}";
}
}
@Component
public class PorscheCar implements Car{
@Override
public String toString() {
return "PorscheCar{}";
}
}
SuperSpringConfig、SpringConfig、SpringConfig02
//此配置类用来扫描包中的使用过注解的Bean
@Configuration
@ComponentScan(value = "com.test.springdome02")
public class SpringConfig {
}
//通过@Bean来创建
@Configuration
public class SpringConfig02 {
@Bean
public Car bMWCar(){
return new BMWCar();
}
}
//用来组合两个SpringConfig配置
@Configuration
@Import({SpringConfig.class,SpringConfig02.class})
public class SuperSpringConfig {
}
测试类
@Test
public void test08(){
AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(SuperSpringConfig.class);
System.out.println(acac.getBean("porscheCar", PorscheCar.class));
System.out.println(acac.getBean("bMWCar", BMWCar.class));
}
JavaConfig配置组合XML配置
新增ChineseRedCar
public class ChineseRedCar implements Car{
@Override
public String toString() {
return "ChineseRedCar{}";
}
}
配置Bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
<bean id="chineseRedCar" class="com.test.springdome02.pojo.ChineseRedCar"></bean>
</beans>
SuperSpringConfig中配置
@Configuration
@Import({SpringConfig.class,SpringConfig02.class})
@ImportResource("classpath:applicationcontext.xml")
public class SuperSpringConfig {
}
测试类增加
System.out.println(acac.getBean("chineseRedCar", ChineseRedCar.class));
多个XML配置进行组合
新增:XiaoPengCar
public class XiaoPengCar implements Car {
@Override
public String toString() {
return "XiaoPengCar{}";
}
}
新增:两个xml配置 applicationcontext01.xml、superapplicationcontext.xml
//applicationcontext01.xml,新增xiaoPengCar Bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="xiaoPengCar" class="com.test.springdome02.pojo.XiaoPengCar"></bean>
</beans>
//新增组合类superapplicationcontext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!--<context:component-scan base-package="com.test.springdome02"></context:component-scan>-->
//导入applicationcontext.xml,applicationcontext01.xml
<import resource="applicationcontext.xml"></import>
<import resource="applicationcontext01.xml"></import>
</beans>
测试类
@Test
public void test09(){
ClassPathXmlApplicationContext cac = new ClassPathXmlApplicationContext("superapplicationcontext.xml");
System.out.println(cac.getBean("chineseRedCar", ChineseRedCar.class));
System.out.println(cac.getBean("xiaoPengCar", XiaoPengCar.class));
}
XML配置组合JavaConfig配置
superapplicationcontext.xml新增配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="com.test.springdome02"></context:component-scan>
//导入JavaConfig配置
<bean class="com.test.springdome02.config.SpringConfig02"/>
//导入XML配置
<import resource="applicationcontext.xml"></import>
<import resource="applicationcontext01.xml"></import>
</beans>
测试类
@Test
public void test10(){
ClassPathXmlApplicationContext cac = new ClassPathXmlApplicationContext("superapplicationcontext.xml");
System.out.println(cac.getBean("chineseRedCar", ChineseRedCar.class));
System.out.println(cac.getBean("xiaoPengCar", XiaoPengCar.class));
System.out.println(cac.getBean("bMWCar", BMWCar.class));
System.out.println(cac.getBean("chineseRedCar", ChineseRedCar.class));
}