Spring Boot 使用Java代码创建Bean并注册到Spring中

来源:互联网 发布:沥青库存数据 编辑:程序博客网 时间:2024/06/04 20:11

转:http://blog.csdn.net/catoop/article/details/50558333,http://412887952-qq-com.iteye.com/blog/2301642


从 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 的配置方式,其执行原理不同于前两种。它是在 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 指定注入对应的实例。


接口Shanhy.java

package com.example.demo;public interface Shanhy {public void dispaly();}

实现类ShanhyA.java

package com.example.demo;public class ShanhyA implements Shanhy{@Overridepublic void dispaly() {// TODO Auto-generated method stubSystem.out.println("AAAAAAAAAAAAAAA");}}


实现类ShanhyB.java

package com.example.demo;public class ShanhyB implements Shanhy{@Overridepublic void dispaly() {// TODO Auto-generated method stubSystem.out.println("BBBBBBBBBB");}}
4、定义接口BeanDefinitionRegistryPostProcessor的实现

package com.example.config;import org.springframework.beans.BeansException;import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;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.Configuration;import com.example.demo.ShanhyA;import com.example.demo.ShanhyB;@Configurationpublic class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {// bean 的名称生成器.private BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {// TODO Auto-generated method stubSystem.out.println("MyBeanDefinitionRegistryPostProcessor.postProcessBeanFactory()");}/** * 先执行:postProcessBeanDefinitionRegistry()方法, * 在执行:postProcessBeanFactory()方法。 * */@Overridepublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {// TODO Auto-generated method stubSystem.out.println("MyBeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry()");/* * 在这里可以注入bean. */registerBean(registry, "shanhyA", ShanhyA.class);registerBean(registry, "shanhyB", ShanhyB.class);}/** * 提供公共的注册方法。 *  * @param beanDefinitionRegistry * @param name * @param beanClass */private void registerBean(BeanDefinitionRegistry registry, String name, Class<?> beanClass) {AnnotatedBeanDefinition annotatedBeanDefinition = new AnnotatedGenericBeanDefinition(beanClass);// 可以自动生成nameString beanName = (name != null ? name: this.beanNameGenerator.generateBeanName(annotatedBeanDefinition, registry));// bean注册的holer类.BeanDefinitionHolder beanDefinitionHolder = new BeanDefinitionHolder(annotatedBeanDefinition, beanName);// 使用bean注册工具类进行注册.BeanDefinitionReaderUtils.registerBeanDefinition(beanDefinitionHolder, registry);}}

这个类里的代码比较多,在这里简单的介绍下:方法postProcessBeanDefinitionRegistry()是用来注册bean的;而具体注册的代码比较是通用的,我们定义一个私有的方法进行注册。

postProcessBeanFactory()是bean配置的工厂方法,在这个方法中可以获取到我们所有在postProcessBeanDefinitionRegistry方法中注册的所有bean,在这里我们可以进行属性的设置等操作。

// 这里可以设置属性,例如       BeanDefinition beanDefinition = beanFactory.getBeanDefinition("dataSourceA");       MutablePropertyValues mutablePropertyValues = beanDefinition.getPropertyValues();       //加入属性.       mutablePropertyValues.addPropertyValue("driverClassName", "com.mysql.jdbc.Driver");       mutablePropertyValues.addPropertyValue("url", "jdbc:mysql://localhost:3306/test");       mutablePropertyValues.addPropertyValue("username", "root");       mutablePropertyValues.addPropertyValue("password", "123456");

测试代码:

以直接注入我们的对象,对于同样接口的我们需要指定name:

package com.example.config;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import com.example.demo.A;import com.example.demo.Shanhy;@Configurationpublic class MyConfig {/** * 这里只是测试使用,没有实际的意义. * 这里有点经验要说一下,在 @Configuration 中,不能使用注入属性的方式注入,只能通过参数的方式注入, * 其原因就是@Configuration的类一开始变被加载,此时你想进行属性注入,需要注入的bean对象都还不存在呢。 */@Beanpublic A filterRegistrationBean(@Qualifier("shanhyB") Shanhy shanhy) { A a = new A();shanhy.dispaly();// 其它处理代码.return a;}}


使用@Resource 或者 @Autowired并指定@Qualifier 也可以

package com.example.controller;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.example.demo.Shanhy;@RestControllerpublic class HelloController {@Resource(name = "shanhyA")private Shanhy shanhyA;@Autowired@Qualifier("shanhyB")private Shanhy shanhyB;@RequestMapping("/test")public String test() {shanhyA.dispaly();shanhyB.dispaly();return "test";}}

访问:http://127.0.0.1:8080/test 查看控制台的打印信息。



0 0