本站收集了一篇相关的编程文章,网友幸成益根据主题投稿了本篇教程内容,涉及到SpringBoot、@Import、SpringBoot自动装配、SpringBoot @Import相关内容,已被479网友关注,涉猎到的知识点内容可以在下方电子书获得。
SpringBoot @Import
@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); } }
结果如下:
到此这篇关于SpringBoot自动装配之@Import深入讲解的文章就介绍到这了,更多相关SpringBoot @Import内容请搜索码农之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持码农之家!