首页 > 编程语言 >41. Spring Boot 使用Java代码创建Bean并注册到Spring中【从零开始学Spring Boot】

41. Spring Boot 使用Java代码创建Bean并注册到Spring中【从零开始学Spring Boot】

时间:2023-07-03 14:32:57浏览次数:39  
标签:importorg Java Spring Boot springframework Bean kfit com



已经好久没有讲一些基础的知识了,这一小节来点简单的,这也是为下节的在Spring多数Boot中使用多数据源做准备。

从Spring 3.0开始,增加了一种新的途径来配置Bean Definition,这就是通过Java Code配置Bean Definition。

       与XML和Annotation两种配置方式不同点在于:

       前两种方式XML和Annotation的配置方式为预定义方式,即开发人员通过XML文件或者Annotation预定义配置Bean的各种属性后,启动Spring容器,Spring容器会首先解析这些配置属性,生成对应的Bean Definition,装入到DefaultListtableBeanFactory对象的属性容器中,以此同时,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 指定注入对应的实例。

 

接口com.kfit.demo.Shanhy.java
package com.kfit.demo;
 
publicinterface Shanhy {
     
     publicvoid dispaly();
     
}
 
实现类com.kfit.demo.ShanhyA.java
package com.kfit.demo;
 
publicclassShanhyA implements Shanhy{
 
 @Override
     publicvoid dispaly() {
"ShanhyA.dispaly()");
     }
}
 
 
实现类com.kfit.ShanhyB.java
package com.kfit.demo;
 
publicclass ShanhyBimplements Shanhy {
 
 @Override
     publicvoid dispaly() {
         System.out.println("ShanhyB.dispaly()");
     }
 
}
 
 
定义接口BeanDefinitionRegistryPostProcessor的实现:
com.kfit.config.
package com.kfit.config;
 
importorg.springframework.beans.BeansException;
importorg.springframework.beans.MutablePropertyValues;
importorg.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
importorg.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
importorg.springframework.beans.factory.config.BeanDefinition;
importorg.springframework.beans.factory.config.BeanDefinitionHolder;
importorg.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
importorg.springframework.beans.factory.support.BeanDefinitionRegistry;
importorg.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.BeanNameGenerator;
importorg.springframework.context.annotation.AnnotationBeanNameGenerator;
importorg.springframework.context.annotation.Configuration;
 
import com.kfit.demo.ShanhyA;
import com.kfit.demo.ShanhyB;
 
/**
 *实现自己实例化bean并注册为Spring管理

 *@version v.0.1
 */
@Configuration
publicclassMyBeanDefinitionRegistryPostProcessorimplementsBeanDefinitionRegistryPostProcessor {
       
 //bean 的名称生成器.
        private BeanNameGeneratorbeanNameGenerator =newAnnotationBeanNameGenerator();
 
       
 @Override
       publicvoidpostProcessBeanFactory(ConfigurableListableBeanFactorybeanFactory)throws BeansException {
              System.out.println("MyBeanDefinitionRegistryPostProcessor.postProcessBeanFactory()");
              
                            
       }
 
 /**
        *先执行:postProcessBeanDefinitionRegistry()方法,
        *在执行:postProcessBeanFactory()方法。
        *
        */
 @Override
       publicvoidpostProcessBeanDefinitionRegistry(BeanDefinitionRegistryregistry)throws BeansException{
              System.out.println("MyBeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry()");
              
 /*
               *在这里可以注入bean.
               */
registry,"shanyA", ShanhyA.class);
registry,"shanyB", ShanhyB.class);
       }
       
 /**
        *提供公共的注册方法。
        *@param beanDefinitionRegistry
        *@param name
        *@param beanClass
        */
       privatevoidregisterBean(BeanDefinitionRegistryregistry,Stringname,Class<?>beanClass){
annotatedBeanDefinition  =new AnnotatedGenericBeanDefinition(beanClass);
 //可以自动生成name
beanName = (name !=null?name:this.beanNameGenerator.generateBeanName(annotatedBeanDefinition,registry));
 //bean注册的holer类.
beanDefinitionHolder =newBeanDefinitionHolder(annotatedBeanDefinition,beanName);
 //使用bean注册工具类进行注册.
              BeanDefinitionReaderUtils.registerBeanDefinition(beanDefinitionHolder,registry);
       }
       
}
这个类里的代码比较多,在这里简单的介绍下:方法postProcessBeanDefinitionRegistry()是用来注册bean的;而具体注册的代码比较是通用的,我们定义一个私有的方法进行注册。
postProcessBeanFactory()是bean配置的工厂方法,在这个方法中可以获取到我们所有在postProcessBeanDefinitionRegistry方法中注册的所有bean,在这里我们可以进行属性的设置等操作。
//这里可以设置属性,例如
beanDefinition =beanFactory.getBeanDefinition("dataSourceA");
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.kfit.config;
 
import org.kfit.service.HelloService;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import com.kfit.demo.Shanhy;
 
@Configuration
publicclass MyConfig {
       
 /**
        * 这里只是测试使用,没有实际的意义.
        */
 @Bean(name="testHelloService")
       public HelloServicefilterRegistrationBean(@Qualifier("shanhyB") Shanhyshanhy){
helloService =new HelloService();
 shanhy.display();
 // 其它处理代码.
        returnhelloService;
       }
       
}

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

 

package com.kfit.controller;
 
import javax.annotation.Resource;
 
import org.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RestController;
 
import com.kfit.demo.Shanhy;
 
