要进行AOP编程,首先要在Spring 的配置文件中引入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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> </beans>
使用切面编程还需要使用以下的jar文件
lib/aspectj/aspectjjweaver.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1-3.jar
启动对@AspectJ注解的支持
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy/> </beans>
加入业务bean
package servlice;
public interface Person {
public void eat();
public void sleep();
}
package impl;
import servlice.Person;
public class PersonImpl implements Person{
public void eat() {
System.out.println("吃饭了");
}
public void sleep() {
System.out.println("睡觉去");
}
}
package intercept;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class MyIntercept {
@Pointcut("execution(* impl.PersonImpl.*(..))")
private void anyMethod(){}
// 第一个*代表所有的返回类型
// 第二个*代表里面所有的方法
@Before("anyMethod()")
private void before()
{
System.out.println("吃饭前先洗手");
}
}
在xml方法中注册bean
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy/> <bean id="myIntercept" class="intercept.MyIntercept"></bean> <bean id="person" class="impl.PersonImpl"></bean> </beans>
测试类
package text;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import servlice.Person;
public class AopText {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Person person=(Person) context.getBean("person");
person.eat();
person.sleep();
}
}
测试结果
吃饭前先洗手
吃饭了
吃饭前先洗手
睡觉去
修改MyIntercept
package intercept;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class MyIntercept {
@Pointcut("execution(* impl.PersonImpl.eat(..))")//修改了这里
private void eat(){}
// 第一个*代表所有的返回类型
// 第二个*代表里面所有的方法
@Before("eat()")
private void beforeEat()
{
System.out.println("吃饭前先洗手");
}
@After("eat()")
private void afterEat()
{
System.out.println("饭后漱口");
}
}
测试结果
吃饭前先洗手
吃饭了
饭后漱口
睡觉去
再次修改
package intercept;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class MyIntercept {
@Pointcut("execution(* impl.PersonImpl.eat(..))")//修改了这里
private void eat(){}
// 第一个*代表所有的返回类型
// eat表示类里面的eat方法
@Before("eat()")
private void beforeEat()
{
System.out.println("吃饭前先洗手");
}
@After("eat()")
private void afterEat()
{
System.out.println("饭后漱口");
}
@Pointcut("execution(* impl.PersonImpl.sleep(..))")//添加了这些
private void sleep(){}
@Before("sleep()")
private void beforeSleep()
{
System.out.println("睡觉前先刷牙洗脸");
}
@After("sleep()")
private void afterSleep()
{
System.out.println("睡觉起来也要刷牙洗脸");
}
}
结果
吃饭前先洗手
吃饭了
饭后漱口
睡觉前先刷牙洗脸
睡觉去
睡觉起来也要刷牙洗脸
再次修改
package intercept;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
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 MyIntercept {
@Pointcut("execution(* impl.PersonImpl.eat(..))")//修改了这里
private void eat(){}
// 第一个*代表所有的返回类型
// eat表示类里面的eat方法
@Before("eat()")
private void beforeEat()
{
System.out.println("吃饭前先洗手");
}
@After("eat()")
private void afterEat()
{
System.out.println("饭后漱口");
}
@Pointcut("execution(* impl.PersonImpl.sleep(..))")//添加了这些
private void sleep(){}
@Before("sleep()")
private void beforeSleep()
{
System.out.println("睡觉前先刷牙洗脸");
}
@AfterReturning("sleep()")
private void afterSleep()
{
System.out.println("睡觉起来也要刷牙洗脸");
}
@Around("sleep()")
private Object around(ProceedingJoinPoint joinPoint) throws Throwable
{
System.out.println("准备睡觉");
Object result=joinPoint.proceed();
System.out.println("睡着了");
return result;
}
}
运行结果:
吃饭前先洗手
吃饭了
饭后漱口
准备睡觉
睡觉前先刷牙洗脸
睡觉去
睡着了
睡觉起来也要刷牙洗脸