欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring配置掃描多個(gè)包問題解析

 更新時(shí)間:2017年10月30日 14:12:22   作者:微wx笑  
這篇文章主要介紹了spring配置掃描多個(gè)包問題解析,具有一定參考價(jià)值,需要的朋友可以了解下。

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)文章

  • SpringBoot2.6.3集成quartz的方式

    SpringBoot2.6.3集成quartz的方式

    quartz是java里頭定時(shí)任務(wù)的經(jīng)典開源實(shí)現(xiàn),這里講述一下如何在SpringBoot2.6.3集成quartz,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2022-02-02
  • springboot框架各個(gè)層次基礎(chǔ)詳解

    springboot框架各個(gè)層次基礎(chǔ)詳解

    這篇文章主要介紹了springboot框架各個(gè)層次基礎(chǔ),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java報(bào)錯(cuò):java.util.concurrent.ExecutionException的解決辦法

    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-09
  • springboot controller參數(shù)注入方式

    springboot controller參數(shù)注入方式

    這篇文章主要介紹了springboot controller參數(shù)注入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • java讀寫串口數(shù)據(jù)你了解多少

    java讀寫串口數(shù)據(jù)你了解多少

    這篇文章主要為大家詳細(xì)介紹了java讀寫串口數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • SpringBoot中多環(huán)境yml的配置與打包問題

    SpringBoot中多環(huán)境yml的配置與打包問題

    這篇文章主要介紹了SpringBoot中多環(huán)境yml的配置與打包問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • SpringMVC @NotNull校驗(yàn)不生效的解決方案

    SpringMVC @NotNull校驗(yàn)不生效的解決方案

    這篇文章主要介紹了SpringMVC @NotNull校驗(yàn)不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • IDEA?code?template配置和參數(shù)方式

    IDEA?code?template配置和參數(shù)方式

    這篇文章主要介紹了IDEA?code?template配置和參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教<BR>
    2024-01-01
  • springboot整合cxf發(fā)布webservice以及調(diào)用的方法

    springboot整合cxf發(fā)布webservice以及調(diào)用的方法

    這篇文章主要介紹了springboot整合cxf發(fā)布webservice以及調(diào)用的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2018-08-08
  • Sprigmvc項(xiàng)目轉(zhuǎn)為springboot的方法

    Sprigmvc項(xiàng)目轉(zhuǎn)為springboot的方法

    本篇文章主要介紹了Sprigmvc項(xiàng)目轉(zhuǎn)為springboot的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2018-02-02

最新評(píng)論