spring和springMVC配置文件中的扫描包如何配置

2023-05-16

我的项目大概文件路径:

然后进入主题:
spring的配置文件名称为applicationContext.xml
springMVC的配置文件名称为dispatcherServlet-servlet.xml

1.spring的配置文件中需要将Controller的注解排除掉。也就是排除@Controller。需要扫描到service和dao层的注解
可以用以下这种方式:

	<context:component-scan base-package="com.dancer.crudr">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

2.springMVC的配置文件中需扫描Controller的注解。也就是扫描@Controller
可以用以下这种方式:

	<context:component-scan base-package="com.dancer.crud" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>

接下来具体分析:

//这个是格式,照抄就完了
<context:component-scan></context:component-scan>  
//这个是要扫描的包,我是没填写完整路径
 base-package
//这个默认是true,意思就是会自动对 @Component、@ManagedBeuse-default-filters="true"an、@Named注解的Bean进行扫描。
  反之把他改为false则不对@Component、@ManagedBeuse-default-filters="true"an、@Named注解的Bean进行扫描
use-default-filters
//这个是排除的意思
exclude-filter
//这个是包含的意思
include-filter


spring分析:

	//自动扫描com.dancer.crudr包下的所有注解
	<context:component-scan base-package="com.dancer.crudr">
		//但是排除Controller的注解
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		//exclude-filter:排除,得到除Controller以外的注解
	</context:component-scan>

springMVC分析:

	//不会自动扫描com.dancer.crudr包下的所有注解,因为use-default-filters改为false
	<context:component-scan base-package="com.dancer.crud" use-default-filters="false">
		//获取Controller的注解
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		//use-default-filters:不会自自动扫描
		//include-filter:包含,得到Controller的注解
	</context:component-scan>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

spring和springMVC配置文件中的扫描包如何配置 的相关文章

随机推荐