软件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. javascript中的with关键字

    说起js中的with关键字,很多小伙伴们的第一印象可能就是with关键字的作用在于改变作用域,然后最关键的一点是不推荐使用with关键字.听到不推荐with关键字后,我们很多人都会忽略掉with关键字 ...

  2. php CI框架log写入

    1.首先,打开application下的config.php文件,将log配置打开如下 /* |---------------------------------------------------- ...

  3. SSM文件上传

    **自己对于SSM文件上传的一些心得** 刚开始的时候也是在网上寻找一些简单的案例,可能我的这篇文章不是最好的,但是这些都是我自己慢慢的摸索以及自己的尝试的一些心得,希望对各位有所帮助. 其实文件的上 ...

  4. OpenvSwitch 解读

    OpenvSwitch 解读 报文匹配流程参考下图 调用流程(内核): ovs_vport_receive->ovs_dp_process_received_packet->ovs_flo ...

  5. python 返回数组的索引

    使用python里的index nums = [1, 2, 3, 4, 5, 6, 1, 9] print nums.index(max(nums)) print nums.index(1) 该方法同 ...

  6. arya-sites模块的主要类

      Site类,生成路由,    - 方法:url,get_urls, register, login,logout   - 字段:_registry = {}    Config,基础配置类,主要用 ...

  7. Android中获取系统内存信息以及进程信息-----ActivityManager的使用(一)

    本节内容主要是讲解ActivityManager的使用,通过ActivityManager我们可以获得系统里正在运行的activities,包括 进程(Process)等.应用程序/包.服务(Serv ...

  8. 用absolute进行页面的自适应布局

    用position:absolute和top,left,bottom,right进行设置可以进行页面的头部,底部,左边框,内容的自适应布局,可以代替position:fixed; <!DOCTY ...

  9. MiniUi-----Spinner 数值调节器(可以实现任意值的递增)

    Spinner 数值调节器可以实现任意值的递增,每次的递增值主要是通过increment="递增值"属性来控制的. 属性 该属性扩展自验证框(validatebox),下面是为微调 ...

  10. 【OpenCV3】cvRound()、cvFloor()、cvCeil()函数详解

    函数cvRound().cvFloor().cvCeil()都是按照一种舍入方式将浮点型数据转换为整型数据. cvRound():返回跟参数最接近的整数值,即四舍五入: cvFloor()  :返回不 ...