Spring @Aspect实现切面编程

时间:2023-03-08 16:23:01

参考:http://blog.****.net/cdl2008sky/article/details/6268628

参考:http://www.360doc.com/content/12/0602/15/7656232_215420487.shtml 中的proxy-target-class

Spring @Aspect实现切面编程:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 这个声明会创建AnnotationAwareAspectJAutoProxyCreator,进行切面Bean的代理 -->
<aop:aspectj-autoproxy />
<!-- 必须将切面类声明为一个Bean -->
<bean id="audience" class="com.stono.sprtest3.Audience"></bean>
<bean id="singer" class="com.stono.sprtest3.Singer"></bean>
</beans>

AppBean:

package com.stono.sprtest3;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppBeans11 { @SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("appbeans11.xml");
Performer singer = (Performer) context.getBean("singer");
singer.perform();
} }

切面类:

package com.stono.sprtest3;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
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 Audience {
// 为一个类进行切点的声明也是可以的;
// @Pointcut("execution(* com.stono.sprtest3.Singer.perform()(..))")
@Pointcut("execution(* com.stono.sprtest3.Performer.perform(..))")
public void pointcut() {// 方法名称就是一个标记
}
@Before("pointcut()")
public void takeSeat() {
System.out.println("com.stono.sprtest2.Audience.takeSeat()");
}
@Before("pointcut()")
public void turnOffPhone() {
System.out.println("com.stono.sprtest2.Audience.turnOffPhone()");
}
@AfterReturning("pointcut()")
public void applaud() {
System.out.println("com.stono.sprtest2.Audience.applaud()");
}
@AfterThrowing("pointcut()")
public void refund() {
System.out.println("com.stono.sprtest2.Audience.refund()");
}
@Around("pointcut()")
public void watchPerformance(ProceedingJoinPoint joinPoint) {
try {
System.out.println("The audience is taking their seats.");
System.out.println("The audience is turning off their cellphones.");
long start = System.currentTimeMillis();
joinPoint.proceed();
long end = System.currentTimeMillis();
System.out.println("CLAP CLAP CLAP CLAP CLAP CLAP");
System.out.println("The performance took " + (end - start) + " milliseconds.");
} catch (Throwable e) {
e.printStackTrace();
System.out.println("Boo! We want our money back!");
}
}
}

接口和类:

package com.stono.sprtest3;
public interface Performer {
void perform();
}
package com.stono.sprtest3;
public class Singer implements Performer {
public void perform() {
System.out.println("com.stono.sprtest3.Singer.perform()");
}
}