欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

千鋒扣丁學(xué)堂Java培訓(xùn)之Spring動(dòng)態(tài)加載bean后調(diào)用實(shí)現(xiàn)方法解析

2019-08-22 14:43:08 4257瀏覽

今天千鋒扣丁學(xué)堂Java培訓(xùn)老師給大家分享一篇關(guān)于Spring動(dòng)態(tài)加載bean后調(diào)用實(shí)現(xiàn)方法解析的詳細(xì)介紹,首先在使用Spring的過程中,我們有時(shí)候并不想通過xml的方式進(jìn)行bean的注入,希望在不改變?cè)械某绦蚪Y(jié)構(gòu)的基礎(chǔ)上添加我們需要的bean,這個(gè)時(shí)候我們可以利用Spring的spring-beans的jar包里提供的BeanFactoryPostProcessor接口類,通過實(shí)現(xiàn)這個(gè)接口類,我們可以動(dòng)態(tài)的加載所需要的bean,特別是在引入已經(jīng)打包在jar里面的程序而沒有辦法做出修改的情況下,這個(gè)接口就為我們提供了及其方便的入口。



因?yàn)槲沂窃谄渌?xiàng)目的基礎(chǔ)上測(cè)試的這個(gè)接口,所以引入的jar有其特殊性,所以需要參考的同學(xué)可以按照自己的需求來實(shí)現(xiàn)。

1.通過pom.xml來引入springboot:

<parent> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-parent</artifactId> 
  <version>1.5.6.RELEASE</version> 
 </parent> 
 <dependencies> 
  <dependency> 
   <groupId>org.springframework.boot</groupId> 
   <artifactId>spring-boot-starter-test</artifactId> 
  </dependency> 
  <dependency> 
   <groupId>org.springframework.boot</groupId> 
   <artifactId>spring-boot-starter-web</artifactId> 
  </dependency> 
  <dependency> 
   <groupId>com.thread</groupId> 
   <artifactId>test</artifactId> 
   <version>1.0</version> 
   <scope>system</scope> 
   <systemPath>f:/threadTest.jar</systemPath> 
  </dependency> 
 </dependencies>

2.創(chuàng)建App.java啟動(dòng)類:

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
/** 
 * @author liaoyubo 
 * @version 1.0 2017/7/31 
 * @description 
 */
@SpringBootApplication
public class App { 
  
 public static void main(String [] args){ 
  SpringApplication.run(App.class); 
 } 
}

3.創(chuàng)建一個(gè)DynamicCreateBean.java來作為動(dòng)態(tài)加載的對(duì)象:

public class DynamicCreateBean { 
 public void printMethod(){ 
  System.out.println("DynamicCreateBean Success"); 
 } 
}

4.在本項(xiàng)目外另外新增一個(gè)項(xiàng)目打成jar的形式導(dǎo)入到該項(xiàng)目中用于測(cè)試動(dòng)態(tài)加載,在我的項(xiàng)目中新增的是threadTest.jar,該包是一個(gè)用于多線程測(cè)試的類,需要的同學(xué)自己隨便新建一個(gè)來打成jar包測(cè)試即可。

5.新增一個(gè)LoadBean.java類用于動(dòng)態(tài)加載bean:

import org.springframework.beans.BeansException; 
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; 
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 
import org.springframework.beans.factory.support.BeanDefinitionBuilder; 
import org.springframework.beans.factory.support.DefaultListableBeanFactory; 
import org.springframework.stereotype.Component; 
import com.thread.mulitSynThreadTest.Run; 
/** 
 * @author liaoyubo 
 * @version 1.0 2017/8/11 
 * @description 
 */
@Component
public class LoadBean implements BeanFactoryPostProcessor { 
 private DefaultListableBeanFactory defaultListableBeanFactory; 
 @Override
 public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { 
  this.defaultListableBeanFactory = (DefaultListableBeanFactory)configurableListableBeanFactory; 
  BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition("com.springRedis.dynamic.DynamicCreateBean"); 
  //用于設(shè)置指定的類中需要引入的其他bean 
  //beanDefinitionBuilder.addPropertyValue("otherBeanName","otherBeanName"); 
  this.defaultListableBeanFactory.registerBeanDefinition("dynamicCreateBean",beanDefinitionBuilder.getBeanDefinition()); 
  beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Run.class.getName()); 
  //用于設(shè)置指定的類中需要引入的其他bean 
  //beanDefinitionBuilder.addPropertyValue("otherBeanName","otherBeanName"); 
  this.defaultListableBeanFactory.registerBeanDefinition("mulitRun",beanDefinitionBuilder.getBeanDefinition()); 
 } 
}

