激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(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中,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

從 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 對象的屬性容器中去。與此同時,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 上下文源碼可知這個對象是一個 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 編程方式定義一個 Java 對象。其優(yōu)點(diǎn)在于可以將配置信息集中在一定數(shù)量的 Java 對象中,同時通過 Java 編程方式,比基于 Annotation 方式具有更高的靈活性。并且該配置方式給開發(fā)人員提供了一種非常好的范例來增加用戶自定義的解析工具類。其主要缺點(diǎn)在于與 Java 代碼結(jié)合緊密,配置信息的改變需要重新編譯 Java 代碼,另外這是一種新引入的解析方式,需要一定的學(xué)習(xí)成本。

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

?
1
org.springframework.context.ApplicationContextAware

它的setApplicationContext 方法將在Spring啟動之前第一個被調(diào)用。我們用來同時啟動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)建成功了,這時會調(diào)用它的唯一方法onApplicationEvent。

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

本例中,我們創(chuàng)建一個接口,然后創(chuàng)建該接口的2個實(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的類一開始變被加載,此時你想進(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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色戒在线版| 视频一区 中文字幕 | 欧美一级网址 | 日韩aⅴ一区二区三区 | 史上最强炼体老祖动漫在线观看 | 国产一区二区在线免费 | av免费提供 | 午夜在线视频观看 | 毛片在线免费视频 | 叉逼视频 | 中文字幕1区2区 | 久久久精品精品 | 欧美精品亚洲人成在线观看 | 久久福利在线 | 狠狠干导航 | 欧美一级黄色片在线观看 | 永久在线观看电影 | 一级做受毛片免费大片 | 黄色av网站在线观看 | 天堂亚洲一区 | 斗罗破苍穹在线观看免费完整观看 | 91成| 欧美日韩电影 | 97超视频在线观看 | 最新中文字幕第一页视频 | 一级黄色淫片 | 精品成人国产在线观看男人呻吟 | 欧美日韩爱爱视频 | 久久久精品视频免费 | 欧美亚洲国产成人 | 1314av| 青青草免费观看完整版高清 | 国产成人精品网站 | 欧美女人天堂 | 久久艹综合 | 欧美黄色一级带 | 国产一级淫片在线观看 | 久久综合网址 | 国产精品av久久久久久久久久 | 精品国产一区二区三区成人影院 | 高清av免费 |