一、说明
1.第三方的bean不能改写源码,在类上加@Component注解
2.只能引入该类进行反射创建,再更改其BeanDefinition信息
二、四种方式
1.@Bean方式,实例化过程可以更改
2.@Import导入类,实例化过程不能干预
3.@Import导入ImportBeanDefinitionRegistrar实现类,可以给BeanDefinition设置属性等信息
4.beanDefinition注册后置处理器(BeanDefinitionRegistryPostProcessor),不需要@Import引入,但顺序在后面
三、代码示例
3.1 @Bean方式
package com.learning.bean;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanConfig {
@Bean
public DruidDataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUsername("root");
return dataSource;
}
}
3.2 @Bean方式
package com.learning.bean;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import(DruidDataSource.class)
public class BeanConfig2 {
}
3.3 @Import导入ImportBeanDefinitionRegistrar实现类
package com.learning.bean;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.type.AnnotationMetadata;
@Configuration
@Import(ImportBeanDefinitionRegistrar.class)
public class BeanConfig3 {
}
class ImportBeanDefinitionRegistrar implements org.springframework.context.annotation.ImportBeanDefinitionRegistrar{
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
// BeanDefinition存储Bean的描述信息,决定Bean生产方式
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(DruidDataSource.class);
rootBeanDefinition.getPropertyValues().add("password", "root");
registry.registerBeanDefinition("dataSource", rootBeanDefinition);
}
}
3.4 BeanDefinitionRegistryPostProcessor
package com.learning.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;
@Component
public class BeanDefinitionRegistryPostProcessor implements org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
RootBeanDefinition beanDefinition = new RootBeanDefinition();
beanDefinition.getPropertyValues().add("password", "root");
beanDefinitionRegistry.registerBeanDefinition("dataSource", beanDefinition);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
}
}
到此这篇关于Spring将一个的类配置成Bean的方式详解的文章就介绍到这了,更多相关Spring将类配成Bean内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!