一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 詳解Spring Boot 使用Java代碼創(chuàng)建Bean并注冊到Spring中

詳解Spring Boot 使用Java代碼創(chuàng)建Bean并注冊到Spring中

2020-08-18 11:35catoop Java教程

本篇介紹了Spring Boot 使用Java代碼創(chuàng)建Bean并注冊到Spring中,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

從 Spring3.0 開始,增加了一種新的途經(jīng)來配置Bean Definition,這就是通過 Java Code 配置 Bean Definition。

與Xml和Annotation兩種配置方式不同點(diǎn)在于:

前兩種Xml和Annotation的配置方式為預(yù)定義方式,即開發(fā)人員通過 XML 文件或者 Annotation 預(yù)定義配置 bean 的各種屬性后,啟動 spring 容器,Spring 容器會首先解析這些配置屬性,生成對應(yīng)都?Bean Definition,裝入到 DefaultListableBeanFactory 對象的屬性容器中去。與此同時(shí),Spring 框架也會定義一些內(nèi)部使用的 Bean 定義,如 bean 名為”org.springframework.context.annotation.internalConfigurationAnnotationProcessor”的 ConfigurationClassPostProcessor 定義。

而后此刻不會做任何 Bean Definition 的定義解析動作,Spring 框架會根據(jù)前兩種配置,過濾出 BeanDefinitionRegistryPostProcessor 類型的 Bean 定義,并通過 Spring 框架生成其對應(yīng)的 Bean 對象(如 ConfigurationClassPostProcessor 實(shí)例)。結(jié)合 Spring 上下文源碼可知這個(gè)對象是一個(gè) processor 類型工具類,Spring 容器會在實(shí)例化開發(fā)人員所定義的 Bean 前先調(diào)用該 processor 的 postProcessBeanDefinitionRegistry(…) 方法。此處實(shí)現(xiàn)基于 Java Code 配置Bean Definition的處理。

基于 Java Code 解析 Bean 的順序圖(查看大圖)

詳解Spring Boot 使用Java代碼創(chuàng)建Bean并注冊到Spring中

基于 Java Code 的配置方式,其執(zhí)行原理不同于前兩種。它是在 Spring 框架已經(jīng)解析了基于 XML 和 Annotation 配置后,通過加入 BeanDefinitionRegistryPostProcessor 類型的 processor 來處理配置信息,讓開發(fā)人員通過 Java 編程方式定義一個(gè) Java 對象。其優(yōu)點(diǎn)在于可以將配置信息集中在一定數(shù)量的 Java 對象中,同時(shí)通過 Java 編程方式,比基于 Annotation 方式具有更高的靈活性。并且該配置方式給開發(fā)人員提供了一種非常好的范例來增加用戶自定義的解析工具類。其主要缺點(diǎn)在于與 Java 代碼結(jié)合緊密,配置信息的改變需要重新編譯 Java 代碼,另外這是一種新引入的解析方式,需要一定的學(xué)習(xí)成本。

另外提及一點(diǎn)的就是,Spring框架有3個(gè)主要的Hook類,分別是:

?
1
org.springframework.context.ApplicationContextAware

它的setApplicationContext 方法將在Spring啟動之前第一個(gè)被調(diào)用。我們用來同時(shí)啟動Jdon框架。

?
1
org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor

它的postProcessBeanDefinitionRegistry 和 postProcessBeanFactory 方法是第二和第三被調(diào)用,它們在Bean初始化創(chuàng)建之前啟動,如果Spring的bean需要的其他第三方中的組件,我們在這里將其注入給Spring。

?
1
org.springframework.context.ApplicationListener

用于在初始化完成后做一些事情,當(dāng)Spring所有XML或元注解的Bean都啟動被創(chuàng)建成功了,這時(shí)會調(diào)用它的唯一方法onApplicationEvent。

下面我們來完成一個(gè),自己通過java代碼創(chuàng)建bean,并注冊為Spring管理。

本例中,我們創(chuàng)建一個(gè)接口,然后創(chuàng)建該接口的2個(gè)實(shí)現(xiàn)類,分別命名不同的名字,然后在需要注入的地方使用@Qualifier 指定注入對應(yīng)的實(shí)例。

1、接口Shanhy.java

?
1
2
3
4
5
6
7
package org.springboot.sample.config;
 
public interface Shanhy {
 
  void display();
 
}

2、實(shí)現(xiàn)類ShanhyA.java

?
1
2
3
4
5
6
7
8
9
10
package org.springboot.sample.config;
 
public class ShanhyA implements Shanhy {
 
  @Override
  public void display() {
    System.out.println("AAAAAAAAAAAA");
  }
 
}

3、實(shí)現(xiàn)類ShanhyB.java

?
1
2
3
4
5
6
7
8
9
10
package org.springboot.sample.config;
 
public class ShanhyB implements Shanhy {
 
  @Override
  public void display() {
    System.out.println("BBBBBBBBBBBB");
  }
 
}

4、定義接口BeanDefinitionRegistryPostProcessor的實(shí)現(xiàn)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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;
 
/**
 * 實(shí)現(xiàn)自己實(shí)例化bean并注冊為Spring管理
 *
 * @author  單紅宇(365384722)
 * @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");
    // 這里可以設(shè)置屬性,例如
    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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * 測試參數(shù)注入
 *
 * @author  單紅宇(365384722)
 * @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 也可以

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@RestController
@RequestMapping("/hello")
public class HelloController {
 
  @Resource(name="shanhyA")
  private Shanhy shanhyA;
 
  @Autowired
  @Qualifier("shanhyB")
  private Shanhy shanhyB;
 
  // 省略代碼
 
}

這里有點(diǎn)經(jīng)驗(yàn)要說一下,在 @Configuration 中,不能使用注入屬性的方式注入,只能通過參數(shù)的方式注入,其原因就是@Configuration的類一開始變被加載,此時(shí)你想進(jìn)行屬性注入,需要注入的bean對象都還不存在呢。

下一篇文章,我們將使用這種方法動態(tài)創(chuàng)建基于MyBatis的多數(shù)據(jù)源。

下面的代碼片段也可以注冊Bean,比較簡單:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@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);
    }
  }
 
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/catoop/article/details/50558333

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人福利免费视频 | 无码AV毛片色欲欧洲美洲 | 91庥豆果冻天美精东蜜桃传媒 | 天堂网www在线中文天堂 | 日本高清在线观看天码888 | 久草高清在线 | 午夜久| 天天做天天爽天天谢 | 国产一区二区三区在线看片 | 奇米网狠狠网 | poverty中国老妇人 | 国产精品视频2020 | 无人区在线观看免费国语完整版 | 911爱豆传媒国产 | 日韩成本大片35分钟免费播放 | 四虎影院最新网址 | 亚洲女人国产香蕉久久精品 | 毛片在线免费观看网站 | 操闺蜜| 亚洲精品视频在线 | 校花在公车上被内射好舒 | 香蕉tv国产在线永久播放 | 亚洲国内精品久久 | 99久久爱热6在线播放 | 免费370理论片中文字幕 | 我与肥熟老妇的性事 | 色香视频在线 | 日本剧情片在线播放中文版 | 久久黄色小视频 | 亚洲精品老司机福利在线播放 | 日本欧美大码a在线视频播放 | 被夫上司强迫中文 | 网址在线观看你懂我意思吧免费的 | 高h短篇校园1v1 | 国产精品亚洲片夜色在线 | 四虎精品视频在线永久免费观看 | 四虎成人永久地址 | 亚洲人的天堂男人爽爽爽 | 亚洲国产高清一区二区三区 | 99热这里只有精品在线 | 国产精品一级视频 |