springboot?serviceImpl初始化注入對(duì)象實(shí)現(xiàn)方式
springboot初始化serviceImpl注入對(duì)象
遇到個(gè)需求是:公司是煉鋼相關(guān)的項(xiàng)目,點(diǎn)位數(shù)據(jù)上來后放入kafka中,kafka要對(duì)數(shù)據(jù)進(jìn)行處理,根據(jù)配置的點(diǎn)位值判斷點(diǎn)位是否正常。
經(jīng)歷
看到這個(gè)需求,開始是這樣實(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è)同事看了我寫的,覺得會(huì)發(fā)生線程安全問題。仔細(xì)一想是覺得有點(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è)問題了,類構(gòu)造器中初始化時(shí)要調(diào)用logConfigMapper,此刻這個(gè)mapper的實(shí)體是null的。以上就是問題產(chǎn)生的前因,下面繼續(xù)講述后果,直接上干貨。
解決方案
- 使用構(gòu)造器注入方式(可能會(huì)引發(fā)A啟動(dòng)要依賴B,B啟動(dòng)要依賴A導(dǎo)致報(bào)錯(cuò)–懶加載避免)
- 使用ApplicationContext的bean管理
- 實(shí)現(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初始化時(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)開掛的感覺,直接到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無法自動(dòng)注入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!";
}
}報(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)注入類型的bean,
網(wǎng)上搜索得知:
- SpringBoot項(xiàng)目的Bean裝配默認(rèn)規(guī)則是根據(jù)Application類所在的包位置從上往下掃描!
- “Application類”是指SpringBoot項(xiàng)目入口類。這個(gè)類的位置很關(guān)鍵:
- 如果Application類所在的包為:com.boot.app,則只會(huì)掃描com.boot.app包及其所有子包,如果service或dao所在包不在com.boot.app及其子包下,則不會(huì)被掃描!
- 即, 把Application類放到dao、service所在包的上級(jí),com.boot.Application
- 知道這一點(diǎn)非常關(guān)鍵,不知道Spring文檔里有沒有給出說明,如果不知道還真是無從解決。
經(jīng)過多方排查得出結(jié)論:
正常情況下加上@Component注解的類會(huì)自動(dòng)被Spring掃描到生成Bean注冊(cè)到spring容器中,既然他說沒找到,也就是該注解被沒有被spring識(shí)別,問題的核心關(guān)鍵就在application類的注解SpringBootApplication上,
這個(gè)注解其實(shí)相當(dāng)于下面這一堆注解的效果,其中一個(gè)注解就是@Component,在默認(rèn)情況下只能掃描與控制器在同一個(gè)包下以及其子包下的@Component注解,以及能將指定注解的類自動(dòng)注冊(cè)為Bean的@Service@Controller和@ Repository,至此明白問題所在,之前我將接口與對(duì)應(yīng)實(shí)現(xiàn)類放在了與控制器所在包的同一級(jí)目錄下,這樣的注解自然是無法被識(shí)別的。


之前我的controller和service是在同級(jí)目錄下面現(xiàn)把controller放在service的上級(jí)目錄,再啟動(dòng)就不報(bào)錯(cuò)了。

至此,得出兩種解決辦法
1.將接口與對(duì)應(yīng)的實(shí)現(xiàn)類放在與application啟動(dòng)類的同一個(gè)目錄或者他的子目錄下,這樣注解可以被掃描到,這是最省事的辦法
2.在指定的application類上加上這么一行注解,手動(dòng)指定application類要掃描哪些包下的注解,見下圖

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用dom4j實(shí)現(xiàn)xml轉(zhuǎn)map與xml轉(zhuǎn)json字符串
這篇文章主要為大家詳細(xì)介紹了如何使用dom4j實(shí)現(xiàn)xml轉(zhuǎn)map與xml轉(zhuǎn)json字符串功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-11-11
Java中jakarta.validation數(shù)據(jù)校驗(yàn)幾個(gè)主要依賴包講解
在Java開發(fā)中,BeanValidationAPI提供了一套標(biāo)準(zhǔn)的數(shù)據(jù)驗(yàn)證機(jī)制,尤其是通過JakartaBeanValidation(原HibernateValidator)實(shí)現(xiàn),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09
Java數(shù)據(jù)類型轉(zhuǎn)換的示例詳解
Java程序中要求參與的計(jì)算的數(shù)據(jù),必須要保證數(shù)據(jù)類型的一致性,如果數(shù)據(jù)類型不一致將發(fā)生類型的轉(zhuǎn)換。本文將通過示例詳細(xì)說說Java中數(shù)據(jù)類型的轉(zhuǎn)換,感興趣的可以了解一下2022-10-10
Spring @Bean vs @Service注解區(qū)別
本篇文章主要介紹了Spring @Bean vs @Service注解區(qū)別,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
使用Jackson實(shí)現(xiàn)Map與Bean互轉(zhuǎn)方式
這篇文章主要介紹了使用Jackson實(shí)現(xiàn)Map與Bean互轉(zhuǎn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java流程控制之循環(huán)結(jié)構(gòu)for,增強(qiáng)for循環(huán)
這篇文章主要介紹了Java流程控制之循環(huán)結(jié)構(gòu)for,增強(qiáng)for循環(huán),for循環(huán)是編程語言中一種循環(huán)語句,而循環(huán)語句由循環(huán)體及循環(huán)的判定條件兩部分組成,其表達(dá)式為:for(單次表達(dá)式;條件表達(dá)式;末尾循環(huán)體){中間循環(huán)體;},下面我們倆看看文章內(nèi)容的詳細(xì)介紹2021-12-12

