基于@Aspect 注解
基于@Aspect 注解的AOP开发主要需要目标类、切面、配置文件。
调用关系如图:
首先引入jar包:
下载jar
目标类(HelloWorld):
package com.demo.spring.aop;
public class Helloworld {
protected String message;//定义String 类型变量
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void execute(){
System.out.println("Hello"+getMessage()+"!");
}
}
切面:
package com.demo.spring.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class MyAspect {
/** * 定义切入点函数 */
@Pointcut("execution(* com.demo.spring.aop.*.execute*(..))")
void pointcut(){
}
/** * 使用@Before声明前置通知 * @param thisJoinPoint */
@Before("pointcut()")
public void beforeExecute(JoinPoint thisJoinPoint){
System.out.println("start to say:");
}
/** * 使用@Around 注解声明环绕通知 * @param thisJoinPoint * @return * @throws Throwable */
@Around("pointcut()")
public Object userOperate(ProceedingJoinPoint thisJoinPoint) throws Throwable{
System.out.println("before 之前");
Object va=thisJoinPoint.proceed();
System.out.println("after 之前");
return va;
}
/** * 使用@After 注解声明后置通知 * @param thisJoinPoint */
@After("pointcut()")
public void afterExecute(JoinPoint thisJoinPoint){
System.out.print("end!");
}
}
applicationContext.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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="Helloworld" class="com.demo.spring.aop.Helloworld">
<property name="message">
<value>World</value>
</property>
</bean>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="myAspect" class="com.demo.spring.aop.MyAspect"></bean>
</beans>
客户端调用:
package com.demo.spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Test {
public static void main(String[] args) { ApplicationContext ctx=new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml"); Helloworld hello=(Helloworld) ctx.getBean("Helloworld"); hello.execute(); }
}
这中方式主要是用Spring的注解方式开发AOP,还有一种是通过Schema(xml)的方式
基于Schema(xml)
与@Aspect注解相比不同之处主要在切面类与配置文件上,切面类不再用注解,而是在配置文件中配置增强
切面类:
package com.demo.spring.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
/** * 切面类 * @author dandan * */
@Aspect
public class MyAspect {
/** * 前置通知 * @param thisJoinPoint */
public void beforeExecute(JoinPoint thisJoinPoint){
System.out.println("start to say:");
}
/** * 环绕通知类 * @param thisJoinPoint * @return * @throws Throwable */
public Object userOperate(ProceedingJoinPoint thisJoinPoint) throws Throwable{
return thisJoinPoint.proceed();
}
/** * 后置通知类 * @param thisJoinPoint */
public void afterExecute(JoinPoint thisJoinPoint){
System.out.println("end!");
}
}
配置文件:
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="HelloWorld" class="com.demo.spring.aop.HelloWorld">
<property name="message">
<value>World</value>
</property>
</bean>
<bean id="myAspect" class="com.demo.spring.aop.MyAspect"></bean>
<aop:config>
<!-- 定义切面 -->
<aop:aspect id="aopAspect" ref="myAspect">
<!-- 定义切点 -->
<aop:pointcut id="pointcut" expression="execution(* execute(..)) and target(com.demo.spring.aop.HelloWorld)"/>
<!-- 定义通知 -->
<aop:before pointcut-ref="pointcut" method="beforeExecute"/>
<aop:around pointcut-ref="pointcut" method="userOperate"/>
<aop:after pointcut-ref="pointcut" method="afterExecute"/>
</aop:aspect>
</aop:config>
</beans>