在本教程中,我们将向你展示如何转换上章节中
Spring AOP+AspectJ 注解转成基于XML的配置。
对于那些不喜欢注释,使用JDK1.4,则可以基于XML,而不使用 AspectJ。
再次回顾上个 customerBo 接口中的几个方法,以后你将学会如何在 XML文件实现 AspectJ 拦截。
File:CustomerBo.java
public interface CustomerBo {
void addCustomer();
String addCustomerReturnValue();
void addCustomerThrowException() throws Exception;
void addCustomerAround(String name);
}
File:CustomerBoImpl.java
public class CustomerBoImpl implements CustomerBo{
@Override
public void addCustomer() {
System.out.println("addCustomer() is running");
}
@Override
public String addCustomerReturnValue() {
System.out.println("addCustomerReturnValue() is running");
return "abc";
}
@Override
public void addCustomerThrowException() throws Exception {
System.out.println("addCustomerThrowException() is running");
throw new Exception("Error");
}
@Override
public void addCustomerAround(String name) {
System.out.println("addCustomerAround() is running, args: " + name);
}
}
1. AspectJ <aop:before> = @Before
File:LoggingAspect.java
public class LoggingAspect {
public void logBefore() {
System.out.println("logBefore() is running!");
System.out.println("******");
}
}
File:applicationContext.xml
在XML同等功能,使用 <aop:before>.
<?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"
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">
<aop:aspectj-autoproxy/>
<bean id="customerBo" class="com.ray.customer.bo.impl.CustomerBoImpl"/>
<!-- Aspect -->
<bean id="logAspect" class="com.ray.aspect.LoggingAspect"/>
<aop:config>
<aop:aspect id="aspectLogging" ref="logAspect">
<!-- @Before -->
<aop:pointcut expression="execution(* com.ray.customer.bo.CustomerBo.addCustomer(..))" id="pointCutBefore"/>
<aop:before method="logBefore" pointcut-ref="pointCutBefore"/>
</aop:aspect>
</aop:config>
</beans>
执行程序
public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerBo customer = (CustomerBo) context.getBean("customerBo");
customer.addCustomer();
}
}
执行结果
logBefore() is running!
******
addCustomer() is running
2. AspectJ <aop:after> = @After
File:LoggingAspect.java
public class LoggingAspect {
public void logAfter() {
System.out.println("logAfter() is running!");
System.out.println("******");
}
}
File:applicationContext.xml
在XML同等功能,使用 <aop:after>实现。
<?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"
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">
<aop:aspectj-autoproxy/>
<bean id="customerBo" class="com.ray.customer.bo.impl.CustomerBoImpl"/>
<!-- Aspect -->
<bean id="logAspect" class="com.ray.aspect.LoggingAspect"/>
<aop:config>
<aop:aspect id="aspectLogging" ref="logAspect">
<!-- @After -->
<aop:pointcut expression="execution(* com.ray.customer.bo.CustomerBo.addCustomer(..))" id="pointCutAfter"/>
<aop:after method="logAfter" pointcut-ref="pointCutAfter"/>
</aop:aspect>
</aop:config>
</beans>
执行结果
addCustomer() is running
logAfter() is running!
******
3. AspectJ <aop:after-returning> = @AfterReturning
File:LoggingAspect.java
public class LoggingAspect {
public void logAfterReturning(JoinPoint jp,Object result) {
System.out.println();
System.out.println("logAfterReturning() is running!");
System.out.println("getName(): " + jp.getSignature().getName());
System.out.println("Method returned value is : " + result);
System.out.println("******");
}
}
File:applicationContext.xml
在XML同等功能 - 使用 <aop:after-returning>.
<?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"
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">
<aop:aspectj-autoproxy/>
<bean id="customerBo" class="com.ray.customer.bo.impl.CustomerBoImpl"/>
<!-- Aspect -->
<bean id="logAspect" class="com.ray.aspect.LoggingAspect"/>
<aop:config>
<aop:aspect id="aspectLogging" ref="logAspect">
<!-- @AfterReturning -->
<aop:pointcut expression="execution(* com.ray.customer.bo.CustomerBo.addCustomerReturnValue(..))" id="pointCutAfterReturning"/>
<aop:after-returning method="logAfterReturning" pointcut-ref="pointCutAfterReturning" returning="result"/>
</aop:aspect>
</aop:config>
</beans>
执行程序
public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerBo customer = (CustomerBo) context.getBean("customerBo");
customer.addCustomerReturnValue();
}
}
执行结果
addCustomerReturnValue() is running
logAfterReturning() is running!
getName(): addCustomerReturnValue
Method returned value is : abc
******
4. AspectJ <aop:after-throwing> = @AfterThrowing
File:LoggingAspect.java
public class LoggingAspect {
public void logAfterThrowing(JoinPoint jp,Throwable error) {
System.out.println();
System.out.println("logAfterThrowing() is running!");
System.out.println("getName() : " + jp.getSignature().getName());
System.out.println("Exception : " + error);
System.out.println("******");
}
}
File:applicationContext.xml
在XML同等功能 - 使用 <aop:after-throwing>.
<?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"
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">
<aop:aspectj-autoproxy/>
<bean id="customerBo" class="com.ray.customer.bo.impl.CustomerBoImpl"/>
<!-- Aspect -->
<bean id="logAspect" class="com.ray.aspect.LoggingAspect"/>
<aop:config>
<aop:aspect id="aspectLogging" ref="logAspect">
<!-- @AfterThrowing -->
<aop:pointcut expression="execution(* com.ray.customer.bo.CustomerBo.addCustomerThrowException(..))" id="pointCutAfterThrowing"/>
<aop:after-throwing method="logAfterThrowing" pointcut-ref="pointCutAfterThrowing" throwing="error"/>
</aop:aspect>
</aop:config>
</beans>
执行程序
public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerBo customer = (CustomerBo) context.getBean("customerBo");
customer.addCustomerThrowException();
}
}
执行结果
addCustomerThrowException() is running
logAfterThrowing() is running!
getName() : addCustomerThrowException
Exception : java.lang.Exception: Error
******
Exception in thread "main" java.lang.Exception: Error
at com.ray.customer.bo.impl.CustomerBoImpl.addCustomerThrowException(CustomerBoImpl.java:21)
5. AspectJ <aop:after-around> = @Around
File:LoggingAspect.java
public class LoggingAspect {
public void logAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("logAround() is running!");
System.out.println("getName(): " + pjp.getSignature().getName());
System.out.println("before is running!");
pjp.proceed();
System.out.println("after is running!");
System.out.println("******");
}
}
File:applicationContext.xml
在XML同等功能 - 使用 <aop:after-around>.
<?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"
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">
<aop:aspectj-autoproxy/>
<bean id="customerBo" class="com.ray.customer.bo.impl.CustomerBoImpl"/>
<!-- Aspect -->
<bean id="logAspect" class="com.ray.aspect.LoggingAspect"/>
<aop:config>
<aop:aspect id="aspectLogging" ref="logAspect">
<!-- @Around -->
<aop:pointcut expression="execution(* com.ray.customer.bo.CustomerBo.addCustomerAround(..))" id="pointCutAround"/>
<aop:around method="logAround" pointcut-ref="pointCutAround"/>
</aop:aspect>
</aop:config>
</beans>
执行程序
public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerBo customer = (CustomerBo) context.getBean("customerBo");
customer.addCustomerAround("ray");
}
}
执行结果
logAround() is running!
getName(): addCustomerAround
before is running!
addCustomerAround() is running, args: ray
after is running!
******
完整的 XML 实例
File:LoggingAspect.java
package com.ray.aspect;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class LoggingAspect {
public void logBefore(JoinPoint joinPoint) {
System.out.println("logBefore() is running!");
System.out.println("getName() : " + joinPoint.getSignature().getName());
System.out.println("******");
}
public void logAfter(JoinPoint joinPoint) {
System.out.println("logAfter() is running!");
System.out.println("getName() : " + joinPoint.getSignature().getName());
System.out.println("******");
}
public void logAfterReturning(JoinPoint joinPoint, Object result) {
System.out.println("logAfterReturning() is running!");
System.out.println("getName() : " + joinPoint.getSignature().getName());
System.out.println("Method returned value is : " + result);
System.out.println("******");
}
public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
System.out.println("logAfterThrowing() is running!");
System.out.println("getName() : " + joinPoint.getSignature().getName());
System.out.println("Exception : " + error);
System.out.println("******");
}
public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("logAround() is running!");
System.out.println("getName() : " + joinPoint.getSignature().getName());
System.out.println("getArgs() : " + Arrays.toString(joinPoint.getArgs()));
System.out.println("Around before is running!");
joinPoint.proceed();
System.out.println("Around after is running!");
System.out.println("******");
}
}
File: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"
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">
<aop:aspectj-autoproxy/>
<bean id="customerBo" class="com.ray.customer.bo.impl.CustomerBoImpl"/>
<!-- Aspect -->
<bean id="logAspect" class="com.ray.aspect.LoggingAspect"/>
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect" >
<!-- @Before -->
<aop:pointcut id="pointCutBefore"
expression="execution(* com.ray.customer.bo.CustomerBo.addCustomer(..))" />
<aop:before method="logBefore" pointcut-ref="pointCutBefore" />
<!-- @After -->
<aop:pointcut id="pointCutAfter"
expression="execution(* com.ray.customer.bo.CustomerBo.addCustomer(..))" />
<aop:after method="logAfter" pointcut-ref="pointCutAfter" />
<!-- @AfterReturning -->
<aop:pointcut id="pointCutAfterReturning"
expression="execution(* com.ray.customer.bo.CustomerBo.addCustomerReturnValue(..))" />
<aop:after-returning method="logAfterReturning" returning="result"
pointcut-ref="pointCutAfterReturning" />
<!-- @AfterThrowing -->
<aop:pointcut id="pointCutAfterThrowing"
expression="execution(* com.ray.customer.bo.CustomerBo.addCustomerThrowException(..))" />
<aop:after-throwing method="logAfterThrowing" throwing="error"
pointcut-ref="pointCutAfterThrowing" />
<!-- @Around -->
<aop:pointcut id="pointCutAround"
expression="execution(* com.ray.customer.bo.CustomerBo.addCustomerAround(..))" />
<aop:around method="logAround" pointcut-ref="pointCutAround" />
</aop:aspect>
</aop:config>
</beans>
测试程序
package com.ray.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ray.customer.bo.CustomerBo;
public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerBo customer = (CustomerBo) context.getBean("customerBo");
// customer.addCustomer();
// customer.addCustomerReturnValue();
// customer.addCustomerThrowException();
customer.addCustomerAround("ray");
}
}