软件152 余建强

1 使用 API 实现 AOP

新建一个用户接口:UserService

 package com.cqvie.aop.api;

 public interface UserService {

     public void add(String name);
public void update(String name);
public void delete(String name);
public void select(String name);
}

实现接口类:UserServiceImpl

 package com.cqvie.aop.api;

 public class UserServiceImpl implements UserService {

     @Override
public void add(String name) {
System.out.println("Add User " + name + " SUCCESS!");
} @Override
public void update(String name) {
System.out.println("Update User " + name + " SUCCESS!");
} @Override
public void delete(String name) {
System.out.println("Delete User " + name + " SUCCESS!");
} @Override
public void select(String name) {
System.out.println("Select User " + name + " SUCCESS!");
} }

写一个日志类,包括前置通知和后置通知:Log

 package com.cqvie.aop.api;

 import java.lang.reflect.Method;

 import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice; public class Log implements MethodBeforeAdvice, AfterReturningAdvice { /**
* 前置通知
* @param method 被调用方法对象
* @param args 被调用的方法参数
* @param target 被调用的方法的目标对象
*/
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName() + " 的 " +
method.getName() + "方法被执行···");
} /**
* 后置通知
* @param returnValue 返回值
* @param method 被调用的方法对象
* @param args 被调用的方法对象的参数
* @param target 被调用的方法对象的目标对象
*/
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName() + " 的 " +
method.getName() + "方法已成功执行!返回值为:" + returnValue);
System.out.println();
}
}

配置 Spring 的配置文件:applicationContext01.xml

 <?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class="com.cqvie.aop.api.UserServiceImpl"></bean>
<bean id="log" class="com.cqvie.aop.api.Log"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.cqvie.aop.api.UserServiceImpl.*(..))" id="pointcut"/>
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
</aop:config> </beans>

添加一个测试类:Test

 package com.cqvie.aop.api;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { @SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext01.xml");
UserService userService = (UserService) ac.getBean("userService");
userService.update("AngeYu");
userService.delete("AngeYu"); } }

运行结果:

2 自定义类实现 AOP

接口和接口的实现类不变,依旧是上面所提到的 UserService 和 UserServiceIpml

自己写一个日志类 Log,包含前置通知和后置通知

 package com.cqvie.aop.custom;

 public class Log {

     public void before() {
System.out.println("----- method start -----");
} public void after() {
System.out.println("----- method end -----");
} }

配置 Spring 的配置文件:applicationContext02.xml

 <?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class="com.cqvie.aop.custom.UserServiceImpl"></bean>
<bean id="log" class="com.cqvie.aop.custom.Log"></bean>
<aop:config>
<aop:aspect ref="log">
<aop:pointcut expression="execution(* com.cqvie.aop.custom.UserServiceImpl.*(..))" id="pointcut"/>
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config> </beans>

添加一个测试类:Test

 package com.cqvie.aop.custom;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { @SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext02.xml");
UserService userService = (UserService) ac.getBean("userService");
userService.update("AngeYu"); } }

运行结果:

3 使用注解实现 AOP

接口和接口的实现类不变,依旧是上面所提到的 UserService 和 UserServiceIpml

自己写一个日志类 Log,包含前置通知、后置通知、环绕通知

 package com.cqvie.aop.annotation;

 import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; @Aspect
public class Log { @Before("execution(* com.cqvie.aop.annotation.UserServiceImpl.*(..))")
public void before() {
System.out.println("----- method start -----");
} @After("execution(* com.cqvie.aop.annotation.UserServiceImpl.*(..))")
public void after() {
System.out.println("----- method end -----");
} @Around("execution(* com.cqvie.aop.annotation.UserServiceImpl.*(..))")
public Object around(ProceedingJoinPoint pj) throws Throwable {
System.out.println("--- around start ---");
System.out.println("方法签名:" + pj.getSignature());
Object result = pj.proceed();
System.out.println("--- around end ---");
return result;
} }

配置 Spring 的配置文件:applicationContext03.xml

 <?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class="com.cqvie.aop.annotation.UserServiceImpl"></bean>
<bean id="log" class="com.cqvie.aop.annotation.Log"></bean>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>

添加一个测试类:Test

 package com.cqvie.aop.annotation;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { @SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext03.xml");
UserService userService = (UserService) ac.getBean("userService");
userService.update("AngeYu"); } }

运行结果:

