一、实现接口MethodBeforeAdvice该拦截器会在调用方法前执行

            实现接口   AfterReturningAdvice该拦截器会在调用方法后执行

            实现接口  MethodInterceptor该拦截器会在调用方法前后都执行,实现环绕结果。

  1. package com.ly.model;
  2. import java.lang.reflect.Method;
  3. import org.aopalliance.intercept.MethodInterceptor;
  4. import org.aopalliance.intercept.MethodInvocation;
  5. import org.springframework.aop.AfterReturningAdvice;
  6. import org.springframework.aop.MethodBeforeAdvice;
  7. public class Advice implements MethodBeforeAdvice,AfterReturningAdvice,MethodInterceptor{
  8. @Override
  9. public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
  10. saveBeforeMessage();
  11. }
  12. @Override
  13. public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
  14. saveAfterMessage();
  15. }
  16. public void saveBeforeMessage(){
  17. System.out.println("调用BeforeAdvice成功");
  18. }
  19. public void saveAfterMessage(){
  20. System.out.println("调用AfterAdvice成功");
  21. }
  22. @Override
  23. public Object invoke(MethodInvocation arg0) throws Throwable {
  24. System.out.println("调用RoundService之前成功");
  25. Object result=arg0.proceed();
  26. System.out.println("调用RoundService之后成功");
  27. return result;
  28. }
  29. }

以下为AOPservice的实现

  1. package com.ly.service.impl;
  2. import com.ly.service.AOPService;
  3. public class AOPServiceImpl implements AOPService{
  4. @Override
  5. public void print(String message) {
  6. System.out.println(message);
  7. }
  8. @Override
  9. public void save() {
  10. System.out.println("保存信息成功");
  11. }
  12. }

以下为配置文件信息

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context.xsd">
  8. <!--目标对象 -->
  9. <bean id="AOPservice" class="com.ly.service.impl.AOPServiceImpl">
  10. </bean>
  11. <!-- advice通知 -->
  12. <bean id="adviceMessage" class="com.ly.model.Advice"></bean>
  13. <!-- 切入点adviser -->
  14. <bean id="adviser" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
  15. <property name="advice" ref="adviceMessage"></property>
  16. <!-- pattern的值使用正则表达式精确指定切入点 ,将print方法设为切入点 -->
  17. <property name="pattern"
  18. value="com\.ly\.service\.impl\.AOPServiceImpl\.print"></property>
  19. </bean>
  20. <!-- 代理对象 返回实例是目标对象 target属性指定的AOPservice对象-->
  21. <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
  22. <!-- 设置目标(target)bean为 AOPservice,
  23. 多个bean可以设置list集合如
  24. <property name="interceptorNames">
  25. <list>
  26. <value>advisor</value>
  27. <value>advisor1</value>
  28. </list>
  29. </property>-->
  30. <property name="target">
  31. <ref bean="AOPservice" />
  32. </property>
  33. <!--源码内固定的属性private String[] interceptorNames;  -->
  34. <property name="interceptorNames">
  35. <value>adviser</value>
  36. </property>
  37. </bean>
  38. </beans>

以下为测试类

  1. package com.ly.control;
  2. import java.io.IOException;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import org.springframework.core.io.ClassPathResource;
  6. import org.springframework.core.io.FileSystemResource;
  7. import org.springframework.core.io.Resource;
  8. import org.springframework.core.io.UrlResource;
  9. import com.ly.service.AOPService;
  10. public class SpringTest {
  11. public static void main(String[] args) throws IOException {
  12. ApplicationContext applicationContext=new ClassPathXmlApplicationContext(new String[]{"springBeans.xml"});
  13. AOPService aopservice=  (AOPService) applicationContext.getBean("proxyService");
  14. //由于在springBeans.xml中只配置了 value="com\.ly\.service\.impl\.AOPServiceImpl\.print"适配print方法,没有适配save方法,故只有调用print方法时才会执行advice通知
  15. aopservice.print("调用print成功==========》");
  16. aopservice.save();
  17. }
  18. }

二、在web应用中调用时需要用WebApplicationContext这个来得到Spring配置中的bean类

    1. public class UserAction extends ActionSupport {
    2. private static final Logger log = Logger.getLogger(UserAction.class);
    3. private UserService userService;
    4. public UserService getUserService(){
    5. if(userService == null){
    6. WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getRequest().getServletContext());
    7. userService = (UserService) applicationContext.getBean("proxyService");
    8. //          userService = new UserServiceImpl();
    9. }
    10. return userService;
    11. }

