Spring——》@Import注解
一、介绍
@Import注解出现在 Spring 3.0
二、作用
1、替换标签,导入第三方的配置类
2、把特定类型的对象注入到Spring容器中(静态注入、动态注入)
三、导入配置类
1、RedisConfig.java
import org.springframework.context.annotation.Configuration;
@Configuration
public class RedisConfig {
}
2、MyBatisConfig.java
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisConfig {
}
3、JavaConfig.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@Import(value = {RedisConfig.class,MyBatisConfig.class})
public class JavaConfig {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("xxx.xml");
System.out.println(ac);
}
}
四、对象注入方式
方式1、静态注入
1)UserService.java
public class UserService {
}
2)JavaConfig.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Import;
@Import(UserService.class)
public class JavaConfig {
public static void main(String[] args) {
ApplicationContext ac = new AnnotationConfigApplicationContext(JavaConfig.class);
// 结果:com.example.test.UserService@1bb266b3
System.out.println(ac.getBean(UserService.class));
}
}
方式2、动态注入
动态注入的实现有2种:
1)实现 ImportSelector 接口 ,重写 selectImports()
2)实现 ImportBeanDefinitionRegistrar接口,重写 registerBeanDefinitions()
1)实现ImportSelector接口 ,重写selectImports()
<1> MyImportSelector.java
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{RedisConfig.class.getName(), MyBatisConfig.class.getName()};
}
}
<2> JavaConfig.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Import;
@Import(MyImportSelector.class)
public class JavaConfig {
public static void main(String[] args) {
ApplicationContext ac = new AnnotationConfigApplicationContext(JavaConfig.class);
for (String beanDefinitionName : ac.getBeanDefinitionNames()) {
// beanDefinitionName = com.example.test.RedisConfig
// beanDefinitionName = com.example.test.MyBatisConfig
System.out.println("beanDefinitionName = " + beanDefinitionName);
}
}
}
2)实现ImportBeanDefinitionRegistrar接口,重写registerBeanDefinitions()
<1> MyImportBeanDefinitionRegistrar.java
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
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) {
RootBeanDefinition redis = new RootBeanDefinition(RedisConfig.class);
registry.registerBeanDefinition("redis666", redis);
RootBeanDefinition mybatis = new RootBeanDefinition(MyBatisConfig.class);
registry.registerBeanDefinition("mybatis", mybatis);
}
}
<2> JavaConfig.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Import;
@Import(MyImportBeanDefinitionRegistrar.class)
public class JavaConfig {
public static void main(String[] args) {
ApplicationContext ac = new AnnotationConfigApplicationContext(JavaConfig.class);
for (String beanDefinitionName : ac.getBeanDefinitionNames()) {
// beanDefinitionName = redis666
// beanDefinitionName = mybatis
System.out.println("beanDefinitionName = " + beanDefinitionName);
}
}
}
四、@Import 与 @Enable区别
@Enable的本质其实就是@Import
五、Spring源码中动态注入的示例
ConfigurationClassParser.processImports()