实现Spring AOP注译例子-springAOP02

51 篇文章 0 订阅
      本章节也就是Spring AOP第二章节,在这章节中,本人继续做Spring AOP面向切面例子,上一章做的是以配置的方式来实现AOP,本章节使用注译方式(annotations)来实现,下面就把一些上一章节的理解AOP短文复制过来给大家理解一下。
    AOP到底是啥,也就是一个面向切面编程罢了,其实它也是相对OOP来说的,类似OOP其实也是一种编程思想。本人暂且把Spring 中的AOP理解成一种方法的拦截器。
这是一个比较常见的理解方式,例如就好比你去自动取款机取钱,边上装了个摄像头在监视着。你取你的钱,不用管那摄像头干嘛,只是对于摄像头来说,已经把你取钱的这一过程记录了下来。你取钱的这一过程我们可以从OOP角度分析,而对于摄像头来说,就是从AOP角度去分析了。反映到我下面要讲的示例就是系统日志的记录。
废话就到此,让我们进入Spring AOP第二个例子:
1。创建工程名称springAOP02

导入所需的jar包





2.创建服务类和接口

服务类清单路径
:springAOP02\WEB-INF\classes\com\annotations\service\dao\PersionImpl
接口类清单路径:springAOP02\WEB-INF\classes\com\annotations\service\dao\Persion
切面类清单路径:  springAOP02\WEB-INF\classes\com\annotations\service\dao\Interceptor

(1)服务类PersionImpl.java 代码:

package com.annotations.service.dao;

import org.springframework.stereotype.Service;

//@Service 这里是spring注入,如果要用这种方式则去掉spring中的<bean id="persionImp" class="com.annotations.service.PersionImp"/>
public class PersionImp implements Persion {

        public String getPersion(String name) {
                System.out.println("调用getPersion()");
                return "test";
        }
}

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

package com.annotations.service.dao;

public interface Persion {

        public String getPersion(String name);

}

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

package com.annotations.service.dao;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;

/*
 * 切面
 */
@Aspect
// 声名切面
@Service
// 类扫描功能
public class Interceptor {
        //标注说明   execution (返回值 包.类.方法名(参数))
        @Pointcut("execution (* com.annotations.service.dao.PersionImp.*(..))")
        private void anyMethod() {
        };// 声名切入点
        //@Before("anyMethod() && args(userName)")//切入点的名称,附加条件:要求有String类型的参数,即这里只拦截save(String name)方法
        @Before("anyMethod()")
        public void doBefore() {
                System.out.println("----------------执行前置通知-----------------");
        }

        @AfterReturning("anyMethod()")
        public void doAfterReturning() {
                System.out.println("----------------执行后置通知-----------------");
        }

        @After("anyMethod()")
        public void doAfter() {
                System.out.println("----------------执行最终通知-----------------");
        }

        @AfterThrowing("anyMethod()")
        public void doAfterThrowing() {
                System.out.println("----------------执行意外通知-----------------");
        }

        @Around("anyMethod()")
        public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
                System.out.println("----------------进入判断方法-----------------");
                Object result = pjp.proceed(); // 该方法必须被执行,主要获取方法的返回参数
                System.out.println("----------------退出判断方法-----------------" + result);
                return result;
        }
}

3。创建一个applicationContext.xml配置文件,清单路径:springAOP02\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">

        <!-- 通过aop命名空间的<aop:aspectj-autoproxy />声明自动为spring容器中
        那些配置@aspectJ切面的bean创建代理,织入切面。当然,spring -->
        <aop:aspectj-autoproxy />
        <!-- 打开自动加载 -->
        <context:component-scan base-package="com.annotations.service.dao" />
        <!--类扫描器-->
        <bean id="persionImp" class="com.annotations.service.dao.PersionImp"/>
</beans>


测试类:
清单路径
:springAOP02\WEB-INF\classes\com\annotations\service\dao\Test

测试类代码如下:

package com.annotations.service.dao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.annotations.service.dao.Persion;


public class Test {

        /**
         * @param args
         */
        public static void main(String[] args) {
                ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
                Persion ser = (Persion) app.getBean("persionImp");
                ser.getPersion("测试");

        }
}

输出信息:
----------------执行前置通知-----------------
----------------进入判断方法-----------------
调用getPersion()
----------------执行后置通知-----------------
----------------执行最终通知-----------------
----------------退出判断方法-----------------test

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值