版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/catoop/article/details/50558333
声明同一个类下的多个实例:
package com.thunisoft.ssptsppt.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.*;
@Configuration
public class MultiBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
private static final Logger logger = LoggerFactory.getLogger(MultiBeanDefinitionRegistryPostProcessor.class);
private ScopeMetadataResolver scopeMetadataResolver = new AnnotationScopeMetadataResolver();
private BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
logger.info("Invoke Metho postProcessBeanFactory");
// 这里可以设置属性,例如
BeanDefinition bd = beanFactory.getBeanDefinition("dataSourceA");
MutablePropertyValues mpv = bd.getPropertyValues();
mpv.addPropertyValue("driverClassName", "com.mysql.jdbc.Driver1");
mpv.addPropertyValue("url", "jdbc:mysql://localhost:3306/test1");
mpv.addPropertyValue("username", "root1");
mpv.addPropertyValue("password", "1234561");
BeanDefinition bd2 = beanFactory.getBeanDefinition("dataSourceB");
MutablePropertyValues mpv2 = bd2.getPropertyValues();
mpv2.addPropertyValue("driverClassName", "com.mysql.jdbc.Driver2");
mpv2.addPropertyValue("url", "jdbc:mysql://localhost:3306/test2");
mpv2.addPropertyValue("username", "root2");
mpv2.addPropertyValue("password", "1234562");
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
logger.info("Invoke Metho postProcessBeanDefinitionRegistry");
registerBean(registry, "dataSourceA", org.apache.tomcat.jdbc.pool.DataSource.class);
registerBean(registry, "dataSourceB", org.apache.tomcat.jdbc.pool.DataSource.class);
}
private void registerBean(BeanDefinitionRegistry registry, String name, Class<?> beanClass) {
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(beanClass);
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
abd.setScope(scopeMetadata.getScopeName());
String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, registry));
AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, registry);
}
}
从 Spring3.0 开始,增加了一种新的途经来配置Bean Definition,这就是通过 Java Code 配置 Bean Definition。
与Xml和Annotation两种配置方式不同点在于:
前两种Xml和Annotation的配置方式为预定义方式,即开发人员通过 XML 文件或者 Annotation 预定义配置 bean 的各种属性后,启动 Spring 容器,Spring 容器会首先解析这些配置属性,生成对应都?Bean Definition,装入到 DefaultListableBeanFactory 对象的属性容器中去。与此同时,Spring 框架也会定义一些内部使用的 Bean 定义,如 bean 名为”org.springframework.context.annotation.internalConfigurationAnnotationProcessor”的 ConfigurationClassPostProcessor 定义。
而后此刻不会做任何 Bean Definition 的定义解析动作,Spring 框架会根据前两种配置,过滤出 BeanDefinitionRegistryPostProcessor 类型的 Bean 定义,并通过 Spring 框架生成其对应的 Bean 对象(如 ConfigurationClassPostProcessor 实例)。结合 Spring 上下文源码可知这个对象是一个 processor 类型工具类,Spring 容器会在实例化开发人员所定义的 Bean 前先调用该 processor 的 postProcessBeanDefinitionRegistry(…) 方法。此处实现基于 Java Code 配置Bean Definition的处理。
基于 Java Code 解析 Bean 的顺序图(查看大图)
该图供大家了解即可,这里不做详细说明。
基于 Java Code 的配置方式,其执行原理不同于前两种。它是在 Spring 框架已经解析了基于 XML 和 Annotation 配置后,通过加入 BeanDefinitionRegistryPostProcessor 类型的 processor 来处理配置信息,让开发人员通过 Java 编程方式定义一个 Java 对象。其优点在于可以将配置信息集中在一定数量的 Java 对象中,同时通过 Java 编程方式,比基于 Annotation 方式具有更高的灵活性。并且该配置方式给开发人员提供了一种非常好的范例来增加用户自定义的解析工具类。其主要缺点在于与 Java 代码结合紧密,配置信息的改变需要重新编译 Java 代码,另外这是一种新引入的解析方式,需要一定的学习成本。
另外提及一点的就是,Spring框架有3个主要的Hook类,分别是:
org.springframework.context.ApplicationContextAware
它的setApplicationContext 方法将在Spring启动之前第一个被调用。我们用来同时启动Jdon框架。
org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor
它的postProcessBeanDefinitionRegistry 和 postProcessBeanFactory 方法是第二和第三被调用,它们在Bean初始化创建之前启动,如果Spring的bean需要的其他第三方中的组件,我们在这里将其注入给Spring。
org.springframework.context.ApplicationListener
用于在初始化完成后做一些事情,当Spring所有XML或元注解的Bean都启动被创建成功了,这时会调用它的唯一方法onApplicationEvent。
下面我们来完成一个,自己通过java代码创建bean,并注册为Spring管理。
本例中,我们创建一个接口,然后创建该接口的2个实现类,分别命名不同的名字,然后在需要注入的地方使用@Qualifier 指定注入对应的实例。
1、接口Shanhy.java
package org.springboot.sample.config;
public interface Shanhy {
void display();
}
2、实现类ShanhyA.java
package org.springboot.sample.config;
public class ShanhyA implements Shanhy {
@Override
public void display() {
System.out.println("AAAAAAAAAAAA");
}
}
3、实现类ShanhyB.java
package org.springboot.sample.config;
public class ShanhyB implements Shanhy {
@Override
public void display() {
System.out.println("BBBBBBBBBBBB");
}
}
4、定义接口BeanDefinitionRegistryPostProcessor的实现
package org.springboot.sample.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.annotation.AnnotationScopeMetadataResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ScopeMetadata;
import org.springframework.context.annotation.ScopeMetadataResolver;
/**
* 实现自己实例化bean并注册为Spring管理
*
* @author 单红宇(365384722)
* @myblog http://blog.csdn.net/catoop/
* @create 2016年1月21日
*/
@Configuration
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
private static final Logger logger = LoggerFactory.getLogger(MyBeanDefinitionRegistryPostProcessor.class);
private ScopeMetadataResolver scopeMetadataResolver = new AnnotationScopeMetadataResolver();
private BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
logger.info("Invoke Metho postProcessBeanFactory");
// 这里可以设置属性,例如
BeanDefinition bd = beanFactory.getBeanDefinition("dataSourceA");
MutablePropertyValues mpv = bd.getPropertyValues();
mpv.addPropertyValue("driverClassName", "com.mysql.jdbc.Driver");
mpv.addPropertyValue("url", "jdbc:mysql://localhost:3306/test");
mpv.addPropertyValue("username", "root");
mpv.addPropertyValue("password", "123456");
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
logger.info("Invoke Metho postProcessBeanDefinitionRegistry");
registerBean(registry, "shanhyA", ShanhyA.class);
registerBean(registry, "shanhyB", ShanhyB.class);
registerBean(registry, "dataSourceA", org.apache.tomcat.jdbc.pool.DataSource.class);
}
private void registerBean(BeanDefinitionRegistry registry, String name, Class<?> beanClass){
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(beanClass);
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
abd.setScope(scopeMetadata.getScopeName());
// 可以自动生成name
String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, registry));
AnnotationConfigUtils.processCommonDefinitionAnnotations(abd);
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, registry);
}
}
5、使用测试
和平常一样可以直接注入我们的对象,对于同样接口的我们需要指定name
/**
* 测试参数注入
*
* @author 单红宇(365384722)
* @myblog http://blog.csdn.net/catoop/
* @create 2016年1月13日
*/
@Configuration
public class MyConfiguration {
@Bean
public FilterRegistrationBean filterRegistrationBean(@Qualifier("shanhyB") Shanhy shanhy) {
FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
shanhy.display();
// 省略代码
return filterRegistration;
}
}
使用@Resource 或者 @Autowired并指定@Qualifier 也可以
@RestController
@RequestMapping("/hello")
public class HelloController {
@Resource(name="shanhyA")
private Shanhy shanhyA;
@Autowired
@Qualifier("shanhyB")
private Shanhy shanhyB;
// 省略代码
}
这里有点经验要说一下,在 @Configuration 中,不能使用注入属性的方式注入,只能通过参数的方式注入,其原因就是@Configuration的类一开始变被加载,此时你想进行属性注入,需要注入的bean对象都还不存在呢。
下一篇文章,我们将使用这种方法动态创建基于MyBatis的多数据源。
下面的代码片段也可以注册Bean,比较简单:
@Configuration
@Import(Registrar.class)
public class TestConfig {
}
class Registrar implements ImportBeanDefinitionRegistrar {
private static final String BEAN_NAME = "myTestBean";
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
if (!registry.containsBeanDefinition(BEAN_NAME)) {
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(ExamplePostProcessor.class);
beanDefinition.setSynthetic(true);
registry.registerBeanDefinition(BEAN_NAME, beanDefinition);
}
}
}
=============
============
===========
Spring点滴十一:Spring中BeanFactoryPostProcessor和BeanPostProcessor区别
Spring中BeanFactoryPostProcessor和BeanPostProcessor都是Spring初始化bean时对外暴露的扩展点。两个接口从名字看起来很相似,但是作用及使用场景却不同。
关于BeanPostProcessor介绍在这篇文章中已经讲过:http://www.cnblogs.com/sishang/p/6576665.html 这里主要介绍BeanFactoryPostProcessor。
Spring IoC容器允许BeanFactoryPostProcessor在容器实例化任何bean之前读取bean的定义(配置元数据),并可以修改它。同时可以定义多个BeanFactoryPostProcessor,通过设置'order'属性来确定各个BeanFactoryPostProcessor执行顺序。
注册一个BeanFactoryPostProcessor实例需要定义一个Java类来实现BeanFactoryPostProcessor接口,并重写该接口的postProcessorBeanFactory方法。通过beanFactory可以获取bean的定义信息,并可以修改bean的定义信息。这点是和BeanPostProcessor最大区别
public interface BeanFactoryPostProcessor { /** * Modify the application context's internal bean factory after its standard * initialization. All bean definitions will have been loaded, but no beans * will have been instantiated yet. This allows for overriding or adding * properties even to eager-initializing beans. * @param beanFactory the bean factory used by the application context * @throws org.springframework.beans.BeansException in case of errors */ void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; }
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"> <!-- 支持Spring注解 --> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<!-- 注册一个BeanPostProcessor --> <bean id="postProcessor" class="com.test.spring.PostProcessor"/> <!-- 注册一个BeanFactoryPostProcessor --> <bean id="factoryPostProcessor" class="com.test.spring.FactoryPostProcessor"/>
<!-- 普通bean --> <bean id="beanFactoryPostProcessorTest" class="com.test.spring.BeanFactoryPostProcessorTest"> <property name="name" value="张三"/> <property name="sex" value="男"/> </bean> </beans>
BeanPostProcessor.java
package com.test.spring; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; /** * bean后置处理器 * @author zss * */ public class PostProcessor implements BeanPostProcessor{ @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("后置处理器处理bean=【"+beanName+"】开始"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("后置处理器处理bean=【"+beanName+"】完毕!"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return bean; } }
BeanFactoryPostProcessor.java
package com.test.spring; import org.springframework.beans.BeansException; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.PropertyValue; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; public class FactoryPostProcessor implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory( ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { System.out.println("******调用了BeanFactoryPostProcessor"); String[] beanStr = configurableListableBeanFactory .getBeanDefinitionNames(); for (String beanName : beanStr) { if ("beanFactoryPostProcessorTest".equals(beanName)) { BeanDefinition beanDefinition = configurableListableBeanFactory .getBeanDefinition(beanName); MutablePropertyValues m = beanDefinition.getPropertyValues(); if (m.contains("name")) { m.addPropertyValue("name", "赵四");
System.out.println("》》》修改了name属性初始值了"); } } } } }
BeanFactoryPostProcessorTest.java
package com.test.spring; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class BeanFactoryPostProcessorTest implements InitializingBean,DisposableBean,BeanNameAware,BeanFactoryAware { private String name; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public void setBeanFactory(BeanFactory paramBeanFactory) throws BeansException { System.out.println("》》》调用了BeanFactoryAware的setBeanFactory方法了"); } @Override public void setBeanName(String paramString) { System.out.println("》》》调用了BeanNameAware的setBeanName方法了"); } @Override public void destroy() throws Exception { System.out.println("》》》调用了DisposableBean的destroy方法了"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("》》》调用了Initailization的afterPropertiesSet方法了"); } @Override public String toString() { return "BeanFactoryPostProcessorTest [name=" + name + ", sex=" + sex + "]"; } }
Test case:
package com.test.spring; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class T { ApplicationContext applicationcontext=null; @Before public void before() { System.out.println("》》》Spring ApplicationContext容器开始初始化了......"); applicationcontext= new ClassPathXmlApplicationContext(new String[]{"spring-service.xml"}); System.out.println("》》》Spring ApplicationContext容器初始化完毕了......"); } @Test public void test() { //BeanLifecycle beanLifecycle =applicationcontext.getBean("beanLifecycle",BeanLifecycle.class); BeanFactoryPostProcessorTest beanFactoryPostProcessorTest=applicationcontext.getBean(BeanFactoryPostProcessorTest.class); System.out.println(beanFactoryPostProcessorTest.toString()); } }
测试结果:
》》》Spring ApplicationContext容器开始初始化了......
2017-03-20 14:36:10 INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@17ad352e: startup date [Mon Mar 20 14:36:10 CST 2017]; root of context hierarchy
2017-03-20 14:36:10 INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [spring-service.xml]
******调用了BeanFactoryPostProcessor
》》》修改了name属性初始值了
》》》调用了BeanNameAware的setBeanName方法了
》》》调用了BeanFactoryAware的setBeanFactory方法了
后置处理器处理bean=【beanFactoryPostProcessorTest】开始
后置处理器开始调用了
》》》调用了Initailization的afterPropertiesSet方法了
后置处理器处理bean=【beanFactoryPostProcessorTest】完毕!
后置处理器调用结束了
》》》Spring ApplicationContext容器初始化完毕了......
BeanFactoryPostProcessorTest [name=赵四, sex=男]
---------------------------------------------------------------------------------------------------------
从测试结果中可以看到beanFactoryPostProcessorTest定义的name值由"张三"变为"赵四",同时发现postProcessorBeanFactory方法执行顺序先于BeanPostProcessor接口中方法。
***************************************************************************************************************************
在Spring中内置了一些BeanFactoryPostProcessor实现类:
- org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
- org.springframework.beans.factory.config.PropertyOverrideConfigurer
- org.springframework.beans.factory.config.CustomEditorConfigurer:用来注册自定义的属性编辑器
备注:下一篇将会介绍PropertyPlaceHoldConfigurer在Spring机制中如何读取配置文件的信息