1.导入bean
主类
package com.example.demo03; import com.example.demo.Role; import com.example.demo.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Import; /* @Import的用法 1.导入bean 2.导入配置类 3.导入ImportSelector的实现类 4.导入ImportBeanDefinitionRegistrar的实现类 */ @SpringBootApplication @Import(User.class) public class Demo03Application { public static void main(String[] args) { ConfigurableApplicationContext context=SpringApplication.run(Demo03Application.class, args); User user = context.getBean(User.class); System.out.println(user); Role role = context.getBean(Role.class); System.out.println(role); } }

2.导入配置类
配置类:
package com.example.config; import com.example.demo.Role; import com.example.demo.User; import org.springframework.context.annotation.Bean; public class UserConfig { @Bean public User user(){ return new User(); } @Bean public Role role(){ return new Role(); } }
启动类:
package com.example.demo03; import com.example.config.UserConfig; import com.example.demo.Role; import com.example.demo.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Import; /* @Import的用法 1.导入bean 2.导入配置类 3.导入ImportSelector的实现类 4.导入ImportBeanDefinitionRegistrar的实现类 */ @SpringBootApplication @Import(UserConfig.class) public class Demo03Application { public static void main(String[] args) { ConfigurableApplicationContext context=SpringApplication.run(Demo03Application.class, args); User user = context.getBean(User.class); System.out.println(user); Role role = context.getBean(Role.class); System.out.println(role); } }
3.导入ImportSelector的实现类
ImportSelector的实现类MyImportSelector:
(这里写得是字符串"com.example.demo.User",可以写在配置文件中动态获取,不用写死)
package com.example.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.example.demo.User","com.example.demo.Role" }; } }
启动类:
package com.example.demo03; import com.example.config.MyImportSelector; import com.example.demo.Role; import com.example.demo.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Import; /* @Import的用法 1.导入bean 2.导入配置类 3.导入ImportSelector的实现类 4.导入ImportBeanDefinitionRegistrar的实现类 */ @SpringBootApplication @Import(MyImportSelector.class) public class Demo03Application { public static void main(String[] args) { ConfigurableApplicationContext context=SpringApplication.run(Demo03Application.class, args); User user = context.getBean(User.class); System.out.println(user); Role role = context.getBean(Role.class); System.out.println(role); } }
4.导入ImportBeanDefinitionRegistrar的实现类
ImportBeanDefinitionRegistrar的实现类MyImportBeanDefinitionRegistrar:
package com.example.config; import com.example.demo.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.beans.factory.support.BeanNameGenerator; import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; import org.springframework.core.type.AnnotationMetadata; public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override//重写ImportBeanDefinitionRegistrar的方法 public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) { AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition(); registry.registerBeanDefinition("user",beanDefinition );//IOC容器里注册了一个Bean,user是名字,类型是User.class } }
启动类:
package com.example.demo03; import com.example.config.MyImportBeanDefinitionRegistrar; import com.example.demo.Role; import com.example.demo.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Import; /* @Import的用法 1.导入bean 2.导入配置类 3.导入ImportSelector的实现类 4.导入ImportBeanDefinitionRegistrar的实现类 */ @SpringBootApplication @Import(MyImportBeanDefinitionRegistrar.class) public class Demo03Application { public static void main(String[] args) { ConfigurableApplicationContext context=SpringApplication.run(Demo03Application.class, args); User user = context.getBean(User.class); System.out.println(user); Role role = context.getBean(Role.class); System.out.println(role); } }
实现类MyImportBeanDefinitionRegistrar注册bean时没注册类Role所以:

本文来自博客园,作者:阿霖找BUG,转载请注明原文链接:https://www.cnblogs.com/lin-07/articles/16331819.html
浙公网安备 33010602011771号