基本术语
Joinpoint(连接点):所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点.
Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义.
Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)
Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.
Target(目标对象):代理的目标对象
简而言之 最核心的有三个:对对象(Target)的那些方法(Pointcut)进行增强 (Advice)
以下是开发流程
1新建maven项目,引入相关jar包,
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test</groupId> <artifactId>spring</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>spring Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.1.3.RELEASE</spring.version> </properties> <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <!-- java编译插件 jkd1.8 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
2建立相关类和接口
public interface UserService { void add(); void delete(); } ======== public class UserServImp implements UserService { @Override public void add() { System.out.println("add"); } @Override public void delete() { System.out.println("delete"); } }
3编写增强
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import org.omg.Messaging.SyncScopeHelper; import org.springframework.aop.BeforeAdvice; /** * * @author Administrator * * 使用的是环绕增强 * */ public class AroundAdvise implements MethodInterceptor{ @Override public Object invoke(MethodInvocation arg0) throws Throwable { System.out.println("开始时间 -----"); Object result = arg0.proceed(); //执行目标的方法 System.out.println("结束时间 -----"); return result; } }
在xml配置,contextBeanPointCutAuto.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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 通过一个<bean>标签设置类的信息,通过id属性为类起个标识. --> <!-- 定义目标对象 --> <bean id="userService" class="com.aop1.UserServImp"> </bean> <!-- 定义我增强 --> <bean id="aroundAdvice" class="com.aop1.AroundAdvise"></bean> <!-- 定义切点切面: --> <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!-- 定义哪些方法需要增强--> <property name="patterns" value=".*add.*,.*delete.*"></property> <property name="advice" ref="aroundAdvice"></property> </bean> <!-- 带有切面的自动代理 基于后处理bean,生成的已经是代理对象了--> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean> </beans>
测试代码
public class TestSpring { @Test public void test() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("contextBeanPointCutAuto.xml"); UserService userService = (UserService) applicationContext.getBean("userService"); userService.add(); userService.delete(); } }
结果如下