spring配置掃描多個(gè)包問題解析
spring 配置掃描多個(gè)包,有時(shí)候我們希望不同功能類型的包放在不同的包下,這就需要
<!-- 自動(dòng)掃描該包,使 SpringMVC 為包下用了@controller注解的類是控制器 --> <context:component-scan base-package="com.weixiao.ssmcleardb.controller" /> <context:component-scan base-package="com.weixiao.listener" />
有時(shí)候我們可能遇到奇怪的問題,
新建了一個(gè)包,在這個(gè)包下面新建了一個(gè)類,也添加了注解,但啟動(dòng)的時(shí)候就是掃描不到,而其它的類又正常!
這就是你新建的包沒有配置為自動(dòng)掃描的原因。
比如我在 com.weixiao.listener 包下新建的一個(gè)類:
package com.weixiao.listener; import javax.servlet.ServletContext; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.springframework.beans.BeansException; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; import org.springframework.web.context.ServletContextAware; @Component("StartupListener") public class StartupListener implements ApplicationContextAware, ServletContextAware, InitializingBean, ApplicationListener<ContextRefreshedEvent> { protected Logger logger = LogManager.getLogger(getClass()); @Override public void setApplicationContext(ApplicationContext ctx) throws BeansException { logger.info("\r\n\r\n\r\n\r\n1 => StartupListener.setApplicationContext"); } @Override public void setServletContext(ServletContext context) { logger.info("\r\n\r\n\r\n\r\n2 => StartupListener.setServletContext"); } @Override public void afterPropertiesSet() throws Exception { logger.info("\r\n\r\n\r\n\r\n3 => StartupListener.afterPropertiesSet"); } @Override public void onApplicationEvent(ContextRefreshedEvent event) { logger.info("\r\n\r\n\r\n\r\n4.1 => MyApplicationListener.onApplicationEvent"); logger.info("\r\n\r\n\r\n\r\n4.1 => " + event.getApplicationContext().getParent()); logger.info("\r\n\r\n\r\n\r\n4.1 => " + event.getApplicationContext().getDisplayName()); if (event.getApplicationContext().getParent() == null) { logger.info("\r\n\r\n\r\n\r\n4.2 => MyApplicationListener.onApplicationEvent"); } else{ logger.info("\r\n\r\n\r\n\r\n4.4 => " + event.getApplicationContext().getParent().getDisplayName()); } if (event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")){ logger.info("\r\n\r\n\r\n\r\n4.3 => MyApplicationListener.onApplicationEvent"); } } }
關(guān)于 component-scan,我們來(lái)看 spring framework 開發(fā)手冊(cè)中的一段話:
Spring 2.5引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。@Component是所有受Spring管理組件的通用形式;而@Repository、@Service和 @Controller則是@Component的細(xì)化,用來(lái)表示更具體的用例(例如,分別對(duì)應(yīng)了持久化層、服務(wù)層和表現(xiàn)層)。也就是說(shuō),你能用@Component來(lái)注解你的組件類,但如果用@Repository、@Service 或@Controller來(lái)注解它們,你的類也許能更好地被工具處理,或與切面進(jìn)行關(guān)聯(lián)。例如,這些典型化注解可以成為理想的切入點(diǎn)目標(biāo)。當(dāng)然,在Spring Framework以后的版本中, @Repository、@Service和 @Controller也許還能攜帶更多語(yǔ)義。如此一來(lái),如果你正在考慮服務(wù)層中是該用@Component還是@Service,那@Service顯然是更好的選擇。同樣的,就像前面說(shuō)的那樣, @Repository已經(jīng)能在持久化層中進(jìn)行異常轉(zhuǎn)換時(shí)被作為標(biāo)記使用了?!?
總結(jié)
以上就是本文關(guān)于spring配置掃描多個(gè)包問題解析的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:Spring配置使用之Bean生命周期詳解、淺談Spring的兩種配置容器等,有什么問題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。
相關(guān)文章
Java報(bào)錯(cuò):java.util.concurrent.ExecutionException的解決辦法
在Java并發(fā)編程中,我們經(jīng)常使用java.util.concurrent包提供的工具來(lái)管理和協(xié)調(diào)多個(gè)線程的執(zhí)行,va并發(fā)編程中,然而,在使用這些工具時(shí),可能會(huì)遇到各種各樣的異常,其中之一就是java.util.concurrent.ExecutionException,本文將詳細(xì)分析這種異常的背景、可能的原因2024-09-09springboot controller參數(shù)注入方式
這篇文章主要介紹了springboot controller參數(shù)注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05SpringBoot中多環(huán)境yml的配置與打包問題
這篇文章主要介紹了SpringBoot中多環(huán)境yml的配置與打包問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09SpringMVC @NotNull校驗(yàn)不生效的解決方案
這篇文章主要介紹了SpringMVC @NotNull校驗(yàn)不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09IDEA?code?template配置和參數(shù)方式
這篇文章主要介紹了IDEA?code?template配置和參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教<BR>2024-01-01springboot整合cxf發(fā)布webservice以及調(diào)用的方法
這篇文章主要介紹了springboot整合cxf發(fā)布webservice以及調(diào)用的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-08-08Sprigmvc項(xiàng)目轉(zhuǎn)為springboot的方法
本篇文章主要介紹了Sprigmvc項(xiàng)目轉(zhuǎn)為springboot的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-02-02