Spring之AOP编程
package cn.african.service; public interface UserService { public void addUser(); public void updateUser(); public void deleteUser(); } package cn.african.service; public class UserServiceImpl implements UserService { @Override public void addUser() { System.out.println("addUser..."); } @Override public void updateUser() { System.out.println("updateUser..."); } @Override public void deleteUser() { System.out.println("deleteUser..."); } }
2. 切面类
package cn.african.aspect; public class MyAspect { public void before() { System.out.println("before"); } public void after() { System.out.println("after"); } }
3. 工厂类
package cn.african.factory; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import cn.african.aspect.MyAspect; import cn.african.service.UserService; import cn.african.service.UserServiceImpl; public class MyFactoryBean { public static UserService createService() { // 1. 目标对象 UserService userService = new UserServiceImpl(); // 2. 切面对象 MyAspect myAspect = new MyAspect(); // 3. 代理对象 UserService proxyService = (UserService) Proxy.newProxyInstance( MyFactoryBean.class.getClassLoader(), userService.getClass().getInterfaces(), new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //前置通知 myAspect.before(); //执行目标类的方法 Object object = method.invoke(userService, args); //后置通知 myAspect.after(); return object; } }); //4. 返回代理对象 return proxyService; } }
InvocationHandler h)
package cn.african.test; import org.junit.Test; import cn.african.factory.MyFactoryBean; import cn.african.service.UserService; public class TestJdkProxy { @Test public void testJdkProxy() { UserService userService = MyFactoryBean.createService(); userService.addUser(); System.out.println("--------------"); userService.updateUser(); System.out.println("--------------"); userService.deleteUser(); } }

package cn.african.service; public class UserServiceImpl { public void addUser() { System.out.println("addUser..."); } public void updateUser() { System.out.println("updateUser..."); } public void deleteUser() { System.out.println("deleteUser..."); } }
2. 切面类
package cn.african.aspect; public class MyAspect { public void before() { System.out.println("before"); } public void after() { System.out.println("after"); } }
package cn.african.factory; import java.lang.reflect.Method; import org.springframework.cglib.proxy.Enhancer; import org.springframework.cglib.proxy.MethodInterceptor; import org.springframework.cglib.proxy.MethodProxy; import cn.african.aspect.MyAspect; import cn.african.service.UserService; import cn.african.service.UserServiceImpl; public class CglibBeanFactory { public static UserService createService() { // 目标类 UserServiceImpl userService = new UserServiceImpl(); // 切面类 MyAspect myAspect = new MyAspect(); // 1. 核心类 Enhancer enhancer = new Enhancer(); // 2. 设置父类 enhancer.setSuperclass(UserServiceImpl.class); // 3. 设置回调函数 enhancer.setCallback(new MethodInterceptor() { @Override public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { myAspect.before(); Object object = method.invoke(userService, args); myAspect.after(); return object; } }); // 4. 创建代理 UserServiceImpl proxyService = (UserServiceImpl) enhancer.create(); return proxyService; } }
package cn.african.test; import org.junit.Test; import cn.african.factory.CglibBeanFactory; import cn.african.service.UserService; public class TestCglibProxy { @Test public void demo01() { UserService userService = CglibBeanFactory.createService(); userService.addUser(); System.out.println("--------------"); userService.updateUser(); System.out.println("--------------"); userService.deleteUser(); } }

package org.aopalliance.aop; /** * Tag interface for Advice. Implementations can be any type * of advice, such as Interceptors. * @author Rod Johnson * @version $Id: Advice.java,v 1.1 2004/03/19 17:02:16 johnsonr Exp $ */ public interface Advice {}
try{ //1. 前置通知 //2. 执行目标方法 //3. 后置通知 }catch{ //4. 抛出异常通知 }
package cn.african.service; public interface UserService { void addUser(); void updateUser(); void deleteUser(); } package cn.african.service; public class UserServiceImpl implements UserService { @Override public void addUser() { System.out.println("addUser..."); } @Override public void updateUser() { System.out.println("updateUser..."); } @Override public void deleteUser() { System.out.println("deleteUser..."); } }
package cn.african.aspect; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class MyAspect implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { // 前置通知 System.out.println("before"); // 手动执行目标方法,这是一个放行方法 Object object = invocation.proceed(); // 后置通知 System.out.println("after"); return object; } }
3. bean-aop.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <!-- 1. 目标类 --> <bean id="userService" class="cn.african.service.UserServiceImpl"></bean> <!-- 2. 切面类 --> <bean id="myAspect" class="cn.african.aspect.MyAspect"></bean> <!-- 3. 代理类 --> <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="interfaces" value="cn.african.service.UserService"></property> <property name="target" ref="userService"></property> <property name="interceptorNames" value="myAspect"></property> <!-- <property name="optimize" value="true"></property> --> <!--如果声明optimize=true,无论是否有接口,都采用cglib代理--> </bean> </beans>
package cn.african.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.african.service.UserService; public class TestAop { @Test public void demo01() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("beans-aop.xml"); UserService userService = context.getBean("proxyService", UserService.class); userService.addUser(); System.out.println("----------------"); userService.updateUser(); System.out.println("----------------"); userService.deleteUser(); context.getClass().getMethod("close").invoke(context); } }
打印结果:
package cn.african.service; public interface UserService { public void addUser(); public void updateUser(); public void deleteUser(); } package cn.african.service; public class UserServiceImpl implements UserService { @Override public void addUser() { System.out.println("addUser..."); } @Override public void updateUser() { System.out.println("updateUser..."); } @Override public void deleteUser() { System.out.println("deleteUser..."); } }
package cn.african.aspect; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class MyAspect implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { // 前置通知 System.out.println("before"); // 手动执行目标方法,这是一个放行方法 Object object = invocation.proceed(); // 后置通知 System.out.println("after"); return object; } }
3. beans-aop.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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> <!-- 1. 目标类 --> <bean id="userService" class="cn.african.service.UserServiceImpl"></bean> <!-- 2. 切面类 --> <bean id="myAspect" class="cn.african.aspect.MyAspect"></bean> <!-- 3. AOP配置 --> <aop:config> <aop:pointcut expression="execution(* cn.african.service.UserServiceImpl.*(..))" id="myPointcut"/> <aop:advisor advice-ref="myAspect" pointcut-ref="myPointcut"/> </aop:config> </beans>
package cn.african.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.african.service.UserService; public class TestAop { @Test public void demo01() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("beans-aop.xml"); UserService userService = context.getBean("userService", UserService.class); userService.addUser(); System.out.println("----------------"); userService.updateUser(); System.out.println("----------------"); userService.deleteUser(); context.getClass().getMethod("close").invoke(context); } }
打印结果:
Spring之AOP编程的更多相关文章
- Java基础-SSM之Spring的AOP编程
Java基础-SSM之Spring的AOP编程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Spring的本质说白了就是动态代理,接下来我们会体验AOP的用法.它是对OOP的 ...
- Spring框架--AOP编程
2 手动实现AOP编程 AOP 面向切面的编程, AOP可以实现"业务代码"与"关注点代码"分离 // 保存一个用户 public void add(User ...
- Spring的AOP编程
1.手动实现AOP编程(代理模式) AOP是面向切面的编程,主要功能就是实现"业务代码"和辅助业务代码的"关注点代码"分离.在一个方法中,出了核心的业务代码,其 ...
- Java进阶知识21 Spring的AOP编程
1.概述 Aop:(Aspect Oriented Programming)面向切面编程 功能: 让关注点代码与业务代码分离! 关注点:重复代码就叫做关注点:切面: 关注点形成的类, ...
- spring的aop编程(半自动、全自动)
1.spring的半自动代理(从spring中获取代理对象) (1)spring中的通知类型 spring中aop的通知类型有五种: 前置:在目标方法执行前实施加强 后置:在目标方法执行后实施加强 环 ...
- 【AOP】spring 的AOP编程报错:[Xlint:invalidAbsoluteTypeName]error
AOP来发过程中,报错如下: warning no match for this type name: net.shopxx.wx.institution.controller [Xlint:inva ...
- spring相关—AOP编程—切入点、连接点
1 切入点表达式 1.1 作用 通过表达式的方式定位一个或多个具体的连接点. 1.2 语法细节 ①切入点表达式的语法格式 execution([权限修饰符] [返回值类型] [简单类名/全类名] [方 ...
- Spring基础篇——Spring的AOP切面编程
一 基本理解 AOP,面向切面编程,作为Spring的核心思想之一,度娘上有太多的教程啊.解释啊,但博主还是要自己按照自己的思路和理解再来阐释一下.原因很简单,别人的思想终究是别人的,自己的理解才是 ...
- Aop编程--注解与xml的实现
一.注解方式 1.首先引入spring对于aop编程的jar支持包,spring框架没有的包请自行在网上下载. aopalliance-alpha1.jar aspectjrt.jar aspectj ...
随机推荐
- 归并排序(非递归,Java实现)
归并排序(非递归):自底向上 public class MergeSort { /** * @param arr 待排序的数组 * @param left 本次归并的左边界 * @param mid ...
- maven 技术总结
1.版本统一控制 在 properties中配置一个参数,在添加依赖时 通过 version标签 限定版本 <properties> <org.springframework.ver ...
- 常用css样式颜色值: 64位真彩和256位值
1. background-color: #eee; 2. background-color: #797979; 3. background-color: #007aff; 继续更新中
- git分支的创建与合并
在git中提倡使用分支,这就涉及到了分支的创建和合并.在git中我们的每次提交类似于一个链表,按照时间顺序向下排列,大约画了一个图,每个小圆圈代表一次提交,在git中有有一个主分支master,我们新 ...
- [JLOI2014] 松鼠的新家
Description 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在"树&q ...
- execCommand的复制
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- vue中的指令
一.声明式渲染有两种: 1.插值用两个花括号如:{{内容}} 例子:html <div id="app1"> <p>{{message}}</p> ...
- 将 Shiro 作为应用的权限基础 四:shiro的配置说明
Apache Shiro的配置主要分为四部分: SecurityManager的配置 URL过滤器的配置 静态用户配置 静态角色配置 其中,由于用户.角色一般由后台进行操作的动态数据,比如通过@Req ...
- ThreadLocal 原理和使用场景分析
ThreadLocal 不知道大家有没有用过,但至少听说过,今天主要记录一下 ThreadLocal 的原理和使用场景. 使用场景 直接定位到 ThreadLocal 的源码,可以看到源码注释中有很清 ...
- 微信小程序测试总结
概述 由于项目中,微信前端和后端对接出现错误.所以Alpha测试分为微信小程序前端,管理员web测试. 测试工具选择 微信小程序的前端使用微信小程序开发工具测试. 管理员web使用web测试. 测试工 ...