XML configuration of Spring AOP
Using a JDK proxy
business class
package com.tgb.aop;
public interface UserManager {
public String findUserById(int userId);
}
package com.tgb.aop;
public class UserManagerImpl implements UserManager {
public String findUserById(int userId) {
System.out.println("---------UserManagerImpl.findUserById()--------");
if (userId <= 0) {
throw new IllegalArgumentException("The user does not exist!");
}
return "Zhang San";
}
}
Advice class
package com.tgb.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* Advice notification class
* 测试after,before,around,throwing,returning Advice.
* @author Admin
*
*/
public class XMLAdvice {
/**
* Executed before the execution of the core business, and cannot prevent the invocation of the core business.
* @param joinPoint
*/
private void doBefore(JoinPoint joinPoint) {
System.out.println("-----doBefore().invoke-----");
System.out.println("This is intended to make some security judgments before executing the core business logic, etc.");
System.out.println("You can get what you need through joinPoint");
System.out.println("-----End of doBefore()------");
}
/**
* Manually control the calling core business logic, as well as the processing before and after the call,
*
* Note: When the core business throws an exception, exit immediately and turn to After Advice
* After executing After Advice, go to Throwing Advice
* @param pjp
* @return
* @throws Throwable
*/
private Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("-----doAround().invoke-----");
System.out.println("You can do something similar to Before Advice here");
// call core logic
Object retVal = pjp.proceed();
System.out.println("You can do something similar to After Advice here");
System.out.println("-----End of doAround()------");
return retVal;
}
/**
* After the core business logic exits (including the end of normal execution and abnormal exit), execute this Advice
* @param joinPoint
*/
private void doAfter(JoinPoint joinPoint) {
System.out.println("-----doAfter().invoke-----");
System.out.println("This is intended to do some logging operations, etc. after executing the core business logic");
System.out.println("You can get what you need through joinPoint");
System.out.println("-----End of doAfter()------");
}
/**
* After the core business logic call exits normally, regardless of whether there is a return value, this Advice will be executed after the normal exit
* @param joinPoint
*/
private void doReturn(JoinPoint joinPoint) {
System.out.println("-----doReturn().invoke-----");
System.out.println("The return value can be further processed here");
System.out.println("You can get what you need through joinPoint");
System.out.println("-----End of doReturn()------");
}
/**
* After the core business logic call exits abnormally, execute this Advice to process the error message
* @param joinPoint
* @param ex
*/
private void doThrowing(JoinPoint joinPoint,Throwable ex) {
System.out.println("-----doThrowing().invoke-----");
System.out.println("Error message: "+ex.getMessage());
System.out.println("This is intended to catch exceptions and do some logging operations, etc. when executing core business logic errors");
System.out.println("You can get what you need through joinPoint");
System.out.println("-----End of doThrowing()------");
}
}
To add the configuration of aspect and pointcut to 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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="userManager" class="com.tgb.aop.UserManagerImpl"/>
<!--<bean id="aspcejHandler" class="com.tgb.aop.AspceJAdvice"/>-->
<bean id="xmlHandler" class="com.tgb.aop.XMLAdvice" />
<aop:config>
<aop:aspect id="aspect" ref="xmlHandler">
<aop:pointcut id="pointUserMgr" expression="execution(* com.tgb.aop.*.find*(..))"/>
<aop:before method="doBefore" pointcut-ref="pointUserMgr"/>
<aop:after method="doAfter" pointcut-ref="pointUserMgr"/>
<aop:around method="doAround" pointcut-ref="pointUserMgr"/>
<aop:after-returning method="doReturn" pointcut-ref="pointUserMgr"/>
<aop:after-throwing method="doThrowing" throwing="ex" pointcut-ref="pointUserMgr"/>
</aop:aspect>
</aop:config>
</beans>
测试类
package com.tgb.aop;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
UserManager userManager = (UserManager)factory.getBean("userManager");
//可以查找张三
userManager.findUserById(1);
System.out.println("=====我==是==分==割==线=====");
try {
// 查不到数据,会抛异常,异常会被AfterThrowingAdvice捕获
userManager.findUserById(0);
} catch (IllegalArgumentException e) {
}
}
}
测试结果