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,我們來看 spring framework 開發(fā)手冊(cè)中的一段話:
Spring 2.5引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。@Component是所有受Spring管理組件的通用形式;而@Repository、@Service和 @Controller則是@Component的細(xì)化,用來表示更具體的用例(例如,分別對(duì)應(yīng)了持久化層、服務(wù)層和表現(xiàn)層)。也就是說,你能用@Component來注解你的組件類,但如果用@Repository、@Service 或@Controller來注解它們,你的類也許能更好地被工具處理,或與切面進(jìn)行關(guān)聯(lián)。例如,這些典型化注解可以成為理想的切入點(diǎn)目標(biāo)。當(dāng)然,在Spring Framework以后的版本中, @Repository、@Service和 @Controller也許還能攜帶更多語義。如此一來,如果你正在考慮服務(wù)層中是該用@Component還是@Service,那@Service顯然是更好的選擇。同樣的,就像前面說的那樣, @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)文章
File的API和常用方法詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了File的API和常用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Java操作XML轉(zhuǎn)JSON數(shù)據(jù)格式詳細(xì)代碼實(shí)例
在Java中我們可以使用一些現(xiàn)成的庫來實(shí)現(xiàn)XML到JSON的轉(zhuǎn)換,下面這篇文章主要給大家介紹了關(guān)于Java操作XML轉(zhuǎn)JSON數(shù)據(jù)格式的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04
springboot使用Hutool的JschUtil及下載安裝步驟
這篇文章主要為大家介紹了springboot使用Hutool的JschUtil的方法及下載安裝詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
遠(yuǎn)程連接Jedis和整合SpringBoot的詳細(xì)過程
這篇文章主要介紹了遠(yuǎn)程連接Jedis和整合SpringBoot的詳細(xì)過程,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Spring?JPA?deleteInBatch導(dǎo)致StackOverflow問題
這篇文章主要介紹了Spring?JPA?deleteInBatch導(dǎo)致StackOverflow問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
SpringBoot整合quartz實(shí)現(xiàn)定時(shí)任務(wù)
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何整合quartz實(shí)現(xiàn)定時(shí)任務(wù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-10-10
minio的下載和springboot整合minio使用方法
本文介紹了如何通過Docker拉取MinIO鏡像,并創(chuàng)建MinIO容器的過程,首先,需要在本地創(chuàng)建/data和/conf兩個(gè)目錄用于掛載MinIO的數(shù)據(jù)和配置文件,接下來,通過docker?run命令啟動(dòng)容器,設(shè)置MinIO的訪問端口、用戶名、密碼等信息,感興趣的朋友一起看看吧2024-09-09

