一、知识点
Spring支持在其AOP框架中使用AspectJ注解编写POJO aspect。但是,必须先在Spring IoC容器中启 用AspecJ注解支持。
只需要在Bean配置文件中定义一个空的XML元素<aop:aspectj-autoproxy>就可以启用Spring IoC容器中AspectJ注解支持。然后,Spring将自动为匹配你的AspectJ aspect的 所有Bean创建代理。
对于接口不可用或者没有用于应用程序设计中的情况,可以依靠CGLIB创建代理。为了启用 CGLIB创建代理,必须在<aop:aspectj-autoproxy>中设置 proxy-targetclass= true 属性。
二、代码示例package com.codeproject.jackie.springrecipesnote.springaop; /** * 加减乘除 * @author jackie * */ public interface ArithmeticCalculator { /** * 加 * @param a * @param b * @return */ public double add(double a, double b); /** * 减 * @param a * @param b * @return */ public double sub(double a, double b); /** * 乘 * @param a * @param b * @return */ public double mul(double a, double b); /** * 除 * @param a * @param b * @return */ public double div(double a, double b); }
package com.codeproject.jackie.springrecipesnote.springaop; /** * 单位转换 * @author jackie * */ public interface UnitCalculator { /** * 千克(或公斤)转换成磅 * @param kilogram * @return */ public double kilogramToPound(double kilogram); /** * 千米转换成英里 * @param kilometer * @return */ public double kilometerToMile(double kilometer); }相应的实现类
package com.codeproject.jackie.springrecipesnote.springaop; /** * 加减乘除实现类 * @author jackie * */ public class ArithmeticCalculatorlmpl implements ArithmeticCalculator { public double add(double a, double b) { double result = a + b; System.out.println(a + " + " + b + " = " + result); return result; } public double sub(double a, double b) { double result = a - b; System.out.println(a + " - " + b + " = " + result); return result; } public double mul(double a, double b) { double result = a * b; System.out.println(a + " * " + b + " = " + result); return result; } public double div(double a, double b) { if (b == 0) { throw new IllegalArgumentException("Division by zero"); } double result = a / b; System.out.println(a + " / " + b + " = " + result); return result; } }
package com.codeproject.jackie.springrecipesnote.springaop; /** * 单位转换实现类 * @author jackie * */ public class UnitCalculatorImpl implements UnitCalculator { public double kilogramToPound(double kilogram) { double pound = kilogram * 2.2; System.out.println(kilogram + " kilogram = " + pound + " pound"); return pound; } public double kilometerToMile(double kilometer) { double mile = kilometer * 0.62; System.out.println(kilometer + " kilometer = " + mile + " mile"); return mile; } }
为了启用这个应用的AspectJ注解支持,你只要在Bean配置文件中定义一个空的XML元素<aop:aspectj-autoproxy>。而且,你必须将aop schema定义添加到<beans>根元素下。当Spring IoC容器发现Bean配置文件中的<aop:aspectj-autoproxy>元素,它将自动为匹配AspecJ aspect的Bean创建代理。
注:如果使用Maven管理依赖,需添加以下依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${springframework.version}</version> </dependency> <!-- AspectJ --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.2</version> </dependency>
<?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-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <aop:aspectj-autoproxy /> <bean id="arithmeticCalculator" class="com.codeproject.jackie.springrecipesnote.springaop.ArithmeticCalculatorlmpl" /> <bean id="unitCalculator" class="com.codeproject.jackie.springrecipesnote.springaop.UnitCalculatorImpl" /> </beans>
@Test public void testAop() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator)applicationContext.getBean("arithmeticCalculator"); arithmeticCalculator.add(1, 2); arithmeticCalculator.sub(1, 2); arithmeticCalculator.mul(1, 2); arithmeticCalculator.div(1, 2); UnitCalculator unitCalculator = (UnitCalculator) applicationContext.getBean("unitCalculator"); unitCalculator.kilogramToPound(100); unitCalculator.kilometerToMile(100); }