SpringBoot解決@Component無法注入其他Bean的問題
SpringBoot @Component無法注入其他Bean
一、現(xiàn)象
在SpringBoot新new一個(gè)普通類,習(xí)慣性添加@Component讓Spring掃描。
在@Component修飾的類里注入了其他Bean,運(yùn)行時(shí)提示注入的為null
但這個(gè)Bean可以在控制層被引入,在普通類就不行。
二、解決
找了些資料,最后也沒解決注入的問題。
最后的方案就是去掉@Component注解,在new這個(gè)普通類時(shí)從Spring上下文實(shí)例中取出這個(gè)Bean賦給成員變量使用。
弊端:這個(gè)類非單例
三、代碼如下
ApplicationContextProvider
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
/**
* 上下文對象實(shí)例
*/
private static ApplicationContext applicationContext;
@SuppressWarnings("static-access")
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
/**
* 獲取applicationContext
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通過name獲取 Bean.
*
* @param name
* @return
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* 通過class獲取Bean.
*
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
/**
* 通過name,以及Clazz返回指定的Bean
*
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
IdentityCheckUtil
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpRequest;
import org.apache.log4j.Logger;
public class IdentityCheckUtil {
private Logger logger = Logger.getLogger(IdentityCheckUtil.class);
private UserMapper userMapper;
public IdentityCheckUtil() {
this.userMapper = ApplicationContextProvider.getBean(UserMapper.class);
}
public boolean allowedPass(ChannelHandlerContext ctx, FullHttpRequest fullHttpRequest) {
System.out.println(userMapper);
}
}
@Component注解自動(dòng)注入失效問題
使用@Component聲明 進(jìn)行自動(dòng)注入失效

@Component,在默認(rèn)情況下只能掃描與控制器在同一個(gè)包下以及其子包下的@Component注解,以及能將指定注解的類自動(dòng)注冊為Bean的@Service@Controller和@ Repository,將接口與對應(yīng)實(shí)現(xiàn)類放在了與控制器所在包的同一級目錄下,這樣的注解自然是無法被識別的。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java?Ribbon與openfeign區(qū)別和用法講解
Ribbon是基于Netflix?Ribbon實(shí)現(xiàn)的一套客戶端負(fù)載均衡的工具,主要功能是提供客戶端的軟件負(fù)載均衡算法和服務(wù)調(diào)用。openfeign對Feign進(jìn)行了增強(qiáng),使其支持Spring MVC注解,另外還整合了Ribbon和Nacos,從而使得Feign的使用更加方便2022-08-08
Java 使用Calendar計(jì)算時(shí)間的示例代碼
這篇文章主要介紹了Java 使用Calendar計(jì)算時(shí)間的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
spring boot輸入數(shù)據(jù)校驗(yàn)(validation)的實(shí)現(xiàn)過程
web項(xiàng)目中,用戶的輸入總是被假定不安全不正確的,在被處理前需要做校驗(yàn)。本文介紹在spring boot項(xiàng)目中實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的過程,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-09-09
一次線上websocket返回400問題排查的實(shí)戰(zhàn)記錄
最近項(xiàng)目中有端對端通信場景,實(shí)時(shí)性要求較高,考慮后選用了websocket 這一通信協(xié)議,下面這篇文章主要給大家介紹了一次線上websocket返回400問題排查的實(shí)戰(zhàn)記錄,需要的朋友可以參考下2022-04-04
Zookeeper實(shí)現(xiàn)分布式鎖代碼實(shí)例
這篇文章主要介紹了Zookeeper實(shí)現(xiàn)分布式鎖代碼實(shí)例,Zookeeper?分布式鎖應(yīng)用了其?臨時(shí)順序節(jié)點(diǎn)?的特性,在Zookeeper中創(chuàng)建一個(gè)持久節(jié)點(diǎn)ParentLock,當(dāng)?shù)谝粋€(gè)客戶端要獲取鎖時(shí),在ParentLock節(jié)點(diǎn)下創(chuàng)建一個(gè)臨時(shí)順序節(jié)點(diǎn),需要的朋友可以參考下2023-12-12

