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

服務(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如何動態(tài)創(chuàng)建Bean示例代碼

Spring Boot如何動態(tài)創(chuàng)建Bean示例代碼

2020-12-30 11:13西夏一品堂 Java教程

這篇文章主要給大家介紹了關(guān)于Spring Boot如何動態(tài)創(chuàng)建Bean的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

前言

本文主要給大家介紹了關(guān)于Spring Boot動態(tài)創(chuàng)建Bean的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。

SpringBoot測試版本:1.3.4.RELEASE

參考代碼如下:

?
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
package com.spring.configuration;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
 
@Configuration
/**
 * 這里的conditional是一個可選條件,表示當(dāng)這個表達(dá)式為true的時候,才動態(tài)創(chuàng)建bean
 */
@ConditionalOnExpression("${my.configuration.enabled}")
public class DynamicConfiguration
{
 @Autowired
 private ApplicationContext applicationContext;
  
 /**
  * 這個方法返回Runnable只是一個幌子,最重要的是執(zhí)行方法里面的代碼
  */
 @Bean
 public Runnable dynamicConfiguration() throws Exception
 {
  ConfigurableApplicationContext context = (ConfigurableApplicationContext)applicationContext;
  DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)context.getBeanFactory();
   
  BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(UserService.class);
  /**
   * 設(shè)置屬性
   */
  beanDefinitionBuilder.addPropertyValue("name", "myConfigure");
  beanDefinitionBuilder.addPropertyValue("jdbcTemplate", applicationContext.getBean(JdbcTemplate.class));
   
  /**
   * 注冊到spring容器中
   */
  beanFactory.registerBeanDefinition("userService", beanDefinitionBuilder.getBeanDefinition());
  return null;
 }
}
class UserService
{
 private String name;
 private JdbcTemplate jdbcTemplate;
 public String getName()
 {
  return name;
 }
 public void setName(String name)
 {
  this.name = name;
 }
 public JdbcTemplate getJdbcTemplate()
 {
  return jdbcTemplate;
 }
 public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
 {
  this.jdbcTemplate = jdbcTemplate;
 }
}

之后,就可以使用如下方式獲取對象了

?
1
2
applicationContext.getBean(UserService.class);
applicationContext.getBean("userService", UserService.class)

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。

原文鏈接:http://blog.csdn.net/mn960mn/article/details/51352007

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国内精品久久久久久不卡影院 | 欧美精品1区| 武侠艳妇屈辱的张开双腿 | 草莓丝瓜芭乐樱桃榴莲色多黄 | 国产区综合另类亚洲欧美 | yy8090韩国日本三理论免费 | 欧美一级艳片视频免费观看 | 日本高清在线精品一区二区三区 | 性xxxx直播放免费 | eeuss免费快捷 | 福利视频一区二区思瑞 | 情侣宾馆愉拍自拍视频 | 女子张腿让男人桶免费 | 国产一区二区三区欧美精品 | 调教老师肉色丝袜的故事 | 国产成人一区二区三区视频免费蜜 | 色老板影视 | 成人网址大全 | 国产高清在线看 | 色婷婷网 | 91人人| 青青青手机视频在线观看 | 污小说免费 | 久久热这里只有 精品 | 波多野结中文字幕在线69视频 | 九二淫黄大片看片 | 欧美夫妇野外交换hd高清版 | 天天拍天天色 | 欧美cosplay极品video | 饭冈加奈子黑人解禁在线播放 | xxx88视频在线观看 | 亚洲精品第二页 | 大团圆免费阅读全文 | 天堂樱桃bt在线www | 暖暖 免费 高清 日本 中文 | 欧美在线播放成人免费 | 色久激情 | 色久久一个亚洲综合网 | 女bbbbxxx孕妇 | 动漫美女日批 | 91免费在线 |