xml配置方式
1.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<aop:config>
<!-- 定义一个切面 -->
<aop:aspect id="TestAspect" ref="aspectBean">
<!-- 定义切入点 -->
<aop:pointcut id="businessService" expression="execution(* com.spring.service.*.*(..))"> </aop:pointcut>
<aop:before pointcut-ref="businessService" method="doBefore"></aop:before>
<aop:after pointcut-ref="businessService" method="doAfter"></aop:after>
<aop:around pointcut-ref="businessService" method="doAround"></aop:around>
<aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"></aop:after-throwing>
</aop:aspect>
</aop:config>
<bean id="aspectBean" class="com.spring.aop.TestAspect"></bean>
<bean id="aServiceImpl" class="com.spring.service.AServiceImpl"/>
</beans>
2.接口类:
package com.spring.service;
public interface AService{
public void testA();
}
3.接口实现类:
package com.spring.service;
public class AServiceImpl implements AService{
public void testA(){
System.out.println("===testA===");
}
}
4.切面类:
package com.spring.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class TestAspect{
public void doAfter(JoinPoint jp) {
System.out.println("log Ending method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long time = System.currentTimeMillis();
Object retVal = pjp.proceed();
time = System.currentTimeMillis() - time;
System.out.println("process time: " + time + " ms");
return retVal;
}
public void doBefore(JoinPoint jp) {
System.out.println("log Begining method: " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName());
}
public void doThrowing(JoinPoint jp, Throwable ex) {
System.out.println("method " + jp.getTarget().getClass().getName() + "." + jp.getSignature().getName() + " throw exception");
System.out.println(ex.getMessage());
}
}
5.测试类:
package com.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.spring.service.AService;
public class Test{
public static void main(String[] args){
ApplicationContext ctx=new FileSystemXmlApplicationContext("src/applicationContext.xml");
AService s=(AService)ctx.getBean("aServiceImpl");
s.testA();
}
}
6.执行结果:
log Begining method: com.spring.service.AServiceImpl.testA
===testA===
log Ending method: com.spring.service.AServiceImpl.testA
process time: 156 ms
注解(Annotation)方式
1.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<!-- 1.配置方式 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 2.配置方式 --><!--
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
--><bean id="aspectBean" class="com.spring.aop.TestAnnotationAspect"></bean>
<bean id="aServiceImpl" class="com.spring.service.AServiceImpl"/>
</beans>
2.注解切面类
package com.spring.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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 TestAnnotationAspect {
@Pointcut("execution(* com.spring.service.*.*(..))")
private void pointCutMethod() {
}
//声明前置通知
@Before("pointCutMethod()")
public void doBefore() {
System.out.println("前置通知");
}
//声明后置通知
@AfterReturning(pointcut = "pointCutMethod()", returning = "result")
public void doAfterReturning(String result) {
System.out.println("后置通知");
}
//声明例外通知
@AfterThrowing(pointcut = "pointCutMethod()", throwing = "e")
public void doAfterThrowing(Exception e) {
System.out.println("例外通知");
System.out.println(e.getMessage());
}
//声明最终通知
@After("pointCutMethod()")
public void doAfter() {
System.out.println("最终通知");
}
//声明环绕通知
@Around("pointCutMethod()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("进入方法---环绕通知");
Object o = pjp.proceed();
System.out.println("退出方法---环绕通知");
return o;
}
}
3.执行结果:
进入方法---环绕通知
前置通知
===testA===
退出方法---环绕通知
最终通知
后置通知