新手学习笔记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,一经查实,立即删除!

相关文章

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

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

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;可以…

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…

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

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

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…

开张了!

今天开张了&#xff0c;试试看&#xff01; Code1using System; 2using System.Collections.Generic; 3using System.Text; 4 5namespace Model 6{ 7 public enum SiteType 8 { System,External,All}; 9 [Serializable]10 class SiteInfo11 {12 public i…

dubbo和zookeeper的关系

转载前言&#xff1a;网络上很多教程没有描述zookeeper和dubbo到底是什么关系、分别扮演了什么角色等信息&#xff0c;都是说一些似是而非的话&#xff0c;这里终于找到一篇文章&#xff0c;比较生动地描述了注册中心和微服务框架之间的关系&#xff0c;以及他们之间的合作分工…

Flink学习(二)Flink中的时间

摘自Apache Flink官网 最早的streaming 架构是storm的lambda架构 分为三个layer batch layerserving layerspeed layer一、在streaming中Flink支持的通知时间 Flink官网写了个了解streaming和各种时间的博客 https://www.oreilly.com/ideas/the-world-beyond-batch-streaming-1…

RSS阅读器使用:ROME,Spring MVC,嵌入式Jetty

在这篇文章中&#xff0c;我将展示一些创建Spring Web应用程序的准则&#xff0c;使用Jetty以及使用名为ROME的外部库运行RSS来运行它。 一般 我最近创建了一个示例Web应用程序&#xff0c;充当RSS阅读器。 我想检查ROME以阅读RSS。 我还想使用Spring容器和MVC创建最简单的视图…

HZOJ string

正解炸了…… 考试的时候想到了正解&#xff0c;非常高兴的打出来了线段树&#xff0c;又调了好长时间&#xff0c;对拍了一下发现除了非常大的点跑的有点慢外其他还行。因为复杂度算着有点高…… 最后正解死于常数太大……旁边的lyl用同样的算法拿了90分我却拿了个暴力的分40……

Unity3D入门其实很简单

在上次发布拙作后&#xff0c;有不少童鞋询问本人如何学习Unity3D。本人自知作为一名刚入门的菜鸟&#xff0c;实在没有资格谈论这么高大上的话题&#xff0c;生怕误导了各位。不过思来想去&#xff0c;决定还是写一些自己的经验&#xff0c;如果能给想要入门U3D的您一些启发&a…

4. HTML表单标签

表单是网页中最常见的元素&#xff0c;也是用户和我们交互的重要手段&#xff0c;在网站中的登录、注册、信息更新这些功能都是依赖表单实现的。在HTML中对于表单提供了一系列的标签&#xff0c;即输入框、下拉框、按钮、文本域&#xff0c;如下是一个最常见的表单结构内容&…

为Lucene选择快速唯一标识符(UUID)

大多数使用Apache Lucene的搜索应用程序都会为每个索引文档分配唯一的ID&#xff08;即主键&#xff09;。 尽管Lucene本身不需要这样做&#xff08;它可能不太在乎&#xff01;&#xff09;&#xff0c;但应用程序通常需要它以后通过其外部ID替换&#xff0c;删除或检索该文档…

ubuntu16.04设置静态ip

最近在课堂上&#xff0c;有很多同学反映在搭建环境的时候&#xff0c;虚拟机ip经常变&#xff0c;那么我们配置好的web服务可能就不能用了。下面讲一下如何在ubuntu上面设置静态ip 1&#xff1a;首先我们确认一下ubuntu的版本 cat /etc/issue 或者sudo lsb_release -a或者unam…

Maven常用的构建命令

Maven常用命令&#xff1a; Maven库&#xff1a; http://repo2.maven.org/maven2/ Maven依赖查询&#xff1a; http://mvnrepository.com/ 一&#xff0c;Maven常用命令&#xff1a; 1. 创建Maven的普通Java项目&#xff1a; mvn archetype:create-DgroupIdpackageName-Dartifa…

课时85.层叠性(掌握)

1.什么是层叠性&#xff1f; 层叠性就是CSS处理冲突的一种能力。 这个字体最终会变为红色 注意点&#xff1a; 层叠性只有在多个选择器选中“同一个标签”,然后又设置了“相同的属性”&#xff0c;才会发生层叠性。 CSS全称&#xff1a;Cascading StyleSheet 层叠样式表&am…

SetProcessWorkingSetSize减少内存占用

系统启动起来以后&#xff0c;内存占用越来越大&#xff0c;使用析构函数、GC.Collect什么的也不见效果&#xff0c;后来查了好久&#xff0c;找到了个办法&#xff0c;就是使用 SetProcessWorkingSetSize函数。这个函数是Windows API 函数。下面是使用的方法&#xff1a;[Syst…

Spring Boot 与消息 (JMS、AMQP、RabbitMQ)

RabbitMQ教程 - 鸟哥的专栏 - CSDN博客 一、概述 大多应用中&#xff0c;可通过消息服务中间件来提升系统异步通信、扩展解耦能力消息服务中两个重要概念&#xff1a;消息代理&#xff08;message broker)和目的地&#xff08;destination) 当消息发送者发送消息以后&#xff0…

JavaOne 2014 –有关提交的一些初步分析

这些天时间不多了。 并行发生的事情如此之多&#xff0c;当然&#xff0c;最重要的Java会议就是一切。 JavaOne 2014已经关闭了CfP门&#xff0c;投票正在进行中。 程序委员会几乎没有什么可以谈论的&#xff0c;但是去年跳过了这种分析之后&#xff0c;现在是我该寻求许可的时…