亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解決netty中spring對(duì)象注入失敗的問(wèn)題

 更新時(shí)間:2022年02月14日 16:38:34   作者:felix飛  
這篇文章主要介紹了解決netty中spring對(duì)象注入失敗的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

netty中spring對(duì)象注入失敗

今天在做項(xiàng)目的時(shí)候發(fā)現(xiàn)在netty中注入service失敗,百度許久后也找不到答案(@Component,@PostConstruct)未起作用,后靈光一現(xiàn)

發(fā)現(xiàn)了問(wèn)題所在

如圖:

這些地方都必須通過(guò)spring注入才能實(shí)現(xiàn)其他依賴注入,之前這里都是采用new的,所以導(dǎo)致spring注入失敗

在netty中注入spring成份

前不久,在Netty中使用到數(shù)據(jù)庫(kù)數(shù)據(jù),由于Netty服務(wù)啟動(dòng)后的上下文與 Spring的上下文不同,所以在Netty中獲取DAO數(shù)據(jù)很頭痛,無(wú)法使用@Autowired注入。

Aware本義就是"自動(dòng)的",顧名思義Spring自動(dòng)做了些事情。在此某些特殊的情況下,Bean需要實(shí)現(xiàn)某個(gè)功能,但該功能必須借助于Spring容器,此時(shí)就必須先獲取Spring容器,然后借助于Spring容器實(shí)現(xiàn)該功能。

為了讓Bean獲取它所在的Spring容器,可以讓該Bean實(shí)現(xiàn)ApplicationContextAware接口。

可以通過(guò)以下方式

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
/**
 * 
 * @author shilei
 *
 * @time 2019年6月19日 下午5:17:50
 *
 * @desc Netty中注入 Spring Autowired
 */
@Component
public class ToolNettySpirngAutowired implements ApplicationContextAware { 
	private static ApplicationContext applicationContext; 
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		if (ToolNettySpirngAutowired.applicationContext == null) {
			ToolNettySpirngAutowired.applicationContext = applicationContext;
		}
	}
 
	// 獲取applicationContext
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}
 
	// 通過(guò)name獲取 Bean.
	public static Object getBean(String name) {
		return getApplicationContext().getBean(name);
	}
 
	// 通過(guò)class獲取Bean.
	public static <T> T getBean(Class<T> clazz) {
		return getApplicationContext().getBean(clazz);
	}
 
	// 通過(guò)name,以及Clazz返回指定的Bean
	public static <T> T getBean(String name, Class<T> clazz) {
		return getApplicationContext().getBean(name, clazz);
	} 
}

在使用時(shí) 可在某業(yè)務(wù)Handler中添加以下代碼:

private static NodeServService nodeServService;?
static {
?? ?nodeServService = ToolNettySpirngAutowired.getBean(NodeServService.class);
}?
private static NodeJpaRepository nodeDao;?
static {
?? ?nodeDao = ToolNettySpirngAutowired.getBean(NodeJpaRepository.class);
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。 

相關(guān)文章

  • Java中面向?qū)ο蟮闹R(shí)點(diǎn)總結(jié)

    Java中面向?qū)ο蟮闹R(shí)點(diǎn)總結(jié)

    Java是一門面向?qū)ο蟮恼Z(yǔ)言。對(duì)象是Java程序中的基本實(shí)體。除了對(duì)象之外Java程序同樣處理基本數(shù)據(jù)。下面這篇文章主要給大家總結(jié)了關(guān)于Java中面向?qū)ο蟮闹R(shí)點(diǎn),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-02-02
  • Spring事務(wù)管理方法步驟解析

    Spring事務(wù)管理方法步驟解析

    這篇文章主要介紹了Spring事務(wù)管理方法步驟解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • IDEA部署JavaWeb項(xiàng)目到Tomcat服務(wù)器的方法

    IDEA部署JavaWeb項(xiàng)目到Tomcat服務(wù)器的方法

    這篇文章主要介紹了IDEA部署JavaWeb項(xiàng)目到Tomcat服務(wù)器的方法,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-06-06
  • 最新評(píng)論