SpringBoot自动装配之@Import深入讲解
不死鸟.亚历山大.狼崽子 人气:0@Enable*底层依赖于@Import注解导入一些类,使用@Import导入的类会被Spring加载到IOC容器中。而@Import提供4中用法:
- 导入Bean
- 导入配置类
- 导入ImportSelector 实现类。一般用于加载配置文件中的类
- 导入ImportBeanDefinitionRegistrar实现类。
1 导入Bean
定义一个user实体类:
package com.enable.entity; public class User { }
测试功能:
package com.example.demo; import com.enable.config.EnableUser; import com.enable.config.UserConfig; import com.enable.entity.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; import java.util.Map; @SpringBootApplication @Import(User.class) public class SpringbootApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args); User user = context.getBean(User.class); System.out.println(user); Map<String,User> map = context.getBeansOfType(User.class); System.out.println(map); } }
结果如下:
2 导入配置类
新建实体类:
package com.enable.entity; public class Role { }
package com.enable.entity; public class User { }
测试功能:
package com.example.demo; import com.enable.config.EnableUser; import com.enable.config.UserConfig; import com.enable.entity.Role; import com.enable.entity.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; import java.util.Map; @SpringBootApplication @Import(UserConfig.class) public class SpringbootApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args); User user = context.getBean(User.class); System.out.println(user); Role role = context.getBean(Role.class); System.out.println(role); } }
测试结果:
3 导入ImportSelector 实现类
新建实体类:
package com.enable.entity; public class Role { }
package com.enable.entity; public class User { }
编写ImportSelector:
package com.enable.config; 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[]{"com.enable.entity.Role","com.enable.entity.User"}; } }
测试功能:
package com.example.demo; import com.enable.config.EnableUser; import com.enable.config.MyImportSelector; import com.enable.config.UserConfig; import com.enable.entity.Role; import com.enable.entity.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; import java.util.Map; @SpringBootApplication @Import(MyImportSelector.class) public class SpringbootApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args); User user = context.getBean(User.class); System.out.println(user); Role role = context.getBean(Role.class); System.out.println(role); } }
结果如下:
4 导入ImportBeanDefinitionRegistrar实现类
新建实体类:
package com.enable.entity; public class User { }
ImportBeanDefinitionRegistrar实现类
package com.enable.config; import com.enable.entity.User; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; 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) { AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition(); registry.registerBeanDefinition("user",beanDefinition); } }
测试结果:
package com.example.demo; import com.enable.config.EnableUser; import com.enable.config.MyImportBeanDefinitionRegistrar; import com.enable.config.MyImportSelector; import com.enable.config.UserConfig; import com.enable.entity.Role; import com.enable.entity.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; import java.util.Map; @SpringBootApplication @Import(MyImportBeanDefinitionRegistrar.class) public class SpringbootApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args); Object user1 = context.getBean("user"); System.out.println(user1); User user = context.getBean(User.class); System.out.println(user); } }
结果如下:
加载全部内容