首先在Eclipse中新建一个普通的Java Project,名称为springAOP。为了使用Spring的注解方式进行面向切面编程,需要在springAOP项目中加入与AOP相关的jar包,spring aop需要额外的jar包有:
- com.springsource.org.aopalliance-1.0.0.jar
- com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
- spring-aop-4.2.5.RELEASE.jar
- spring-aspects-4.2.5.RELEASE.jar
这样,springAOP项目共须jar包如下:
要进行AOP编程,我们接着要在Spring的配置文件——beans.xml中引入aop命名空间:
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
</beans>
Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种:
- 基于XML配置方式声明切面。
- 基于注解方式声明切面。
本文选用第二种方式进行面向切面编程,即基于Spring注解的方式声明切面。
接下来我们在src目录下新建一个it.cast.service包,并在该包下创建PersonService接口