Spring AOP 的实现的更多相关文章

  1. 学习AOP之深入一点Spring Aop

    上一篇<学习AOP之认识一下SpringAOP>中大体的了解了代理.动态代理及SpringAop的知识.因为写的篇幅长了点所以还是再写一篇吧.接下来开始深入一点Spring aop的一些实 ...

  2. 学习AOP之认识一下Spring AOP

    心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...

  3. spring aop

    什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将 ...

  4. spring aop注解方式与xml方式配置

    注解方式 applicationContext.xml 加入下面配置 <!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy ...

  5. 基于Spring AOP的JDK动态代理和CGLIB代理

    一.AOP的概念  在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的 ...

  6. Spring AOP详解

    一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...

  7. Spring AOP实例——异常处理和记录程序执行时间

    实例简介: 这个实例主要用于在一个系统的所有方法执行过程中出线异常时,把异常信息都记录下来,另外记录每个方法的执行时间. 用两个业务逻辑来说明上述功能,这两个业务逻辑首先使用Spring AOP的自动 ...

  8. 从零开始学 Java - Spring AOP 实现用户权限验证

    每个项目都会有权限管理系统 无论你是一个简单的企业站,还是一个复杂到爆的平台级项目,都会涉及到用户登录.权限管理这些必不可少的业务逻辑.有人说,企业站需要什么权限管理阿?那行吧,你那可能叫静态页面,就 ...

  9. 从零开始学 Java - Spring AOP 实现主从读写分离

    深刻讨论为什么要读写分离? 为了服务器承载更多的用户?提升了网站的响应速度?分摊数据库服务器的压力?就是为了双机热备又不想浪费备份服务器?上面这些回答,我认为都不是错误的,但也都不是完全正确的.「读写 ...

  10. 从零开始学 Java - Spring AOP 拦截器的基本实现

    一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...

随机推荐

  1. 关于ListBox的几个问题

    Winfrom ListBox绑定数据源list界面不更新问题与绑定数据源不可CRUD问题 场景:获取一个listbox的选中项添加到另一个listbox中 解决方案-1:不要直接绑定DataSour ...

  2. 如何在 .NET Core 上测试库

    设置解决方案. 可使用以下命令实现此目的: mkdir SolutionWithSrcAndTest cd SolutionWithSrcAndTest dotnet new sln dotnet n ...

  3. c#使用NPOI快速导出到Excel

    接上篇博文<C#快速导出到excel>:由于此种方法不能导出成.xlsx格式,为解决此问题,本次分享使用NPOI. 参考:https://www.cnblogs.com/lazyneal/ ...

  4. JAVA 从头开始<二>

    一.JAVA_HOME 1.环境变量如果经常变更,就要经常操作到Path,可能会一不小心把什么东西给删了 2.最好新建一个环境变量 3.如果使用新环境变量 ①原来的写法 ②现在的写法 新建环境变量JA ...

  5. 附2 volatile

    注:在阅读本章之前,先要了解Java内存模型,见上一章<附1 Java内存模型与共享变量可见性>,链接如下: http://www.cnblogs.com/java-zhao/p/5124 ...

  6. app开发技术调研

    l  面向消费者与公众的应用系统,主要分为3种主流的渠道: 1.   web应用 2.   基于腾讯微信开放api构建的微信app 3.   移动端app ll 在移动端app方面,通过调研,现主流的 ...

  7. Day 10 动态参数&名称空间,局部全部.函数嵌套&global nonlocal关键字.

    一.动态参数#形参 有3种动态参数#*args 动态参数,不定长参数def func (*args): print(args,type(args))func(1,2,"alex", ...

  8. poj 107 DNA sorting

    关于Java的题解,也许效率低下,但是能解决不只是ACGT的序列字符串 代码如下: import java.util.*; public class Main { public static void ...

  9. JMeter基础:请求参数Parameters 、Body Data的区别

    使用Jmeter测试时,很多人不知道请求参数Parameters .Body Data的区别和用途,这里简单介绍下 先了解一个接口的基本概念 在客户机和服务器之间进行请求-响应时,HTTP协议中包括G ...

  10. Python小白学习之路(六)—— 【元祖】【元祖相关功能】

    元祖 tu = (111,'alex',(11,['aa','xhg',(78,43)],'aaa'),789,) 通过这个例子,我们看到元祖的特征: 是通过括号()括起来的 一般写元祖的时候,推荐子 ...