spring配置扫描多个包 spring配置扫描多个包问题解析

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

spring配置扫描多个包 spring配置扫描多个包问题解析

微wx笑   2021-03-28 我要评论
想了解spring配置扫描多个包问题解析的相关内容吗,微wx笑在本文为您仔细讲解spring配置扫描多个包的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:spring,配置扫描包,spring,配置扫描多个包,spring,扫描配置,下面大家一起来学习吧。

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

<!-- 自动扫描该包,使 SpringMVC 为包下用了@controller注解的类是控制器 --> 
<context:component-scan base-package="com.weixiao.ssmcleardb.controller" /> 
<context:component-scan base-package="com.weixiao.listener" /> 

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

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

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

比如我在 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<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 开发手册中的一段话:

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配置扫描多个包问题解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:Spring配置使用之Bean生命周期详解浅谈Spring的两种配置容器等,有什么问题可以随时留言,小编会及时回复大家的。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们