context:component-scan base-package

Element : component-scan
Scans the classpath for annotated components that will be auto-registered as Spring beans. By
 default, the Spring-provided @Component, @Repository, @Service, and @Controller stereotypes will
 be detected. Note: This tag implies the effects of the 'annotation-config' tag, activating @Required,
 @Autowired, @PostConstruct, @PreDestroy, @Resource, @PersistenceContext and @PersistenceUnit
 annotations in the component classes, which is usually desired for autodetected components (without
 external configuration). Turn off the 'annotation-config' attribute to deactivate this default behavior,
 for example in order to use custom BeanPostProcessor definitions for handling those annotations.
 Note: You may use placeholders in package paths, but only resolved against system properties
 (analogous to resource paths). A component scan results in new bean definition being registered;
 Spring's PropertyPlaceholderConfigurer will apply to those bean definitions just like to regular bean
 definitions, but it won't apply to the component scan settings themselves.

Content Model : (include-filter*, exclude-filter*)

上面文字,是人家官方解释。翻译如下:

扫描注释组件会自动注册为Spring bean的类路径。通过
 默认情况下,Spring提供@Component,@Repository,@Service和@Controller将定型
 被检测到。注意:这个标签意味着“注释的配置”标签的效果,激活@Required,
 @Autowired,@PostConstruct,@PreDestroy,@Resource,@PersistenceContext和@PersistenceUnit
 在组件类,这通常是所希望的用于自动检测部件注解(无
 外部配置)。关闭“注释的配置”属性停用​​此默认行为,
 例如,为了使用BeanPostProcessor的定义来处理这些注释。
 注意:您可以在包路径中使用的占位符,但只解决了对系统属性
 (类似于资源路径)。在新的bean定义组件扫描结果被注册;
 Spring的PropertyPlaceholderConfigurer将适用于那些bean定义,就像定期豆
 的定义,但它不适用于该组件扫描设置自己。

意思就是,这个,可以扫描controller ,也可以扫描,respository ,service等等 其他的,所有的注解。

但是,在配置扫描路径的时候需要注意,

@Controller,  只能  在 spring mvc-config.xml中,进行配置扫描。
其他的注解,包括,@Autowired , @Resource ,等等的注解,在 applicationContext.xml中,或者springmvc-config.xml中配置均可。

下面是我的web.xml配置。原文地址:http://blog.csdn.net/u012246342/article/details/52126286

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:application*.xml</param-value>
	</context-param>
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
				/WEB-INF/classes/springmvc-config.xml
			</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>

springMVC的配置文件有这两个。

controller的 注解扫描,必须在springmvc-config.xml中进行配置,否则,虽然运行项目不报错,但是,请求的所有方法,均不会被扫描到。

其他的 注解扫描,可以在 这两个文件中任意进行配置,如果不进行配置,报错如下:

错误日志信息,本文最后。。

我的所有注解扫描配置,均写在 springmvc-config.xml 中。多个扫描路径,用 英文  ,  隔开。

最好写的详细一点,详细到具体的包,别配置全路径,全部扫描很慢的,影响效率

我的配置如下:

<context:component-scan base-package="com.wswhr.LoginServer.dao.*,com.wswhr.LoginServer.service.*,com.wswhr.LoginServer.springmvc.*" />

最后,没有配置 @Autowired 等等 其他配置,报错 日志如下。

严重: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wswhr.LoginServer.service.SystemUserService com.wswhr.LoginServer.springmvc.controller.LoginController.sysUserService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.wswhr.LoginServer.service.SystemUserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
	at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
	at javax.servlet.GenericServlet.init(GenericServlet.java:212)
	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1213)
	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1026)
	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4451)
	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4768)
	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1061)
	at org.apache.catalina.core.StandardHost.start(StandardHost.java:822)
	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1061)
	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
	at org.apache.catalina.core.StandardService.start(StandardService.java:525)
	at org.apache.catalina.core.StandardServer.start(StandardServer.java:759)
	at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.wswhr.LoginServer.service.SystemUserService com.wswhr.LoginServer.springmvc.controller.LoginController.sysUserService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.wswhr.LoginServer.service.SystemUserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
	... 34 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.wswhr.LoginServer.service.SystemUserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
	... 36 more

