SpringBoot自动装配之@Import深入讲解
短信预约 -IT技能 免费直播动态提醒
@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内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341