- 普通类
- 实现了ImportSelector接口的类
- 实现了ImportBeanDefinitionRegistrar接口的类
普通类
package hdu.gongsenlin;
public class NorMal {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
import org.springframework.context.annotation.ComponentScan;
@ComponentScan("hdu.gongsenlin")
public class AppConfig {
}
package hdu.gongsenlin;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(NorMal.class)
public class MyConfigure {
}
import hdu.gongsenlin.NorMal;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
ac.start();
NorMal normal = ac.getBean(NorMal.class);
System.out.println(normal != null);
}
}
结果:运行的结果是输出true,表示Normal对象可以通过Spring容器获取,也就证明了Import注解起作用了。
实现了ImportSelector接口的类
package hdu.gongsenlin;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
import java.util.function.Predicate;
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"hdu.gongsenlin.Normal"};
}
}
@Configuration
@Import(MyImportSelector.class)
public class MyConfigure {
}
import hdu.gongsenlin.NorMal;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
ac.start();
NorMal normal = ac.getBean(NorMal.class);
System.out.println(normal != null);
}
}
MyImportSelector改为如下,表示过滤掉类全限定名为"hdu.gongsenlin.Normal"的类,该类将不会被注入到Spring容器当中。当然这里可以使用通配符的匹配方式,读者可以自己去尝试一下。
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"hdu.gongsenlin.Normal"};
}
@Override
public Predicate<String> getExclusionFilter() {
Predicate<String> predicate = new Predicate<String>() {
@Override
public boolean test(String s) {
if (s.matches("hdu.gongsenlin.Normal")) {
return true;
}
return false;
}
};
return predicate;
}
}
此时再次执行测试代码,控制台输出如下:
实现了ImportBeanDefinitionRegistrar接口的类
package hdu.gongsenlin;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(NorMal.class);
AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
registry.registerBeanDefinition("normal", beanDefinition);
}
//或者 使用如下的方法也可以,自动生成beanName
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(NorMal.class);
AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
String beanName = importBeanNameGenerator.generateBeanName(beanDefinition, registry);
registry.registerBeanDefinition(beanName, beanDefinition);
}
}
测试代码不变,执行结果为true,这里就不贴图了。