八月 19, 2016 4:43:10 下午 org.apache.catalina.core.StandardContext loadOnStartup
严重: Servlet /LoginSystem threw load() exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.wswhr.LoginServer.service.SystemUserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
	at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
	at javax.servlet.GenericServlet.init(GenericServlet.java:212)
	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1213)
	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1026)
	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4451)
	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4768)
	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1061)
	at org.apache.catalina.core.StandardHost.start(StandardHost.java:822)
	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1061)
	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
	at org.apache.catalina.core.StandardService.start(StandardService.java:525)
	at org.apache.catalina.core.StandardServer.start(StandardServer.java:759)
	at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
时间: 2024-09-20 17:46:45

context:component-scan base-package的相关文章

Spring Bean注册方式小结

IOC容器 Spring的核心是一个IOC容器,管理着我们向容器注册的所有bean.下面我们来看下两种向容器注册bean的方式, 通过BeanDefinitionReader读取Spring Bean的配置文件,解析然后注册: 通过ClassPathBeanDefinitionScanner直接扫描带有Spring Bean注解的Java类并注册: Reader BeanDefinitionReader的使用方式如下, import org.springframework.beans.facto

史上最强Spring mvc入门

一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <!--configure the setting of springmvcDispatcherServlet and configure the mapping--> <servlet>     <servlet-name>

看看Spring的源码(一)——Bean加载过程

本文转自独立博客:Geeekr 最近几天跟同事聊起Spring的一些问题,对一些地方有些疑问,趁这两天有点空,看看Spring的源码,了解下具体的实现细节.本文基于Spring 4.0.5版本. 首先Web项目使用Spring是通过在web.xml里面配置org.springframework.web.context.ContextLoaderListener初始化IOC容器的. <listener> <listener-class>org.springframework.web.

史上最全最强SpringMVC详细示例实战教程(图文)_java

一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 <!--configure the setting of springmvcDispatcherServlet and configure the mapping--> <servlet> <servlet-name>springmvc</servlet-name> <serv

《Spring实战(第4版)》——2.2 自动化装配bean

2.2 自动化装配bean 在本章稍后的内容中,你会看到如何借助Java和XML来进行Spring装配.尽管你会发现这些显式装配技术非常有用,但是在便利性方面,最强大的还是Spring的自动化配置.如果Spring能够进行自动化装配的话,那何苦还要显式地将这些bean装配在一起呢? Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中所创建的bean.自动装配(autowiring):Spring自动满足bean之间的依赖

Freeline - Android平台上的秒级编译方案

Freeline 技术揭秘 Freeline是什么? Freeline是蚂蚁金服旗下一站式理财平台蚂蚁聚宝团队15年10月在Android平台上的量身定做的一个基于动态替换的编译方案,5月阿里集团内部开源,稳定性方面:完善的基线对齐,进程级别异常隔离机制.性能方面:内部采用了类似Facebook的开源工具buck的多工程多任务并发思想:端口扫描,代码扫描,并发编译,并发dx,并发merge dex等策略,在多核机器上有明显加速效果,另外在class及dex,resources层面作了相应缓存策略

ASP.net控件开发系列之五

TypeConverter 在本系列的上篇文章中,和大家控讨了控件开发与propertyGrid的关系,不知现在大家现在对propertygrid有没有一个较全面的了解,也不知大家有没有做个工程,把propertyGrid拉进去鼓捣鼓捣? "另起炉灶" 现在我们来思考一个问题:假于,propertygrid没有把属性和事件分成两个分页来显示,会产生什么效果? 那还用说,太乱了. 那如果你设计的控件有很多的属性,而一些关联性很强,或都是操作一个方面的,那么我们可以把它们分门别类,摆到一起

Android手机 Root 安全问题 机制

本帖转自:        http://su1216.iteye.com/        http://blog.csdn.net/su1216/ 前提:你有一部已经root的android手机,并且手机中有busybox和superuser 写本博客的原因是:有无数用户觉得root没有什么风险,或者风险不会降临到自己头上.这里告诉大家,其实风险就在身边! 1.Android手机Root安全风险篇章一 导读:本文介绍一种简单的病毒以及如何"防御". 这里讲的内容其实对于大多数开发者都不

史上最全最强SpringMVC详细示例实战教程

1)Spring MVC 在调用处理方法之前,在请求线程中自动的创建一个隐含的模型对象. 2)调用所有方法级的 标注了 @ModelAttribute 的方法,并将方法返回值添加到隐含的模型对象中. 3)如果方法所在的控制器 (标记 @Controller的类)没有标记 @SessionAttributes("sessionXXX") 注释,则该处理步骤可跳过. 查看Session 中是否存在 sessionXXX 属性,如果有,将其添加到隐含的模型对象中.如果隐含的模型对象中已经存在