context:exclude-filter 与 context:include-filter 转
2016-01-10 22:31
495 查看
context:exclude-filter 与 context:include-filter 转
1 在主容器中(applicationContext.xml),将Controller的注解打消掉<context:component-scan base-package="com">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
2 而在springMVC配置文件中将Service注解给去掉
<context:component-scan base-package="com">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
因为spring的context是父子容器,所以会产生冲突,Controller会进步前辈行扫描装配,而此时的
Service还没有进行事务的加强处理惩罚,获得的将是原样的Service(没有经过事务加强处理惩罚,故而没有事务处理惩罚才能)
,最后才是applicationContext.xml中的扫描设备进行事务处理惩罚
上面的好像有错
Spring注解自动注入Bean
我们知道采用Spring注解时,配置如下:<context:annotation-config />
<context:component-scan base-package="cn.itkt"></context:component-scan>
这样的话,在com包及其所有子包下的所有类如果含有@Component、@Controller、@Service、@Repository等 注解的话都会自动纳入到Spring容器中,但是每个类都一个个加上注解,有时难免觉得繁琐,其实Spring也为我们提供了自动为类加上注解的功能。配 置如下:
<context:component-scan base-package="cn.itkt" use-default-filters="false">
<context:include-filter type="regex" expression="cn.itkt.*.service.*.*" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
我们可以看到加了context:include-filter标签和context:exclude-filter标签。
context:include-filter
此标签的含义是:在其扫描到的所有类中,全部自动加上注解并纳入Spring容器中,比如有个类为
public class StudentService implements IStudentService {
}
那么该标签等用于为StudentService类加上@Component注解,且bean的id为studentService。
@Component("studentService")
public class StudentService implements IStudentService {
}
context:exclude-filter
此标签的含义是:排除扫描到的所有类,不纳入Spring容器中。
但需要注意的是,采用自动注入,类名不能相同(即便包名不同),因为自动注入时,id与类名相同,所以如果两个类名一样的话,会因为Bean的id相同而报错。
如果类名一定要相同的话,只能是其中一个类,手动加上注解并将名称改为其他。
相关文章推荐
- NodeJS基础一,安装NodeJs,express
- 【Unity入门】场景、游戏物体和组件的概念
- Hadoop 实现Writable接口
- HDU2825 Wireless Password
- cygwin安装教程以及使用notepad++编写shell脚本,并通过cygwin运行
- akka 资料收集
- java抓取动态生成的网页
- MongoDB
- 关于网络爬虫的那些事:干掉伪装的爬虫
- 毕业十年纪念
- Longest Valid Parentheses
- Python抓取和解析网页
- kruscal算法求最小生成树总结
- C#版三层登录
- java_61_SE lesson8
- Dagger2 使用初步
- android studio 开发经验积累
- Install spark cluster
- HDU1247(经典字典树)
- Android之极光推送发送自定义消息