AOP切点切面内容的更多相关文章

  1. 【原创】Android AOP面向切面编程AspectJ

    一.背景: 在项目开发中,对 App 客户端重构后,发现用于统计用户行为的友盟统计代码和用户行为日志记录代码分散在各业务模块中,比如在视频模块,要想实现对用户对监控点的实时预览和远程回放行为进行统计, ...

  2. 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~

    简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...

  3. AOP 日志切面

    AOP把软件的功能模块分为两个部分:核心关注点和横切关注点.业务处理的主要功能为核心关注点,而非核心.需要拓展的功能为横切关注点.AOP的作用在于分离系统中的各种关注点,将核心关注点和横切关注点进行分 ...

  4. AOP面向切面编程的四种实现

     一.AOP(面向切面编程)的四种实现分别为最原始的经典AOP.代理工厂bean(ProxyFacteryBean)和默认自动代理DefaultAdvisorAutoProxyCreator以及Bea ...

  5. 如何通过自定义注解实现AOP切点定义

    面向切面编程(Aspect Oriented Programming, AOP)是面向对象编程(Object Oriented Programming,OOP)的强大补充,通过横切面注入的方式引入其他 ...

  6. Spring AOP 创建切面

        增强被织入到目标类的所有方法中,但是如果需要有选择性的织入到目标类某些特定的方法中时,就需要使用切点进行目标连接点的定位.增强提供了连接点方位信息:如织入到方法前面.后面等,而切点进一步描述织 ...

  7. 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十 || AOP面向切面编程浅解析:简单日志记录 + 服务切面缓存

    代码已上传Github+Gitee,文末有地址 上回<从壹开始前后端分离[ .NET Core2.0 Api + Vue 2.0 + AOP + 分布式]框架之九 || 依赖注入IoC学习 + ...

  8. Spring:AOP面向切面编程

    AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...

  9. Spring AOP 切点(pointcut)表达式

    这遍文章将介绍Spring AOP切点表达式(下称表达式)语言,首先介绍两个面向切面编程中使用到的术语. 连接点(Joint Point):广义上来讲,方法.异常处理块.字段这些程序调用过程中可以抽像 ...

随机推荐

  1. OkHttp完全解析之整体调用流程

    前言:阅读好的代码如同观赏美景一样的美妙 OkHttp是一个Square公司在github开源的Java网络请求框架,非常流行.OkHttp 的代码并不是特别庞大,代码很多巧妙的实现,非常值得学习. ...

  2. javascrapy 正则表达式验证 IP和MAC

    var psrc = $('input[name="psrc"]').val() var hwsrc = $('input[name="hwsrc"]').va ...

  3. 在mysql语句中为什么要加反引号

    在MySQL语句中我们有时候经常会遇到反引号(``),刚开始的时候不知道什么意思,他是什么作用呢? Select * from `member` order by posts desc limit 0 ...

  4. The method setItems(String) in the type ForTokensTag is not applicable for the arguments (Object)

    1. 问题 看到这个错误以为是貌似jsp页面有误,c:forTokens标签用错了?? An error occurred at line: in the jsp file: /WEB-INF/pag ...

  5. kotlin语法

    https://try.kotlinlang.org/#/Examples/Hello,%20world!/Simplest%20version/Simplest%20version.kt /** * ...

  6. 关于ES7里面的async和await

    async / await是ES7的重要特性之一,也是目前社区里公认的优秀异步解决方案.目前,async / await这个特性已经是stage 3的建议,可以看看TC39的进度,本篇文章将分享asy ...

  7. 关于YARN的基本结构

  8. Node Sass could not find a binding for your current environment

    Node环境从8升级到10后,Node Sass could not find a binding for your current environment 标签(空格分隔): Node Node环境 ...

  9. 推荐一个可以把网页背景色调成护眼色的Chrome扩展应用

    程序员一天有10几个小时要面对着电脑,老是这种白晃晃的屏幕,谁的眼睛受得了? 我在网上逛了一圈,找到一个比较实用的Chrome扩展应用,可以一键实现将Chrome打开网页的背景色修改成护眼的豆沙绿,这 ...

  10. vim使用常看

    原网址http://www.runoob.com/linux/linux-vim.html 补充参考https://blog.csdn.net/w178191520/article/details/8 ...