/**
 *
 *@version v.0.1
 */
@RestController
publicclass HelloController {
 
 @Resource(name = "shanhyA")
       private ShanhyshanhyA;
 
 @Autowired
 @Qualifier("shanhyB")
       private ShanhyshanhyB;
 
 @RequestMapping("/test")
       public String test(){
 shanhyA.display();
 shanhyB.display();
              return"test";
       }
}

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

在源代码中由于代码有此系列教程别的章节的代码,请自行忽略阅读,给大家造成的不变请谅解。

 

这里有点经验要说一下,在 @Configuration 中,不能使用注入属性的方式注入,只能通过参数的方式注入,其原因就是@Configuration的类一开始便被加载,此时你想进行属性注入,需要注入的bean对象都还不存在。

 

下面的代码片段也可以注册Bean,比较简单:

@Configuration
@Import(Registrar.class)
publicclassTestConfig {
 
}
 
classRegistrarimplementsImportBeanDefinitionRegistrar {
 
   privatestaticfinal String BEAN_NAME ="myTestBean";
 
    @Override
   publicvoid registerBeanDefinitions(AnnotationMetadataimportingClassMetadata, BeanDefinitionRegistry registry) {
 
       if(!registry.containsBeanDefinition(BEAN_NAME)) {
           GenericBeanDefinition beanDefinition =newGenericBeanDefinition();
           beanDefinition.setBeanClass(ExamplePostProcessor.class);
           beanDefinition.setSynthetic(true);
           registry.registerBeanDefinition(BEAN_NAME, beanDefinition);
        }
    }
 
}


标签:importorg,Java,Spring,Boot,springframework,Bean,kfit,com
From: https://blog.51cto.com/u_11142439/6611265

相关文章

  • 34. Spring Boot的启动器Starter详解【从零开始学Spring Boot】
        SpringBoot应用启动器基本的一共有N(现知道的是44)种:具体如下: 1)spring-boot-starter这是SpringBoot的核心启动器,包含了自动配置、日志和YAML。2)spring-boot-starter-actuator帮助监控和管理应用。3)spring-boot-starter-amqp通过spring-rabbit来支持AMQP协议(AdvancedMess......
  • 32. Spring Boot使用@SpringBootApplication注解【从零开始学Spring Boot】
     如果看了我之前的文章,这个节你就可以忽略了,这个是针对一些刚入门的选手存在的困惑进行写的一篇文章。很多SpringBoot开发者总是使用@Configuration,@EnableAutoConfiguration和@ComponentScan注解他们的main类。由于这些注解被如此频繁地一块使用(特别是你遵循以上最佳实践时),S......
  • 29. Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】
    文件上传主要分以下几个步骤:(1)新建mavenjavaproject;(2)在pom.xml加入相应依赖;(3)新建一个表单页面(这里使用thymeleaf);(4)编写controller;(5)测试;(6)对上传的文件做一些限制;(7)多文件上传实现(1)新建mavenjavaproject新建一个名称为spring-boot-fileuploadmavenjava项目;(2)在pom.xml......
  • springboot下的@NotBlank,@NotNull,@NotEmpty
    话不多说1.三个注解区别@NotBlank只能作用在String上,不能为null,而且调用trim()后,长度必须大于0(不能为空格)@NotNull不能为null,但可以为空字符串,校验Integer类型不能为空@NotEmpty不能为null,并且长度必须大于0,校验List类型不能为空2.依赖引入`<dependency> ......
  • 郑州Java面试题
    常见题库https://github.com/cosen1024/Java-Interviewhttps://github.com/whx123/JavaHomehttps://github.com/lvminghui/Java-Noteshttps://github.com/cosen1024/Java-Interview==和equals区别==基本类型比较的是值,对象类型比较的是地址,equals默认情况下也是比较地址......
  • JAVA生成xml文件格式
    publicboolean A(参数1,……){Documentdocument=DocumentHelper.createDocument();Namespacena=Namespace.get("");Strings=null;na=new Namespace(xxxxxxxxxxxxxxxxxxxxx);//命名空间Elementroot=document.addElement(newQName(“A......
  • java 相对路径问题 和绝对路径
    小例:java代码:都可以成功Filefile=newFile("./xml/a.properties");Filefile=newFile("xml/a.properties");下面就会出错Filefile=newFile("/xml/a.properties"); 总结:.为当前目录,即工程名所在文件夹  下面的当前路径都是你的工程目录Filefile=newFile("./......
  • 关于Java RDP协议实现远程桌面连接的开源项目properjavardp
    最近想学一下在Android平台上实现RDP协议远程连接PC,于是在网上找这方面的资料,发现了一个开源的JavaRDP项目,很不错,拿出来和大家分享一下。关于properjavardp的一些说明,可以到这里看看:http://properjavardp.sourceforge.net/ 。1、首先到http://sourceforge.net/projects/properjav......
  • Apache FtpServer Spring3 整合
    配置运行成功,以备遗忘。Spring配置当中加入 <importresource="applicationFTP.xml"/>在Spring配置同级目录创建 applicationFTP.xml<?xmlversion="1.0"encoding="UTF-8"?><serverxmlns="http://mina.apache.org/ftpserver/spring/v1"  ......
  • JavaCV的摄像头实战之十三:年龄检测
    欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos本篇概览本文是《JavaCV的摄像头实战》系列的第十三篇,前文《JavaCV的摄像头实战之十二:性别检测》中,借助训练好的卷积神经网络模型开发出了识别性别的应用,今天在前文基础......