您的位置:首页 > 其它

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相同而报错。

如果类名一定要相同的话,只能是其中一个类,手动加上注解并将名称改为其他。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: