新手学习笔记Spring AOP全自动编程

业务类

package oyb.service;public interface IUserService {public void add();public void delete();
}
package oyb.service;public class UserServiceImpl implements IUserService {@Overridepublic void add() {System.out.println("添加用户。。。。");}@Overridepublic void delete() {System.out.println("删除用户");}
}

切面类

package oyb.aspect;import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;public class MyAspect implements MethodInterceptor{@Overridepublic Object invoke(MethodInvocation methodInvocation) throws Throwable {return methodInvocation.proceed();}
}

Spring配置文件

<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--配置UserService--><bean id="userService" class="oyb.service.UserServiceImpl"></bean><!--默认情况下Spring的AOP生成的代理是JDK的Proxy实现的--><!--配置切面对象--><bean id="myAspect" class="oyb.aspect.MyAspect"></bean><!--proxy-target-class为true表示开启cglib增强--><aop:config proxy-target-class="true"><aop:pointcut id="myPointcut" expression="execution(* oyb.service.*.*(..))"></aop:pointcut><!--通知 关联 切入点--><aop:advisor advice-ref="myAspect" pointcut-ref="myPointcut"></aop:advisor></aop:config></beans>
package oyb.test;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import oyb.service.IUserService;public class test {@Testpublic void test(){ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");IUserService userService = (IUserService) context.getBean("userService");userService.add();}
}

测试结果:

之前一直抛出Bean named 'myAspect' must be of type [org.aopalliance.aop.Advice],这个异常,检查之后发现切面类MyAspect中没有实现MethodInterceptor接口,所以会报这个错误。

这个简单的案例当中,导入了com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar这个包

这里简单介绍一下AspectJ,AspectJ是一个基于Java语言的AOP框架,在Spring 2.0以后新增了对AspectJ切点表达式支持,在新版本的Spring当中,大部分都使用AspectJ的方式来进行开发。

 下面介绍一下AspectJ的几种通知类型

1.before:前置通知,主要应用到各种校验。在方法执行前执行,如果通知抛出异常,方法将进行不了。

2.afterReturning:后置通知,主要应用于常规数据处理。方法执行如果抛出异常,则也执行不了。在方法执行后执行,可以获得方法的返回值。

3.around:环绕通知,

4.afterThrowing:抛出异常通知,方法抛出异常时执行,否则无法执行

5.after:最终通知,方法执行完毕之后执行,无论是否抛出异常

例子:

1.导入相应的包(基于xml配置):

2.业务类

package oyb.service;public interface IUserService {public void add();public void delete();public void update();
}
package oyb.service.impl;import oyb.service.IUserService;public class UserServiceImpl implements IUserService {@Overridepublic void add() {System.out.println("添加用户。。。");}@Overridepublic void delete() {System.out.println("删除用户。。。");}@Overridepublic void update() {System.out.println("更新用户。。。");}
}

3.切面类

package oyb.aspect;import org.aspectj.lang.JoinPoint;public class MyAspect {public void myBefore(JoinPoint joinPoint){System.out.println("前置通知");}public void myReturning(JoinPoint joinPoint){System.out.println("后置通知");}
}

配置文件

<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="userService" class="oyb.service.impl.UserServiceImpl"></bean><bean id="myAspect" class="oyb.aspect.MyAspect"></bean><aop:config><aop:aspect ref="myAspect"><aop:pointcut id="myPointcut" expression="execution(* oyb.service.impl.UserServiceImpl.*(..))"/><!--前置通知--><aop:before method="myBefore" pointcut-ref="myPointcut"></aop:before><!--后置通知--><aop:after-returning method="myReturning" pointcut-ref="myPointcut" ></aop:after-returning></aop:aspect></aop:config></beans>

测试类

package oyb.test;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import oyb.service.IUserService;public class test {@Testpublic  void test(){ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");IUserService userService = (IUserService) context.getBean("userService");userService.add();}
}

测试结果

 使用注解

Spring配置文件中

<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><context:component-scan base-package="oyb"></context:component-scan><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans>

业务类中添加注解如图

切面类

package oyb.aspect;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Component
@Aspect
public class MyAspect {//声明公共切入点@Pointcut("execution(* oyb.service.impl.UserServiceImpl.*(..))")public void myPointcut(){}@Before(value ="myPointcut()")public void myBefore(JoinPoint joinPoint){System.out.println("前置通知");}@AfterReturning(value = "myPointcut()")public void myReturning(JoinPoint joinPoint){System.out.println("后置通知");}
}

测试类

package oyb.test;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import oyb.service.IUserService;public class test {@Testpublic  void test(){ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");IUserService userService = (IUserService) context.getBean("userService");userService.add();}
}

测试结果如图:

 

转载于:https://www.cnblogs.com/ouyangbo/p/10610296.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/363314.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Gradle入门:我们的第一个Java项目

这篇博客文章描述了如何使用Gradle编译和打包一个简单的Java项目。 我们的Java项目只有一个要求&#xff1a; 我们的构建脚本必须创建一个可执行的jar文件。 换句话说&#xff0c;我们必须能够使用以下命令运行程序&#xff1a; java -jar jarfile.jar让我们找出如何满足这一…

特征码弊端渐显 杀毒技术面临革命

一种观点认为&#xff0c;防病毒与安全供应商们在与网络罪犯们的战斗中正逐步失去主动。黑客们的网络爬虫正越来越多的偷偷潜入计算机&#xff0c;植入恶意程序&#xff0c;打开计算机发送远程攻击指令&#xff0c;并把它们变为僵尸网络的僵尸军团。 造成这个局面的根本原因在于…

《金字塔原理》读书笔记

第一篇 表达的逻辑 1.金字塔原理序文 人们希望达到的境界&#xff1a;想清楚、说明白、知道说什么、怎么说。 需要清楚3件事情&#xff1a; 谁是我的听众&#xff1f; 他们想听什么&#xff1f; 他们想怎么样听&#xff1f; 金字塔的基本原理 金字塔原理是一种重点突出、逻…

9 个鲜为人知的 Python 数据科学库

除了 pandas、scikit-learn 和 matplotlib&#xff0c;还要学习一些用 Python 进行数据科学的新技巧。Python 是一种令人惊叹的语言。事实上&#xff0c;它是世界上增长最快的编程语言之一。它一次又一次地证明了它在各个行业的开发者和数据科学者中的作用。Python 及其库的整个…

列出和过滤NIO.2中的目录内容

在Java 7发行之前&#xff0c;列出目录内容的领域并没有发生太多的事情。但是&#xff0c;由于NIO.2引入了一种新的方式来做到这一点&#xff0c;因此涵盖这一领域可能是值得的。 NIO.2的一大优点是能够在一个方法调用中立即使用列表和过滤。 这为与文件系统相关的大多数列表/筛…

Windows Mobile 编程 (Win32) - 获取设备能力

《Windows 程序设计》第五章重点讲述图形基础。首先一个示例代码是获取设备描述表信息。代码与Windows Mobile 编程 (Win32) - 输出文本中的代码类似。 #include <windows.h>#define NUMLINES ((int)(sizeof devcaps / sizeof devcaps[0]))struct {int iIndex;TCHAR …

Netty : writeAndFlush的线程安全及并发问题

使用Netty编程时&#xff0c;我们经常会从用户线程&#xff0c;而不是Netty线程池发起write操作&#xff0c;因为我们不能在netty的事件回调中做大量耗时操作。那么问题来了 – 1&#xff0c; writeAndFlush是线程安全的吗&#xff1f; 2&#xff0c; 是否使用了锁&#xff0c;…

[翻译-ASP.NET MVC]Contact Manager开发之旅

本翻译系列为asp.net mvc官方实例教程。在这个系列中&#xff0c;Stephen Walther将演示如何通过ASP.NET MVC framework结合单元测试、TDD、Ajax、软件设计原则及设计模式创建一个完整的Contact Manager应用。本系列共七个章节&#xff0c;也是七次迭代过程。本人将陆续对其进行…

数据库 日期格式操作

sql server: 日期转字符串-日期select CONVERT(varchar(100), GETDATE(), 23) from RegionRealtimeData 日期转字符串-全select CONVERT(varchar(100), GETDATE(), 20) from RegionRealtimeData 字符串转日期-日期select CONVERT(date, 2016-02-11, 23) from RegionRealtimeDat…

jsp输出所有请求头的名称

Enumeration headernamesrequest.getHeaderNames();while(headernames.hasMoreElements()){String headernameheadernames.nextElement();out.println(headername "-->" request.getHeader(headername) "");}out.println("");更多专业前端知识…

Spring4:具有Java 8 Date-Time API的@DateTimeFormat

在Spring 3.0中作为Formatter SPI的一部分引入的DateTimeFormat批注可用于解析和打印Web应用程序中的本地化字段值。 在Spring 4.0中&#xff0c; DateTimeFormat批注可以直接与Java 8 Date-Time API&#xff08; java.time &#xff09;一起使用。 在Spring中&#xff0c;可以…

一、rollup

参考&#xff1a;reduxreach-routerrollup-starter-librollup-starter-approller-clicreate-react-library 一、安装 npm install --global rollup二、命令&#xff1a; rollup -c 默认指向rollup.config.jsimport babel from rollup-plugin-babel; import commonjs from ro…

从一本书看经济危机中创业者的机会

最近抽时间在看一本书《赢道&#xff1a;成功创业者的28条戒律》&#xff0c;赢道营销总裁邓超明、中国企业家联合会秘书长刘洋和资深IT经理人代腾飞三位创业者联手所写。就如同网上所介绍的&#xff0c;这本书分析了近30年来国内外100位风云人物创业成败之道&#xff0c;讲述了…

JSF中run项目时候Tomcat8启动不了的一种方法

把另一个博客内容迁移到这 我的问题是Tomcat是可以启动的 但是run那个jsp的时候七月 10, 2016 3:14:54 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property source to org.eclipse…

elasticsearch_dsl.exceptions.ValidationException: You cannot write to a wildcard index.

elasticsearch_dsl.exceptions.ValidationException: You cannot write to a wildcard index. 这里是因为版本不匹配的问题 查看es版本方法如下&#xff1a; 查看elasticsearch包与elasticsearch-dsl版本方法&#xff08;pip list&#xff09;如下&#xff1a; 因为我的es是5.1…

ListView执行notifyDatasetChanged无数据显示,getView未执行

自定义的一个ListView放到布局文件中&#xff0c;设置widthmatch_parent&#xff0c;heightwrap_content。 设置数据后执行notifyDatasetChanged。可以确定数据发生了变化&#xff0c;但是没有进入到getView中刷新数据。 经过尝试&#xff0c;设置height为match_parent之后数据…

在Java中对Singleton类进行双重检查锁定

Singleton类在Java开发人员中非常常见&#xff0c;但是它给初级开发人员带来了许多挑战。 他们面临的主要挑战之一是如何使Singleton保持为Singleton&#xff1f; 也就是说&#xff0c;无论出于何种原因&#xff0c;如何防止单个实例的多个实例。 对Singleton进行双重检查锁定是…

【转】解析.Net框架下的XML编程技术

【引自突破思维的禁忌的博客】一、前言 XML是微软.Net战略的一个重要组成部分&#xff0c;而且它可谓是XML Web服务的基石&#xff0c;所以掌握.Net框架下的XML技术自然显得非常重要了。本文将指导大家如何运用C#语言完成.Net框架下的XML文档的读写操作。首先&#xff0c;我会向…

line-height 属性

p.small {line-height:90%} p.big {line-height:200%} 该属性会影响行框的布局。在应用到一个块级元素时&#xff0c;它定义了该元素中基线之间的最小距离而不是最大距离。 line-height 与 font-size 的计算值之差&#xff08;在 CSS 中成为“行间距”&#xff09;分为两半&…

wstngfw中使用Viscosity连接OpenV-P-N服务器

wstngfw中使用Viscosity连接OpenV-P-N服务器 在本例中&#xff0c;将假设以下设置&#xff1a; 站点 A站点 B名称Beijing Office&#xff08;北京办公室&#xff09;名称Shenzheng Office&#xff08;深圳办公室&#xff09;WAN IP192.168.10.46WAN IP192.168.20.46LAN 子网192…