Spring運(yùn)行時(shí)手動(dòng)注入bean的方法實(shí)例
有時(shí)候,會(huì)有這樣一個(gè)需求,在程序運(yùn)行時(shí)動(dòng)態(tài)生成的對(duì)象,需要注入到Spring容器中進(jìn)行管理。
下面是獲取Bean以及注入Bean的工具類
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.Map;
@Component
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtils.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
public static Object getBean(Class name) throws BeansException {
return applicationContext.getBean(name);
}
/**
* bean注入spring容器
* map[id,className,...]
* id 為 bean的標(biāo)識(shí)
* className 為 類的全限定類名
* ... 為其他屬性
* @param map
*/
public static void injectBean(Map<String, String> map) {
String className = map.get("className");
Class<?> aClass;
if (className == null) {
throw new RuntimeException("map參數(shù)缺少className");
}
try {
aClass = Class.forName(className);
if (aClass == null) {
throw new RuntimeException("className不存在");
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(aClass);
Field[] declaredFields = aClass.getDeclaredFields();
for (int i = 0; i < declaredFields.length; i++) {
String fieldName = declaredFields[i].getName();
if (map.get(fieldName) != null) {
// 必須設(shè)置可訪問,否則下面的操作做不了
// declaredFields[i].setAccessible(true);
builder.addPropertyValue(fieldName, map.get(fieldName));
}
}
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) applicationContext;
// 注冊(cè)bean 第一個(gè)參數(shù)為 name ,第二個(gè)為 bean定義類
String id = map.get("id");
if (id == null) {
registry.registerBeanDefinition(className, builder.getBeanDefinition());
return;
}
registry.registerBeanDefinition(id, builder.getBeanDefinition());
}
}測試
@Test
public void test2() {
HashMap<String, String> map = new HashMap<>();
map.put("id", "helloTask");
map.put("className", "com.example.demo.Task.HelloTask4");
// 注冊(cè)bean
SpringUtils.injectBean(map);
// HelloTask4 helloTask =(HelloTask4) SpringUtils.getBean(HelloTask4.class);
HelloTask4 helloTask = (HelloTask4) SpringUtils.getBean("helloTask");
System.out.println(helloTask.getClass());
}
附:利用注解向Spring容器中注入Bean
常用注解包含:@Controller、@Service、@Repository、@Component,其中@Controller、@Service、@Repository都是基于@Component的擴(kuò)展。通常的@Controller用于標(biāo)識(shí)處理前端請(qǐng)求的類,@Service用于標(biāo)識(shí)業(yè)務(wù)邏輯類,@Repository用于標(biāo)識(shí)DAO層的類,@Component為通用注解,可以標(biāo)注在任何想要注入容器中的類上面;
@Component
//@Controller
//@Service
//@Repository
class Stu{
}也可以利用@Bean與@Configuration注入,其中@Configuration用于標(biāo)注一個(gè)配置類,@Bean用于標(biāo)注返回需注入Bean的方法;
@Configuration
class MyConfig{
@Bean
public Student getStudent(){
return new Student();
}
}總結(jié)
到此這篇關(guān)于Spring運(yùn)行時(shí)手動(dòng)注入bean的文章就介紹到這了,更多相關(guān)Spring運(yùn)行手動(dòng)注入bean內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 17 隨機(jī)數(shù)生成器來了一波穩(wěn)穩(wěn)的增強(qiáng)
JDK 當(dāng)中的隨機(jī)數(shù)生成器其實(shí)對(duì)于普通開發(fā)者來講基本夠用,不過對(duì)于一些比較復(fù)雜的場景來講,原有的類結(jié)構(gòu)對(duì)擴(kuò)展并不是很友好,除了 Random 類,JDK 當(dāng)中還提供了另外幾個(gè)隨機(jī)數(shù)的成員,下面文章將詳細(xì)介紹,需要的朋友可以參考一下2021-09-09
SpringBoot集成Sharding-JDBC實(shí)現(xiàn)分庫分表方式
這篇文章主要介紹了SpringBoot集成Sharding-JDBC實(shí)現(xiàn)分庫分表方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Java的無參構(gòu)造函數(shù)用法實(shí)例分析
這篇文章主要介紹了Java的無參構(gòu)造函數(shù)用法,結(jié)合實(shí)例形式分析了java無參構(gòu)造函數(shù)基本原理、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-09-09
Java生成獨(dú)一無二的工單號(hào)實(shí)例
這篇文章主要介紹了Java生成獨(dú)一無二的工單號(hào)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-09-09

