@Import是Spring框架提供的注解org.springframework.context.annotation.Import,可以通过条件配置,选择性的注入哪些Bean到Spring IOC容器中;
一 @Import注Bean到Spring容器
直接使用@Import注解将Bean对象注入到容器
java">public class OrderChoice {public void printOrderMsg() {System.out.println("One订单信息是: order001");}
}@Configuration
@Import({OrderChoice.class})
public class CommonConfig {}@SpringBootTest
@RunWith(SpringRunner.class)
public class TestOne {@Autowiredprivate OrderChoice orderChoice;@Testpublic void testImportOne() {orderChoice.printOrderMsg();}
}
二 @Import和ImportSelector接口
使用@Import注解和ImportSelector接口静态地导入类
java">public class OrderChoice {public void printOrderMsg() {System.out.println("One订单信息是: order001");}
}public class OrderChoiceTwo {public void printOrderMsg() {System.out.println("Two订单信息是: order002");}
}public class OrderImportSelector implements ImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata annotationMetadata) {return new String[]{"com.hot.mango.service.OrderChoice","com.hot.mango.service.OrderChoiceTwo"};}
}@Configuration
@Import(OrderImportSelector.class)
public class CommonConfig {}@SpringBootTest
@RunWith(SpringRunner.class)
public class TestOne {@Autowiredprivate OrderChoice orderChoice;@Autowiredprivate OrderChoiceTwo orderChoiceTwo;@Testpublic void testImportTwo() {orderChoice.printOrderMsg();orderChoiceTwo.printOrderMsg();}
}
使用@Import注解和ImportSelector接口选择性地导入类
创建一个Order接口和两个实现类StudentOrder和TeacherOrder,有选择性的导入其中一个实现类
java">public interface Order {void printOrderMsg();
}public class StudentOrder implements Order{@Overridepublic void printOrderMsg() {System.out.println("学生的订单信息...");}
}public class TeacherOrder implements Order{@Overridepublic void printOrderMsg() {System.out.println("老师的订单信息...");}
}
创建ImportSelector接口的实现类,重写selectImports方法选择需要注入容器的Bean对象的全类名
java">public class OrderTwoImportSelector implements ImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata annotationMetadata) {Map<String, Object> map = annotationMetadata.getAnnotationAttributes(EnableOrder.class.getName());EnumOrder enumValue = (EnumOrder) map.get("value");switch (enumValue) {case STUDENT:return new String[] {"com.hot.mango.domain.StudentOrder"};case TEACHER:return new String[] {"com.hot.mango.domain.TeacherOrder"};default:return null;}}
}@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(OrderTwoImportSelector.class)
public @interface EnableOrder {EnumOrder value() default EnumOrder.STUDENT;
}public enum EnumOrder {STUDENT, TEACHER;
}
将@Import注解和其value属性值ImportSelector接口的实现类,添加在EnableOrder注解上,通过EnableOrder注解的value属性值可以选择性的注入Bean对象到容器
java">@Configuration
@EnableOrder(value = EnumOrder.TEACHER)
public class CommonConfig {}@SpringBootTest
@RunWith(SpringRunner.class)
public class TestOne {@Autowiredprivate Order order;@Testpublic void testImportThree() {order.printOrderMsg();}
}
三 @Import和ImportBeanDefinitionRegistrar接口
java">public class OrderRegistrar {public void printOrderMsg() {System.out.println("order msg print successfully");}
}public class OrderBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {@Overridepublic void registerBeanDefinitions(AnnotationMetadata metadata,BeanDefinitionRegistry registry) {RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();rootBeanDefinition.setBeanClass(OrderRegistrar.class);registry.registerBeanDefinition("orderRegistrar", rootBeanDefinition);}
}@Configuration
@Import(OrderBeanDefinitionRegistrar.class)
public class CommonConfig {}@SpringBootTest
@RunWith(SpringRunner.class)
public class TestOne {@Autowiredprivate OrderRegistrar orderRegistrar;@Testpublic void test() {orderRegistrar.printOrderMsg();}
}