springboot?serviceImpl初始化注入對(duì)象實(shí)現(xiàn)方式
springboot初始化serviceImpl注入對(duì)象
遇到個(gè)需求是:公司是煉鋼相關(guān)的項(xiàng)目,點(diǎn)位數(shù)據(jù)上來(lái)后放入kafka中,kafka要對(duì)數(shù)據(jù)進(jìn)行處理,根據(jù)配置的點(diǎn)位值判斷點(diǎn)位是否正常。
經(jīng)歷
看到這個(gè)需求,開(kāi)始是這樣實(shí)現(xiàn)的(然后每次修改了配置,就置空一下這個(gè)map)
public static Map<String, EnergyInstrumentLogDTO> monitorConfigMap = null //消費(fèi)方法 private void consumerMsg(Object msg){ ?? ?if(monitorConfigMap == null){ ?? ??? ?//初始化加載 ?? ?} ?? ?//消費(fèi)消息 }
有個(gè)同事看了我寫(xiě)的,覺(jué)得會(huì)發(fā)生線程安全問(wèn)題。仔細(xì)一想是覺(jué)得有點(diǎn)不對(duì)勁,于是乎就修改成了下面這樣
private static Map<String, EnergyInstrumentLogDTO> monitorConfigMap = new HashMap<>(); @Autowired private LogConfigMapper logConfigMapper; private ConsumerPointDataMsg(){ ?? ?initMonitorConfig(); } public void refreshMonitorMap() { ?? ??? ?//此處會(huì)調(diào)用數(shù)據(jù)庫(kù)日志配置logConfigMapper ? ? ? ? initMonitorConfig(); ?}
這樣就有一個(gè)問(wèn)題了,類(lèi)構(gòu)造器中初始化時(shí)要調(diào)用logConfigMapper,此刻這個(gè)mapper的實(shí)體是null的。以上就是問(wèn)題產(chǎn)生的前因,下面繼續(xù)講述后果,直接上干貨。
解決方案
- 使用構(gòu)造器注入方式(可能會(huì)引發(fā)A啟動(dòng)要依賴(lài)B,B啟動(dòng)要依賴(lài)A導(dǎo)致報(bào)錯(cuò)–懶加載避免)
- 使用ApplicationContext的bean管理
- 實(shí)現(xiàn)InitializingBean接口,重寫(xiě)afterPropertiesSet方法。該方法是設(shè)置屬性后執(zhí)行
- 構(gòu)造方法注入
1.構(gòu)造器注入
@Component public class PostConstructTest1 { ? ? @Autowired ? ? PostConstructTest2 postConstructTest2; ? ? public PostConstructTest1() { ?? ??? ?//postConstructTest2.test(); ? ? } ? ? @PostConstruct ? ? public void init() { ? ? ? ? // todo sth ? ? } }
Bean初始化時(shí)的執(zhí)行順序: 構(gòu)造方法 -> @Autowired -> @PostConstruct
2.ApplicationContext
@Component public class ApplicationContextProvider implements ApplicationContextAware { ? ? private static ApplicationContext applicationContext; ? ? @Override ? ? public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { ? ? ? ? this.applicationContext = applicationContext; ? ? } ? ? public static ApplicationContext getApplicationContext() { ? ? ? ? return applicationContext; ? ? } ? ? public static Object getBean(String name) { ? ? ? ? return getApplicationContext().getBean(name); ? ? } ? ? public static <T> T getBean(Class<T> clazz) { ? ? ? ? return getApplicationContext().getBean(clazz); ? ? } ? ? public static <T> T getBean(String name, Class<T> clazz) { ? ? ? ? return getApplicationContext().getBean(name, clazz); ? ? } } //使用時(shí) private void initMonitorConfig(){ ?? ?LogConfigMapper mapper = ApplicationContextProvider.getBean(LogConfigMapper.class); }
此種方案不太建議使用,因?yàn)橛悬c(diǎn)開(kāi)掛的感覺(jué),直接到spring容器中去獲取bean
3.重寫(xiě)afterPropertiesSet
@Component @Slf4j public class ConsumerPointDataMsg implements InitializingBean { ?? ?private static Map<String, EnergyInstrumentLogDTO> monitorConfigMap = new HashMap<>(); ?? ?@Autowired ?? ?private LogConfigMapper logConfigMapper; ?? ? @Override ? ? public void afterPropertiesSet() throws Exception { ? ? ? ? initMonitorConfig(); ? ? } }
4. 構(gòu)造方法注入
@Configuration public class XxlJobConfig { ?? ?private final AddressServiceImpl addressService; ?? ?public XxlJobConfig(AddressServiceImpl addressService) { ?? ? ? ? ? ?this.addressService = addressService; ?? ?} ?? ?//直接調(diào)用 ?? ?addressService.serviceUrl("XXL-JOB-ADMIN") }
springboot無(wú)法自動(dòng)注入bean問(wèn)題
Description:
Field demoService in com.spring.web.DemoApplication required a bean of type 'com.spring.service.DemoService' that could not be found.
Action:
Consider defining a bean of type 'com.spring.service.DemoService' in your configuration.
谷歌翻譯為:
com.spring.web.DemoApplication中的field demoService需要無(wú)法找到類(lèi)型為“com.spring.service.DemoService”的bean。
我的代碼:
controller:DemoApplication
package com.spring.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.spring.service.DemoService; @RestController //@EnableAutoConfiguration @SpringBootApplication() public class DemoApplication { @Autowired private DemoService demoService; @RequestMapping("/") public String helloWorld(){ String msg = demoService.demo(); return msg; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
Service:DemoService
package com.spring.service; public interface DemoService { public String demo(); }
ServiceImpl:DemoServiceImpl
package com.spring.service.impl; import org.springframework.stereotype.Service; import com.spring.service.DemoService; @Component public class DemoServiceImpl implements DemoService { @Override public String demo() { System.out.println("Hello World!"); return "Hello World!"; } }
報(bào)錯(cuò):
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.6.RELEASE)
2017-09-11 16:16:10.367 INFO 14512 --- [ main] com.spring.web.DemoApplication : Starting DemoApplication on PC2014101864 with PID 14512 (E:\workspacedubbo\boot\boot-web\target\classes started by Administrator in E:\workspacedubbo\boot\boot-web)
2017-09-11 16:16:10.369 INFO 14512 --- [ main] com.spring.web.DemoApplication : No active profile set, falling back to default profiles: default
2017-09-11 16:16:10.421 INFO 14512 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@31206beb: startup date [Mon Sep 11 16:16:10 CST 2017]; root of context hierarchy
2017-09-11 16:16:11.710 INFO 14512 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-09-11 16:16:11.721 INFO 14512 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-09-11 16:16:11.722 INFO 14512 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.16
2017-09-11 16:16:11.856 INFO 14512 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-09-11 16:16:11.856 INFO 14512 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1438 ms
2017-09-11 16:16:11.992 INFO 14512 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-09-11 16:16:11.995 INFO 14512 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-09-11 16:16:11.995 INFO 14512 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-09-11 16:16:11.995 INFO 14512 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-09-11 16:16:11.995 INFO 14512 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-09-11 16:16:12.025 WARN 14512 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoApplication': Unsatisfied dependency expressed through field 'demoService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.spring.service.DemoService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-09-11 16:16:12.026 INFO 14512 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2017-09-11 16:16:12.038 INFO 14512 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-09-11 16:16:12.107 ERROR 14512 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field demoService in com.spring.web.DemoApplication required a bean of type 'com.spring.service.DemoService' that could not be found.
Action:
Consider defining a bean of type 'com.spring.service.DemoService' in your configuration.
解決辦法
根據(jù)英文的提示是在配置中找不到一個(gè)指定自動(dòng)注入類(lèi)型的bean,
網(wǎng)上搜索得知:
- SpringBoot項(xiàng)目的Bean裝配默認(rèn)規(guī)則是根據(jù)Application類(lèi)所在的包位置從上往下掃描!
- “Application類(lèi)”是指SpringBoot項(xiàng)目入口類(lèi)。這個(gè)類(lèi)的位置很關(guān)鍵:
- 如果Application類(lèi)所在的包為:com.boot.app,則只會(huì)掃描com.boot.app包及其所有子包,如果service或dao所在包不在com.boot.app及其子包下,則不會(huì)被掃描!
- 即, 把Application類(lèi)放到dao、service所在包的上級(jí),com.boot.Application
- 知道這一點(diǎn)非常關(guān)鍵,不知道Spring文檔里有沒(méi)有給出說(shuō)明,如果不知道還真是無(wú)從解決。
經(jīng)過(guò)多方排查得出結(jié)論:
正常情況下加上@Component注解的類(lèi)會(huì)自動(dòng)被Spring掃描到生成Bean注冊(cè)到spring容器中,既然他說(shuō)沒(méi)找到,也就是該注解被沒(méi)有被spring識(shí)別,問(wèn)題的核心關(guān)鍵就在application類(lèi)的注解SpringBootApplication上,
這個(gè)注解其實(shí)相當(dāng)于下面這一堆注解的效果,其中一個(gè)注解就是@Component,在默認(rèn)情況下只能掃描與控制器在同一個(gè)包下以及其子包下的@Component注解,以及能將指定注解的類(lèi)自動(dòng)注冊(cè)為Bean的@Service@Controller和@ Repository,至此明白問(wèn)題所在,之前我將接口與對(duì)應(yīng)實(shí)現(xiàn)類(lèi)放在了與控制器所在包的同一級(jí)目錄下,這樣的注解自然是無(wú)法被識(shí)別的。
之前我的controller和service是在同級(jí)目錄下面現(xiàn)把controller放在service的上級(jí)目錄,再啟動(dòng)就不報(bào)錯(cuò)了。
至此,得出兩種解決辦法
1.將接口與對(duì)應(yīng)的實(shí)現(xiàn)類(lèi)放在與application啟動(dòng)類(lèi)的同一個(gè)目錄或者他的子目錄下,這樣注解可以被掃描到,這是最省事的辦法
2.在指定的application類(lèi)上加上這么一行注解,手動(dòng)指定application類(lèi)要掃描哪些包下的注解,見(jiàn)下圖
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
接口隔離原則_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了接口隔離原則,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08Springboot 如何使用 SaToken 進(jìn)行登錄認(rèn)證、權(quán)限管理及路由規(guī)則接口攔截
Sa-Token 是一個(gè)輕量級(jí) Java 權(quán)限認(rèn)證框架,主要解決:登錄認(rèn)證、權(quán)限認(rèn)證、單點(diǎn)登錄、OAuth2.0、分布式Session會(huì)話(huà)、微服務(wù)網(wǎng)關(guān)鑒權(quán) 等一系列權(quán)限相關(guān)問(wèn)題,這篇文章主要介紹了Springboot 使用 SaToken 進(jìn)行登錄認(rèn)證、權(quán)限管理以及路由規(guī)則接口攔截,需要的朋友可以參考下2024-06-06redis 使用lettuce 啟動(dòng)內(nèi)存泄漏錯(cuò)誤的解決方案
這篇文章主要介紹了redis 使用lettuce 啟動(dòng)內(nèi)存泄漏錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04SpringBoot整合Elasticsearch7.2.0的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot整合Elasticsearch7.2.0的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08springboot的logging.group日志分組方法源碼流程解析
這篇文章主要為大家介紹了springboot的logging.group日志分組方法源碼流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12springboot+redis分布式鎖實(shí)現(xiàn)模擬搶單
這篇文章主要介紹了springboot+redis分布式鎖實(shí)現(xiàn)模擬搶單,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03java中Class類(lèi)的基礎(chǔ)知識(shí)點(diǎn)及實(shí)例
在本篇文章里小編給大家分享了關(guān)于java中Class類(lèi)的基礎(chǔ)知識(shí)點(diǎn)及實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-05-05