context.component-scan节点
base-package属性告诉spring要扫描的包
use-default-filters=”false”表示不要使用默认的过滤器,此处的默认过滤器,会扫描包含Service,Component,Responsitory,Controller注释修饰类。
@Service告诉spring容器,这是一个Service类,标识持久层Bean组件,默认情况会自动加载它到spring容器中。
@Autowried注解告诉spring,这个字段需要自动注入
@Scope指定此spring bean的scope是单例
@Repository注解指定此类是一个容器类,是DA层类的实现。标识持久层Bean组件
@Componet:基本注解,标识一个受Spring管理的Bean组件
@Controller:标识表现层Bean组件
Filter Type Examples Expression Description
annotation org.example.SomeAnnotation 符合SomeAnnoation的target class
assignable org.example.SomeClass 指定class或interface的全名
aspectj org.example..*Service+ AspetJ语法
regex org.example.Default.* Regelar Expression
custom org.example.MyTypeFilter Spring3新增自订Type,称作org.springframework.core.type.TypeFilter
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.sparta.trans" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.point.plat.protocol" />
</beans>
<context:component-scan base-package="com.test.scan.core" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<!--含义是这里我们不扫描core包下所有的@Repository注解-->
<!--当然也可以设置为@Service、@Controller、@Component、@Configuration其中一种-->
</context:component-scan>
<context:component-scan base-package="com.test.scan.core" >
<context:exclude-filter type="assignable" expression="com.test.scan.core.UserImpl"/>
<!--含义是这里我们不扫描指定UserImpl类-->
</context:component-scan>
<context:component-scan base-package="com.test.scan.core" >
<context:exclude-filter type="aspectj" expression="com.test.scan..*"/>
<!--通过aop的方式-->
</context:component-scan>
<context:component-scan base-package="com.test.scan.core" >
<context:exclude-filter type="regex" expression=".test.scan.*"/>
<!--通过aop的方式-->
</context:component-scan>
@Transactional 事务