最近有点时间分享一个自己写的最为简单的Spring AOP的应用,只是把一些个人的理解分享下提供以后参考。可能很多人刚开始不太理解AOP到底是啥,其实它也是相对OOP来说的,类似OOP其实也是一种编程思想吧。本人暂且把Spring 中的AOP理解成一种方法的拦截器。
这是一个比较常见的理解方式,例如就好比你去自动取款机取钱,边上装了个摄像头在监视着。你取你的钱,不用管那摄像头干嘛,只是对于摄像头来说,已经把你取钱的这一过程记录了下来。你取钱的这一过程我们可以从OOP角度分析,而对于摄像头来说,就是从AOP角度去分析了。反映到我下面要讲的示例就是系统日志的记录。
导入所需的jar包
2.创建服务类和接口
服务类清单路径:springAOP01\WEB-INF\classes\com\configuration\service\dao\PersionImpl
接口类清单路径:springAOP01\WEB-INF\classes\com\configuration\service\dao\Persion
切面类清单路径: springAOP01\WEB-INF\classes\com\configuration\service\dao\Interceptor
(1)服务类PersionImpl.java 代码:
package com.configuration.service.dao;
public class PersionImpl implements Persion
{
public void addPersion(String username, String password)
{
System.out.println("--------PersionImpl.addUser()----------");
}
package com.configuration.service.dao;
public interface Persion
{
public abstract void addPersion(String username, String password);
package com.configuration.service.dao;
import org.aspectj.lang.ProceedingJoinPoint;
public class Interceptor
{
public void doBefore()
{
System.out.println("----------------执行前置通知-----------------");
}
public void doAfterReturning()
{
System.out.println("----------------执行后置通知-----------------");
}
public void doAfter()
{
System.out.println("----------------执行最终通知-----------------");
}
public void doAfterThrowing()
{
System.out.println("----------------执行意外通知-----------------");
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable
{
System.out.println("----------------进入判断方法-----------------");
Object result=pjp.proceed();
System.out.println("----------------退出判断方法-----------------");
return result;
}
}
<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/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="interceptorBean" class="com.configuration.service.dao.Interceptor"/>
<bean id="persionBean" class="com.configuration.service.dao.PersionImpl"/>
<!-- 配置AOP -->
<aop:config>
<!-- 配置aspect切面类 -->
<aop:aspect id="interceptor" ref="interceptorBean">
<!-- 配置pointcut,即切入点,对哪些类的哪些方法起到AOP的作用 -->
<aop:pointcut id="persionImpl" expression="execution (* com.configuration.service.dao.PersionImpl.*(..))"/>
<!-- 配置advice,即Interceptor类中的doBefore()方法,这里采用在业务方法执行前进行拦截 -->
<aop:before pointcut-ref="persionImpl" method="doBefore"/>
<aop:after-returning pointcut-ref="persionImpl" method="doAfterReturning"/>
<aop:after pointcut-ref="persionImpl" method="doAfter"/>
<aop:after-throwing pointcut-ref="persionImpl" method="doAfterThrowing"/>
<aop:around pointcut-ref="persionImpl" method="doAround"/>
</aop:aspect>
</aop:config>
</beans>
测试类:
清单路径:springAOP\WEB-INF\classes\com\configuration\service\dao\Test
测试类代码如下:
package com.configuration.service.dao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test
{
public static void main(String[] args)
{
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Persion manager=(Persion)context.getBean("persionBean");
manager.addUser("测试", "测试");
}
备注:如果那些地方写的不清楚,请给本人留言。
这是一个比较常见的理解方式,例如就好比你去自动取款机取钱,边上装了个摄像头在监视着。你取你的钱,不用管那摄像头干嘛,只是对于摄像头来说,已经把你取钱的这一过程记录了下来。你取钱的这一过程我们可以从OOP角度分析,而对于摄像头来说,就是从AOP角度去分析了。反映到我下面要讲的示例就是系统日志的记录。
废话就到此,让我们进入Spring AOP第一个例子:
导入所需的jar包

2.创建服务类和接口
服务类清单路径:springAOP01\WEB-INF\classes\com\configuration\service\dao\PersionImpl
接口类清单路径:springAOP01\WEB-INF\classes\com\configuration\service\dao\Persion
切面类清单路径: springAOP01\WEB-INF\classes\com\configuration\service\dao\Interceptor
(1)服务类PersionImpl.java 代码:
package com.configuration.service.dao;
public class PersionImpl implements Persion
{
public void addPersion(String username, String password)
{
System.out.println("--------PersionImpl.addUser()----------");
}
}
package com.configuration.service.dao;
public interface Persion
{
public abstract void addPersion(String username, String password);
}
package com.configuration.service.dao;
import org.aspectj.lang.ProceedingJoinPoint;
public class Interceptor
{
public void doBefore()
{
System.out.println("----------------执行前置通知-----------------");
}
public void doAfterReturning()
{
System.out.println("----------------执行后置通知-----------------");
}
public void doAfter()
{
System.out.println("----------------执行最终通知-----------------");
}
public void doAfterThrowing()
{
System.out.println("----------------执行意外通知-----------------");
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable
{
System.out.println("----------------进入判断方法-----------------");
Object result=pjp.proceed();
System.out.println("----------------退出判断方法-----------------");
return result;
}
}
3。创建一个applicationContext.xml配置文件,清单路径:springAOP\src\applicationContext.xml,配置AOP信息如下:
<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/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="interceptorBean" class="com.configuration.service.dao.Interceptor"/>
<bean id="persionBean" class="com.configuration.service.dao.PersionImpl"/>
<!-- 配置AOP -->
<aop:config>
<!-- 配置aspect切面类 -->
<aop:aspect id="interceptor" ref="interceptorBean">
<!-- 配置pointcut,即切入点,对哪些类的哪些方法起到AOP的作用 -->
<aop:pointcut id="persionImpl" expression="execution (* com.configuration.service.dao.PersionImpl.*(..))"/>
<!-- 配置advice,即Interceptor类中的doBefore()方法,这里采用在业务方法执行前进行拦截 -->
<aop:before pointcut-ref="persionImpl" method="doBefore"/>
<aop:after-returning pointcut-ref="persionImpl" method="doAfterReturning"/>
<aop:after pointcut-ref="persionImpl" method="doAfter"/>
<aop:after-throwing pointcut-ref="persionImpl" method="doAfterThrowing"/>
<aop:around pointcut-ref="persionImpl" method="doAround"/>
</aop:aspect>
</aop:config>
</beans>
测试类:
清单路径:springAOP\WEB-INF\classes\com\configuration\service\dao\Test
测试类代码如下:
package com.configuration.service.dao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test
{
public static void main(String[] args)
{
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Persion manager=(Persion)context.getBean("persionBean");
manager.addUser("测试", "测试");
}
}
输出信息:
----------------执行前置通知-----------------
----------------进入判断方法-----------------
--------PersionImpl.addUser()----------
----------------执行后置通知-----------------
----------------执行最终通知-----------------
----------------退出判断方法-----------------
备注:如果那些地方写的不清楚,请给本人留言。