Spring实现AOP配置例子-springAOP01

51 篇文章 0 订阅
      最近有点时间分享一个自己写的最为简单的Spring AOP的应用,只是把一些个人的理解分享下提供以后参考。可能很多人刚开始不太理解AOP到底是啥,其实它也是相对OOP来说的,类似OOP其实也是一种编程思想吧。本人暂且把Spring 中的AOP理解成一种方法的拦截器。
这是一个比较常见的理解方式,例如就好比你去自动取款机取钱,边上装了个摄像头在监视着。你取你的钱,不用管那摄像头干嘛,只是对于摄像头来说,已经把你取钱的这一过程记录了下来。你取钱的这一过程我们可以从OOP角度分析,而对于摄像头来说,就是从AOP角度去分析了。反映到我下面要讲的示例就是系统日志的记录。

废话就到此,让我们进入Spring AOP第一个例子:


1。创建工程名称springAOP01
导入所需的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()----------");
        }

}


(2)接口类Persion.java 代码:

package com.configuration.service.dao;

public interface Persion
{

        public abstract void addPersion(String username, String password);

}


(3)切面类Interceptor.java 代码:

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信息如下:


<?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: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()----------
----------------执行后置通知-----------------
----------------执行最终通知-----------------
----------------退出判断方法-----------------


备注:如果那些地方写的不清楚,请给本人留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值