这篇“Spring怎么将一个的类配置成Bean”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Spring怎么将一个的类配置成Bean”文章吧。
一、说明
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; /** * @Author wangyouhui * @Description 配置类 **/ @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; /** * @Author wangyouhui * @Description 配置类 **/ @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; /** * @Author wangyouhui * @Description 配置类 **/ @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; /** * @Author wangyouhui * @Description beanDefinition注册后置处理器 **/ @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 { } }