前言
Spring和SpringBoot是当前最火的Java开发框架,很多时候我们都会将对象注册到Spring的容器中,将对象交给Spring来管理,那么今天我们就来盘点一下将Bean注册到Spring容器的7种方式。
一:xml配置 + @ImportResource
@Data
public class XmlTestBean {
private Integer age;
private String name;
}
① 在resource目录下创建Spring的xml配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="xmlTestBean" class="com.example.demo.XmlTestBean">
<property name="age" value="18"/>
<property name="name" value="xmlBean"/>
</bean></beans>复制代码
② 利用@ImportResource注解导入xml配置文件
@SpringBootApplication
@ImportResource("classpath:XmlTestBean.xml")
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);
XmlTestBean bean = run.getBean(XmlTestBean.class);
System.out.println(bean);
}
}
输出结果:
二:@Component + @ComponentScan
① 在需要注册的类上加@Component注解,并且通过@ComponentScan对其进行扫描。
@Component
@Data
public class ComponentTestBean {
private Integer age = 18;
private String name = "componentTestBean";
}
② 通过@ComponentScan注解扫描有@Component注解的类
@SpringBootApplication(scanBasePackages = "com.example.demo")
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);
ComponentTestBean bean = run.getBean(ComponentTestBean.class);
System.out.println(bean);
}
}
输出结果:
注:@Service,@Controller,@Repository,@Configuration注解都是@Component注解的复合注解,都归为一类吧。
三: @Configuration + @Bean + @ComponentScan
@Data
public class ConfigurationTestBean {
private Integer age;
private String name;
}
① 通过@Configuration和@Bean注解定义bean的信息
@Configuration
public class TestBeanConfiguration {
@Bean
public ConfigurationTestBean configurationTestBean(){
ConfigurationTestBean configurationTestBean = new ConfigurationTestBean();
configurationTestBean.setAge(18);
configurationTestBean.setName("configurationTestBean");
return configurationTestBean;
}
}
②:利用@ComponentScan注解扫描@Configuration注解
@SpringBootApplication(scanBasePackages = "com.example.demo")
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);
ConfigurationTestBean bean = run.getBean(ConfigurationTestBean.class);
System.out.println(bean);
}
}
输出结果:
四:实现FactoryBean接口
@Data
public class TestFactoryBean {
private Integer age;
private String name;
}
① 实现FactoryBean接口
重写getObject()方法,在getObject()方法中定义bean的信息。
@Component
public class FactoryBeanTest implements FactoryBean<TestFactoryBean> {
@Override
public TestFactoryBean getObject() throws Exception {
TestFactoryBean testFactoryBean = new TestFactoryBean();
testFactoryBean.setAge(18);
testFactoryBean.setName("testFactoryBean");
return testFactoryBean;
}
@Override
public Class<?> getObjectType() {
return TestFactoryBean.class;
}
}
② 利用@ComponentScan注解扫描FactoryBean实现类的@Component注解
@SpringBootApplication(scanBasePackages = "com.example.demo")
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);
TestFactoryBean bean = run.getBean(TestFactoryBean.class);
System.out.println(bean);
}
}
输出结果:
五:实现BeanDefinitionRegistryPostProcessor接口
@Data
public class RegisterTestBean {
private Integer age;
private String name;
}
①:实现BeanDefinitionRegistryPostProcessor接口
实现BeanDefinitionRegistryPostProcessor接口,并且重写postProcessBeanDefinitionRegistry方法,利用BeanDefinitionBuilder构建BeanDefinition,然后利用BeanDefinitionRegistry将bean进行注册。
@Component
public class RegisterBeanTest implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
BeanDefinitionBuilder beanDefinitionBuilder =
BeanDefinitionBuilder.genericBeanDefinition(RegisterTestBean.class);
beanDefinitionBuilder.addPropertyValue("age",18);
beanDefinitionBuilder.addPropertyValue("name","registerTestBean");
AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
registry.registerBeanDefinition("registerTestBean",beanDefinition);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
}
}
注:也可以在postProcessBeanFactory方法中实现相关的逻辑,因为在这里ConfigurableListableBeanFactory其实也是BeanDefinitionRegistry类型,可以进行强转。
②利用@ComponentScan注解扫描BeanDefinitionRegistryPostProcessor实现类的@Component注解
@SpringBootApplication(scanBasePackages = "com.example.demo")
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);
RegisterTestBean bean = run.getBean(RegisterTestBean.class);
System.out.println(bean);
}
}
输出结果:
六:实现BeanFactoryPostProcessor接口
① 实现BeanFactoryPostProcessor接口,利用BeanDefinitionBuilder构建bean的信息,然后通过BeanDefinitionRegistry将bean注册到Spring容器中。
@Data
public class BeanFactoryProcessorTestBean {
private Integer age;
private String name;
}
@Component
public class TestBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
if (configurableListableBeanFactory instanceof BeanDefinitionRegistry){
BeanDefinitionBuilder beanDefinitionBuilder =
BeanDefinitionBuilder.genericBeanDefinition(BeanFactoryProcessorTestBean.class);
beanDefinitionBuilder.addPropertyValue("age",18);
beanDefinitionBuilder.addPropertyValue("name","BeanFactoryProcessorTestBean");
AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
BeanDefinitionRegistry registry = (BeanDefinitionRegistry)configurableListableBeanFactory;
registry.registerBeanDefinition("beanFactoryProcessorTestBean",beanDefinition);
}
}
}
② 利用@ComponentScan注解扫描BeanFactoryPostProcessor实现类的@Component注解
@SpringBootApplication(scanBasePackages = "com.example.demo")
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);
BeanFactoryProcessorTestBean bean = run.getBean(BeanFactoryProcessorTestBean.class);
System.out.println(bean);
}
}
输出结果:
七:通过@Import注解
@Import注解有四种用法
① import一个普通的类
② import ImportSelector
接口的实现类
③ import DeferredImportSelector
接口的实现类
④ import ImportBeanDefinitionRegistrar
接口的实现类
注:关于@Import注解 ,细讲的话篇幅会过长,如果你还对@Import注解有所疑问,那么可以去看我的另外一篇文章 @Import注解的使用和原理,相信会对你有所帮助
最后
如果有任何疑问,欢迎在下方评论区留言。