aop 面向切面的编程
Spring如何配置一个简单的aop
第一步:首先是编写一个Operator的类,(类的名字你随便起,开心最为重要)
package com.wdg.operator;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.AopInvocationException;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class Operator
{
private Logger logger = Logger.getLogger(this.getClass());
public void pointCut() {
}
public void doBefore(JoinPoint joinPoint) {
System.out.println("AOP Before Advice...");
return;
}
public void doAfter(JoinPoint joinPoint) {
System.out.println("AOP After Advice...");
return;
}
public Object afterReturn(JoinPoint joinPoint, Object returnVal) {
System.out.println("AOP AfterReturning Advice:" + returnVal);
return null;
}
public void afterThrowing(JoinPoint joinPoint, Throwable error) {
System.out.println("AOP AfterThrowing Advice..." + error);
System.out.println("AfterThrowing...");
}
public Object around(ProceedingJoinPoint pjp) {
System.out.println("AOP Aronud before...");
try {
System.out.println(pjp.toString());
}
catch (AopInvocationException e) {
logger.info(e.getMessage());
}
catch(Exception e){
logger.info(e.getMessage());
}
System.out.println("AOP Aronud after...");
return null;
}
}
第二步:引入配置文件,这个地方我专门为aop的配置新增了一个配制文件spring-aop.xml,在applicationContent.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<import resource="classpath:spring-beans.xml" />
<import resource="classpath:spring-jdbc.xml" />
<import resource="classpath:spring-aop.xml" />
<!-- <import resource="classpath:spring-monodb.xml" /> <import resource="classpath:spring-redis.xml"
/> -->
</beans>
spring-aop.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<aop:aspectj-autoproxy />
<!-- 使用注解时要开启注解扫描 要扫描的包 -->
<bean id="operator" class="com.wdg.operator.Operator"></bean>
<aop:config proxy-target-class="true">
<aop:pointcut id="servicePointCut"
expression="execution(* com.wdg.login.impl..*..*(..))" />
<aop:aspect id="serviceAspect" ref="operator">
<aop:before method="doBefore" pointcut-ref="servicePointCut" />
<!-- <aop:around method="around" pointcut-ref="servicePointCut" /> -->
<aop:around method="doAfter" pointcut-ref="servicePointCut" />
</aop:aspect>
</aop:config>
</beans>
这样当我们调用com.wdg.login.impl中的下面的类,里面的任意的方法的时候,都能够监控到,并且执行Opertor中的doBefore和doAfter的方法
希望对你有所帮助