标签:gap port for ima hit selector security cti 选择
public class Broker {} public class Customer {}
/** * 使用Import 将指定的类的实例注入到spring 容器中 */ @Component @Import({Customer.class,Broker.class}) public class ImportDirect { //顶部的@Import注解会将两个类的实例注入到spring IOC容器(注意,不是注入到ImportDirect类的实例中) }
public class FirstTest { public static void main(String[] args) { //ImportDirect类 顶部的@Import注解会将两个类的实例注入到spring IOC容器(注意,不是注入到ImportDirect类的实例中) AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ImportDirect.class); System.out.println(context.getBean(Customer.class)); System.out.println(context.getBean(Broker.class)); } }
public class Forwards {} public class Security {}
/** * 通过实现ImportBeanDefinitionRegistrar接口,将bean注入到spring 容器中 */ public class MyImportRegistrar implements ImportBeanDefinitionRegistrar { @Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { //手动注入实例 BeanDefinitionBuilder beanDef_forwards = BeanDefinitionBuilder.rootBeanDefinition(Forwards.class); registry.registerBeanDefinition("forwards",beanDef_forwards.getBeanDefinition()); BeanDefinitionBuilder beanDef_security = BeanDefinitionBuilder.rootBeanDefinition(Security.class); registry.registerBeanDefinition("security",beanDef_security.getBeanDefinition()); } }
@Component @Import({MyImportRegistrar.class}) public class App {}
public class SecondTest { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(App.class); System.out.println(context.getBean(Security.class)); System.out.println(context.getBean(Forwards.class)); System.out.println(context.getBean(MyImportRegistrar.class)); } }
public class Market {} public class Sales {}
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Import({MyImportSelector.class}) public @interface EnableMyTest { String[] name(); }
public class MyImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { System.out.println(importingClassMetadata.getAnnotationAttributes(EnableMyTest.class.getName()).toString().contains("onlySale")); //获取自定的@EnableLogInfo 信息 如果包含 onlySale 则只注入Sales class 否则 注入 Sales 和 Market 两个类 if (importingClassMetadata.getAnnotationAttributes(EnableMyTest.class.getName()) != null && importingClassMetadata.getAnnotationAttributes(EnableMyTest.class.getName()).toString().contains("onlySale")) { return new String[] {Sales.class.getName() }; } // 将指定的类 return new String[] { Market.class.getName(), Sales.class.getName() }; } }
@Component @EnableMyTest(name="onlySale") public class App {}
public class ThirdTest { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(App.class); System.out.println(context.getBean(Sales.class)); System.out.println(context.getBean(Market.class)); System.out.println(context.getBean(MyImportSelector.class)); } }
标签:gap port for ima hit selector security cti 选择
原文地址:https://www.cnblogs.com/nevermorewang/p/9032353.html