springboot?serviceImpl初始化注入對象實現(xiàn)方式
springboot初始化serviceImpl注入對象
遇到個需求是:公司是煉鋼相關(guān)的項目,點位數(shù)據(jù)上來后放入kafka中,kafka要對數(shù)據(jù)進行處理,根據(jù)配置的點位值判斷點位是否正常。
經(jīng)歷
看到這個需求,開始是這樣實現(xiàn)的(然后每次修改了配置,就置空一下這個map)
public static Map<String, EnergyInstrumentLogDTO> monitorConfigMap = null //消費方法 private void consumerMsg(Object msg){ ?? ?if(monitorConfigMap == null){ ?? ??? ?//初始化加載 ?? ?} ?? ?//消費消息 }
有個同事看了我寫的,覺得會發(fā)生線程安全問題。仔細一想是覺得有點不對勁,于是乎就修改成了下面這樣
private static Map<String, EnergyInstrumentLogDTO> monitorConfigMap = new HashMap<>(); @Autowired private LogConfigMapper logConfigMapper; private ConsumerPointDataMsg(){ ?? ?initMonitorConfig(); } public void refreshMonitorMap() { ?? ??? ?//此處會調(diào)用數(shù)據(jù)庫日志配置logConfigMapper ? ? ? ? initMonitorConfig(); ?}
這樣就有一個問題了,類構(gòu)造器中初始化時要調(diào)用logConfigMapper,此刻這個mapper的實體是null的。以上就是問題產(chǎn)生的前因,下面繼續(xù)講述后果,直接上干貨。
解決方案
- 使用構(gòu)造器注入方式(可能會引發(fā)A啟動要依賴B,B啟動要依賴A導(dǎo)致報錯–懶加載避免)
- 使用ApplicationContext的bean管理
- 實現(xiàn)InitializingBean接口,重寫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初始化時的執(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); ? ? } } //使用時 private void initMonitorConfig(){ ?? ?LogConfigMapper mapper = ApplicationContextProvider.getBean(LogConfigMapper.class); }
此種方案不太建議使用,因為有點開掛的感覺,直接到spring容器中去獲取bean
3.重寫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無法自動注入bean問題
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需要無法找到類型為“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!"; } }
報錯:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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ù)英文的提示是在配置中找不到一個指定自動注入類型的bean,
網(wǎng)上搜索得知:
- SpringBoot項目的Bean裝配默認規(guī)則是根據(jù)Application類所在的包位置從上往下掃描!
- “Application類”是指SpringBoot項目入口類。這個類的位置很關(guān)鍵:
- 如果Application類所在的包為:com.boot.app,則只會掃描com.boot.app包及其所有子包,如果service或dao所在包不在com.boot.app及其子包下,則不會被掃描!
- 即, 把Application類放到dao、service所在包的上級,com.boot.Application
- 知道這一點非常關(guān)鍵,不知道Spring文檔里有沒有給出說明,如果不知道還真是無從解決。
經(jīng)過多方排查得出結(jié)論:
正常情況下加上@Component注解的類會自動被Spring掃描到生成Bean注冊到spring容器中,既然他說沒找到,也就是該注解被沒有被spring識別,問題的核心關(guān)鍵就在application類的注解SpringBootApplication上,
這個注解其實相當(dāng)于下面這一堆注解的效果,其中一個注解就是@Component,在默認情況下只能掃描與控制器在同一個包下以及其子包下的@Component注解,以及能將指定注解的類自動注冊為Bean的@Service@Controller和@ Repository,至此明白問題所在,之前我將接口與對應(yīng)實現(xiàn)類放在了與控制器所在包的同一級目錄下,這樣的注解自然是無法被識別的。
之前我的controller和service是在同級目錄下面現(xiàn)把controller放在service的上級目錄,再啟動就不報錯了。
至此,得出兩種解決辦法
1.將接口與對應(yīng)的實現(xiàn)類放在與application啟動類的同一個目錄或者他的子目錄下,這樣注解可以被掃描到,這是最省事的辦法
2.在指定的application類上加上這么一行注解,手動指定application類要掃描哪些包下的注解,見下圖
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot 如何使用 SaToken 進行登錄認證、權(quán)限管理及路由規(guī)則接口攔截
Sa-Token 是一個輕量級 Java 權(quán)限認證框架,主要解決:登錄認證、權(quán)限認證、單點登錄、OAuth2.0、分布式Session會話、微服務(wù)網(wǎng)關(guān)鑒權(quán) 等一系列權(quán)限相關(guān)問題,這篇文章主要介紹了Springboot 使用 SaToken 進行登錄認證、權(quán)限管理以及路由規(guī)則接口攔截,需要的朋友可以參考下2024-06-06redis 使用lettuce 啟動內(nèi)存泄漏錯誤的解決方案
這篇文章主要介紹了redis 使用lettuce 啟動內(nèi)存泄漏錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04SpringBoot整合Elasticsearch7.2.0的實現(xiàn)方法
這篇文章主要介紹了SpringBoot整合Elasticsearch7.2.0的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08springboot的logging.group日志分組方法源碼流程解析
這篇文章主要為大家介紹了springboot的logging.group日志分組方法源碼流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12springboot+redis分布式鎖實現(xiàn)模擬搶單
這篇文章主要介紹了springboot+redis分布式鎖實現(xiàn)模擬搶單,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03