博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring 配置扫描多个包
阅读量:7071 次
发布时间:2019-06-28

本文共 2741 字,大约阅读时间需要 9 分钟。

spring 配置扫描多个包,有时候我们希望不同功能类型的包放在不同的包下,这就需要

有时候我们可能遇到奇怪的问题,

新建了一个包,在这个包下面新建了一个类,也添加了注解,但启动的时候就是扫描不到,而其它的类又正常!

这就是你新建的包没有配置为自动扫描的原因。

比如我在 com.weixiao.listener 包下新建的一个类:

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
{ 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 开发手册中的一段话:

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已经能在持久化层中进行异常转换时被作为标记使用了。”

转载地址:http://itell.baihongyu.com/

你可能感兴趣的文章
Android自定义进度条
查看>>
编写高质量代码改善C#程序的157个建议[匿名类型、Lambda、延迟求值和主动求值]...
查看>>
CkEditor 插件开发
查看>>
《如何让TT T4模板输出多个文件(VS2010中)》-- access911.net 文章
查看>>
CSS Hack大全-可区分出IE6-IE10、FireFox、Chrome、Opera
查看>>
从程序员到项目经理(16):原来一切问题都是可以解决的【转载】
查看>>
当kfreebsd 用户遇见openSUSE系统
查看>>
Struts2自己定义拦截器实例—登陆权限验证
查看>>
调用webservice查询手机号码归属地信息
查看>>
RESTFul basic introduction
查看>>
NoSQL数据库的分布式模型
查看>>
Win7下同时使用有线和无线时的优先级设置
查看>>
Python文件遍历二种方法
查看>>
GUN 的汇编语法
查看>>
java.lang.VerifyError: Inconsistent stackmap frames at branch target
查看>>
sqlite 判断表中是否包含 某个字段
查看>>
freemarker序列的拆分
查看>>
angularjs基本执行流程
查看>>
线段树 + 区间更新: HDU 4893 Wow! Such Sequence!
查看>>
再探vim经常使用命令
查看>>