6.創(chuàng)建測(cè)試類:

import com.springRedis.App; 
import com.springRedis.dynamic.DynamicCreateBean; 
import com.thread.mulitSynThreadTest.Run; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.test.context.SpringBootTest; 
import org.springframework.test.context.junit4.SpringRunner; 
/** 
 * @author liaoyubo 
 * @version 1.0 2017/8/11 
 * @description 
 */
@RunWith(SpringRunner.class) 
@SpringBootTest(classes = App.class) 
public class MultiTest { 
 @Autowired
 private DynamicCreateBean dynamicCreateBean; 
 @Autowired
 private Run run; 
 @Test
 public void testDynamic(){ 
  dynamicCreateBean.printMethod(); 
  run.printRun(); 
 } 
}

以上就是整個(gè)的動(dòng)態(tài)加載過程,如果需要了解更多,可以在網(wǎng)上繼續(xù)查找資料。

最近在看springcloudFeign相關(guān)文章時(shí)發(fā)現(xiàn)了另外一種注入動(dòng)態(tài)bean的方式,里面的代碼提供是在FeignClientsRegistrar.java類中

具體代碼為:

private void registerFeignClient(BeanDefinitionRegistry registry, AnnotationMetadata annotationMetadata, Map<String, Object> attributes) { 
  String className = annotationMetadata.getClassName(); 
  BeanDefinitionBuilder definition = BeanDefinitionBuilder.genericBeanDefinition(FeignClientFactoryBean.class); 
  this.validate(attributes); 
  definition.addPropertyValue("url", this.getUrl(attributes)); 
  definition.addPropertyValue("path", this.getPath(attributes)); 
  String name = this.getName(attributes); 
  definition.addPropertyValue("name", name); 
  definition.addPropertyValue("type", className); 
  definition.addPropertyValue("decode404", attributes.get("decode404")); 
  definition.addPropertyValue("fallback", attributes.get("fallback")); 
  definition.addPropertyValue("fallbackFactory", attributes.get("fallbackFactory")); 
  definition.setAutowireMode(2); 
  String alias = name + "FeignClient"; 
  AbstractBeanDefinition beanDefinition = definition.getBeanDefinition(); 
  boolean primary = ((Boolean)attributes.get("primary")).booleanValue(); 
  beanDefinition.setPrimary(primary); 
  String qualifier = this.getQualifier(attributes); 
  if(StringUtils.hasText(qualifier)) { 
   alias = qualifier; 
  } 
  BeanDefinitionHolder holder = new BeanDefinitionHolder(beanDefinition, className, new String[]{alias}); 
  BeanDefinitionReaderUtils.registerBeanDefinition(holder, registry); 
 }

以上就是關(guān)于千鋒扣丁學(xué)堂Java培訓(xùn)之Spring動(dòng)態(tài)加載bean后調(diào)用實(shí)現(xiàn)方法解析的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,想要了解更多關(guān)于Java方面內(nèi)容的小伙伴,請(qǐng)關(guān)注扣丁學(xué)堂Java培訓(xùn)官網(wǎng)、微信等平臺(tái),扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育平臺(tái)為您提供權(quán)威的Java開發(fā)視頻,Java培訓(xùn)后的前景無(wú)限,行業(yè)薪資和未來的發(fā)展會(huì)越來越好的,扣丁學(xué)堂老師精心推出的Java視頻教程定能讓你快速掌握J(rèn)ava從入門到精通開發(fā)實(shí)戰(zhàn)技能??鄱W(xué)堂Java技術(shù)交流群:850353792。


                          JavaEE/微服務(wù)/源碼解析/分布式/企業(yè)級(jí)架構(gòu)【VIP體驗(yàn)課】


     【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】        【掃碼進(jìn)入JavaEE/微服務(wù)VIP免費(fèi)公開課】  



查看更多關(guān)于“Java開發(fā)資訊”的相關(guān)文章>>

標(biāo)簽: Java培訓(xùn) Java視頻教程 Java多線程 Java面試題 Java學(xué)習(xí)視頻 springBoot項(xiàng)目

熱門專區(qū)

暫無(wú)熱門資訊

課程推薦

微信
微博
15311698296

全國(guó)免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號(hào)中關(guān)村智誠(chéng)科創(chuàng)大廈4層
京ICP備2021002079號(hào)-2   Copyright ? 2017 - 2022
返回頂部 返回頂部