Class Files:
public interface Work {
public void doWork(String username);
}
public class WorkImpl implements Work {
@Override
public void doWork(String username) {
System.out.println(username + " 正在工作...");
// int i = 5/0;
}
}
public class LogBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] parameters, Object target) throws Throwable {
System.out.println(parameters[0] + " 开始工作...");
}
}
public class LogAfterReturnAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] parameters, Object target) throws Throwable {
System.out.println(parameters[0] + " 结束工作...");
}
}
public class LogExceptionAdvice implements ThrowsAdvice {
public void afterThrowing(Method method, Object[] parameters, Object target, Exception ex){
System.out.println(parameters[0] + " 工作中出现异常... ", ex);
}
}
public class LogAroundAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println(arg0.getArguments()[0] + " 工作中,请勿打扰...");
return arg0.proceed();
}
}
XML配置文件
第一种方法
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- Target Object -->
<bean id="work" class="com.demo.aop.WorkImpl"></bean>
<!-- define Advice -->
<bean id="logBeforeAdvice" class="com.demo.aop.LogBeforeAdvice"></bean>
<bean id="logAfterReturnAdvice" class="com.demo.aop.LogAfterReturnAdvice"></bean>
<bean id="logExceptionAdvice" class="com.demo.aop.LogExceptionAdvice"></bean>
<!-- define Advisor -->
<bean id="logBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="LogBeforeAdvice"/> <!-- logBeforeAdvice 必须实现Advice接口-->
<property name="patterns" value=".*doWork"/>
</bean>
<bean id="logAfterReturnAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="logAfterReturnAdvice"/> <!-- logAfterAdvice 必须实现Advice接口-->
<property name="patterns" value=".*doWork"/>
</bean>
<bean id="logExceptionAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="logExceptionAdvice"/> <!-- logExceptionAdvice 必须实现Advice接口-->
<property name="patterns" value=".*doWork"/>
</bean>
<!-- Proxy Object -->
<!-- <bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>com.demo.aop.Work</value>
</list>
</property>
<property name="target" ref="work"></property>
<property name="interceptorNames">
<list>
<value>logBeforeAdvisor</value>
<value>logAfterReturnAdvisor</value>
<value>logExceptionAdvisor</value>
</list>
</property>
</bean> -->
<bean id="beanNameAutoProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>work</value>
<value>*Service</value>
<value>*DAO</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>logBeforeAdvisor</value>
<value>logAfterReturnAdvisor</value>
<value>logExceptionAdvisor</value>
<value>*Advisor</value>
</list>
</property>
</bean>
<!-- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> -->
</beans>
第二种方法
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- Target Object -->
<bean id="work" class="com.demo.aop.WorkImpl"></bean>
<!-- define Advice -->
<bean id="logBeforeAdvice" class="com.demo.aop.LogBeforeAdvice"></bean>
<bean id="logAfterReturnAdvice" class="com.demo.aop.LogAfterReturnAdvice"></bean>
<bean id="logExceptionAdvice" class="com.demo.aop.LogExceptionAdvice"></bean>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.demo.aop.*.doWork(..))" />
<aop:advisor advice-ref="logBeforeAdvice" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="logAfterReturnAdvice" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="logExceptionAdvice" pointcut-ref="pointcut"/>
</aop:config>
</beans>
第三种方法
//定义一个切面类
public class LogAspect {
public void before(JoinPoint joinPoint) {
System.out.println(joinPoint.getArgs()[0] + " 开始工作...");
}
public void afterReturning (JoinPoint joinPoint, Object retValue) {
System.out.println(joinPoint.getArgs()[0] + " 结束工作...");
}
public void afterThrowing (JoinPoint joinPoint, Exception ex) {
System.out.println(joinPoint.getArgs()[0] + " 工作中出现异常... " + ex);
}
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println(joinPoint.getArgs()[0] + " 工作中,请勿打扰...");
return joinPoint.proceed();
}
public void after (JoinPoint joinPoint) {
System.out.println(joinPoint.getArgs()[0] + " 回家...");
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="work" class="com.demo.aop.WorkImpl"></bean>
<bean id="logAspect" class="com.demo.aop.LogAspect"/>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.demo.aop.*.doWork(..))" />
<aop:aspect id="aspect" ref="logAspect">
<aop:before pointcut-ref="pointCut" method="before"/>
<aop:after-returning pointcut-ref="pointcut" method="afterReturning" returning="retValue"/>
<aop:after-throwing pointcut-ref="pointcut" method="afterThrowing" throwing="ex"/>
<aop:after pointcut-ref="pointcut" method="after"/>
<aop:around pointcut-ref="pointcut" method="around"/>
</aop:aspect>
</aop:config>
</beans>