spring 配置掃描多個包,有時候我們希望不同功能類型的包放在不同的包下,這就需要
1
2
3
|
<!-- 自動掃描該包,使 SpringMVC 為包下用了@controller注解的類是控制器 --> < context:component-scan base-package = "com.weixiao.ssmcleardb.controller" /> < context:component-scan base-package = "com.weixiao.listener" /> |
有時候我們可能遇到奇怪的問題,
新建了一個包,在這個包下面新建了一個類,也添加了注解,但啟動的時候就是掃描不到,而其它的類又正常!
這就是你新建的包沒有配置為自動掃描的原因。
比如我在 com.weixiao.listener 包下新建的一個類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
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" ); } } } |
關于 component-scan,我們來看 spring framework 開發手冊中的一段話:
1
|
Spring 2.5引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。@Component是所有受Spring管理組件的通用形式;而@Repository、@Service和 @Controller則是@Component的細化,用來表示更具體的用例(例如,分別對應了持久化層、服務層和表現層)。也就是說,你能用@Component來注解你的組件類,但如果用@Repository、@Service 或@Controller來注解它們,你的類也許能更好地被工具處理,或與切面進行關聯。例如,這些典型化注解可以成為理想的切入點目標。當然,在Spring Framework以后的版本中, @Repository、@Service和 @Controller也許還能攜帶更多語義。如此一來,如果你正在考慮服務層中是該用@Component還是@Service,那@Service顯然是更好的選擇。同樣的,就像前面說的那樣, @Repository已經能在持久化層中進行異常轉換時被作為標記使用了。” |
總結
以上就是本文關于spring配置掃描多個包問題解析的全部內容,希望對大家有所幫助。有什么問題可以隨時留言,小編會及時回復大家的。
原文鏈接:http://blog.csdn.net/testcs_dn/article/details/78115603