maven:
<!-- spring -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.9</version>
</dependency>
测试项目结构
application.xml:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
default-autowire="byName">
<tx:annotation-driven />
<context:component-scan base-package="com" use-default-filters="false">
<context:include-filter type="regex"
expression="com\.wanba\..*\.service\..*" />
<context:include-filter type="regex"
expression="com\.wanba\..*\.dao\..*" />
</context:component-scan>
</beans>
Test.java
package com;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wanba.TestService;
import com.wanba.service.WanbaService;
import com.wanba.user.service.UserService;
public class Test
{
@SuppressWarnings("resource")
public static void main(String[] args)
{
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
Object bean = classPathXmlApplicationContext.getBean("userService");
UserService service = (UserService)bean;
service.serviceuser();
System.out.println("-----------------------------");
Object bean2 = classPathXmlApplicationContext.getBean("wanbaService");
WanbaService wanbaService = (WanbaService)bean2;
wanbaService.serviceWanba();
System.out.println("-----------------------------");
Object bean3 = classPathXmlApplicationContext.getBean("testService");
TestService service3 = (TestService)bean3;
service3.service();
}
}
通过修改各种配置,注解。小结如下:
1.采用默认配置use-default-filters=”true”默认为true
<tx:annotation-driven />
<context:component-scan base-package="com" use-default-filters="true">
</context:component-scan>
此时spring只会扫描包下的类且是使用诸如@Service、@Repository、@Component之类的注解符号注解过了的。其他的类则扫描不到。此时若是添加扫描规则,起作用的是:在扫描规则下的包不需要写注解符号。
2.use-default-filters=”true”设置为false
<tx:annotation-driven />
<context:component-scan base-package="com" use-default-filters="false">
</context:component-scan>
此时啥也扫描不到,其实是扫描没工作。这里的意思是在com包下只扫描自定义的路径下的类。
3.use-default-filters=”true”设置为false,添加自定义规则
<tx:annotation-driven />
<context:component-scan base-package="com" use-default-filters="false">
<context:include-filter type="regex" expression="com.wanba.user.service.*" />
<context:include-filter type="regex" expression="com.wanba.user.dao.*" />
</context:component-scan>
此时com.wanba.user.service和com.wanba.user.dao包下的类,无论是不是有注解符号都可以被扫描到。其他的包下不会去扫描。
4.use-default-filters=”true”设置为false,添加自定义规则扫描多个包
<tx:annotation-driven />
<context:component-scan base-package="com" use-default-filters="false">
<context:include-filter type="regex" expression="com.wanba.*.service.*" />
<context:include-filter type="regex" expression="com.wanba.*.dao.*" />
</context:component-scan>
此时com.wanba包下的service和dao或者子包下的service和dao包下的类无论是否添加注解都会被扫描到。不在这个范围内不会被扫描。
5.use-default-filters=”true”设置为false,添加自定义规则扫描多个包,添加剔除扫描范围的包
<tx:annotation-driven />
<context:component-scan base-package="com" use-default-filters="false">
<context:include-filter type="regex"
expression="com.*" />
<context:exclude-filter type="regex"
expression="com.wanba.user.*" />
</context:component-scan>
include-filter:扫描哪些包。
exclude-filter:不扫描哪些包。
这里需要注意的是,当扫描一些存在依赖关系的包的时候,如果某一个类的依赖类被剔除扫描了,那么这个类会因为无法完成实例化而扫描失败,进而加载失败。