Spring AOP(1)- 前置增强 实现
来源:互联网 发布:java eclipse 编辑:程序博客网 时间:2024/06/07 12:28
实现原理:
前置增强完成功能:在目标类的方法执行之前嵌入增强逻辑.
实现原理:
1. 实现接口 MethodBeforeAdvice,重写函数 before ,在函数before中实现需要插入目标方法之前的逻辑代码
2. 利用ProxyFactoryBean代理类调用目标类方法 或者使用AOP动态代理
3.配置Spring XML
定义接口类
package com.advice;import com.smart.domain.User;import java.sql.SQLException;/** * @author Duoduo * @version 1.0 * @date 2017/4/22 18:58 */public interface IUserDao { int addUser(User user); int updateUser(User user); void deleteUser(User user); void removeUser(User user) throws SQLException;}
接口实现类
package com.advice;import com.smart.domain.User;import java.sql.SQLException;/** * @author Duoduo * @version 1.0 * @date 2017/4/22 19:00 */public class UserDaoImpl implements IUserDao { @Override public int addUser(User user) { System.out.println("Add user"); return 0; } @Override public int updateUser(User user) { System.out.println("Update user"); return 0; } @Override public void deleteUser(User user) { //throw new RuntimeException("exception test"); } @Override public void removeUser(User user) throws SQLException { //throw new SQLException("SQLException Test"); }}
定义前置增强类
package com.advice;import com.smart.domain.User;import org.springframework.aop.MethodBeforeAdvice;import java.lang.reflect.Method;/** * 前置增强:在目标类的方法执行之前嵌入增强逻辑 * @author Duoduo * @version 1.0 * @date 2017/4/22 19:01 */public class AddUserBeforAdvice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] args, Object target) throws Throwable { User user = (User)args[0]; //编写前置增强逻辑 ... System.out.println("Before Add user for user name :"+user.getUserName()); }}
Spring XML 配置 有两种方式
1.配置代理工厂
特别注意:代理工厂是 ProxyFactoryBean 不是 ProxyFactory
<!-- 代理设置 --><bean id="userDaoImpl" class="com.advice.UserDaoImpl"/><bean id="addUserBeforAdvice" class="com.advice.AddUserBeforAdvice"/><bean id="addUserAfterAdvice" class="com.advice.AddUserAfterAdvice"/><bean id="addUserProxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean" p:proxyInterfaces="com.advice.IUserDao" p:interceptorNames="addUserBeforAdvice" p:target-ref="userDaoImpl"/>
2.配置动态代理,使用AOP配置
注意:expression是一个表达式。其中: .. 表示所有参数 *表示所有方法例如:<aop:pointcut id="pointcut" expression="execution(* com.advice.UserDaoImpl.*(..))"/> 表示拦截UserDaoImpl所有的方法<aop:pointcut id="pointcut" expression="execution(* com.advice.*.*(..))"/>表示拦截advice下的所有的类的所有方法
<bean id="userDaoImpl" class="com.advice.UserDaoImpl"/> <bean id="addUserBeforAdvice" class="com.advice.AddUserBeforAdvice"/> <aop:config expose-proxy="true"> <!--<aop:pointcut id="pointcut" expression="execution(* com.advice.UserDaoImpl.*(..))"/>--> <aop:pointcut id="pointcut" expression="execution(* com.advice.UserDaoImpl.addUser(..))"/> <aop:advisor advice-ref="addUserBeforAdvice" pointcut-ref="pointcut"/> </aop:config>
Junit测试 – 使用代理工厂模式
两种调用方式:
1.根据XML配置来进行调用测试
2.自己创建代理来进行测试
另种方法都需要注意:此处代理获取的是代理Bean,指向的是Interface的代理。
@Testpublic void addUserAdivceTest(){ /*IUserDao userDao = new UserDaoImpl(); BeforeAdvice beforeAdvice = new AddUserBeforAdvice(); //创建代理工厂 ProxyFactory proxyFactory = new ProxyFactory(); //设置代理目标类 proxyFactory.setTarget(userDao); //为代理添加增强 proxyFactory.addAdvice(beforeAdvice); //生成代理实例 //代理为指向Interface的代理 IUserDao proxyUserDao = (IUserDao)proxyFactory.getProxy(); proxyUserDao.addUser(user);*/ System.out.println("************ addUserAdivceTest ************"); ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-another-context.xml"); //代理为指向Interface的代理 IUserDao userDaoProxyFactory = (IUserDao) context.getBean("addUserProxyFactory"); userDaoProxyFactory.addUser(user);}
测试结果
** addUserAdivceTest **
四月 23, 2017 1:23:57 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@add0edd: startup date [Sun Apr 23 13:23:57 CST 2017]; root of context hierarchy
四月 23, 2017 1:23:57 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-another-context.xml]
Before Add user for user name :Test User Name
Add user
Junit测试 – 使用AOP动态代理模式
@Test public void testBefore() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext( "classpath:spring-advice.xml"); IUserDao userDao = (IUserDao) context.getBean("userDaoImpl"); userDao.addUser(new User("username", "password", true, "ADMIN")); userDao.updateUser(new User("username", "password", true, "ADMIN")); }
测试结果
AddUserBeforAdvice class com.advice.UserDaoImpl, method.name : addUser , args : [com.smart.domain.User@71809907]Add user
- Spring AOP(1)- 前置增强 实现
- Spring-AOP 通过配置文件实现 前置增强
- spring AOP详解之--前置增强 (MethodBeforeAdvice)
- spring AOP 前置增强,后置增强小Demo
- Spring AOP(2)- 后置增强 实现
- Spring-------- 增强(前置,后置,异常,环绕)
- spring(四)前置增强
- Spring的aspectj的AOP操作(前置增强 后置增强 环绕)
- Spring AOP-前置通知
- Spring--AOP-前置通知
- 前置增强的实现
- Spring Aop 实现函数内部增强
- Spring-AOP 通过配置文件实现 后置增强
- Spring-AOP 通过配置文件实现 环绕增强
- Spring AOP 面向切面编程 常见通知实现(前置,后置,环绕,异常)
- 黑马程序员--Spring Aop 面向切面编程,实现前置通知
- Spring 3.x企业应用开发实战(9)----AOP前置增强
- Spring关于增强(前置,后置,异常,环绕。。。)
- 题目1367:二叉搜索树的后序遍历序列
- linux上的mysql从5.5升级到5.6
- 栈的压入、弹出序列
- 【Summary】——>Web组·Week3
- CSDN-markdown编辑器使用教程存档
- Spring AOP(1)- 前置增强 实现
- css样式的优先级
- 顺序栈
- ZOJ 3959 Problem Preparation
- 选择器
- 【笔记】formvalidtor常见问题总结
- 详解c++指针的指针和指针的引用
- R语言-文本挖掘 主题模型 文本分类
- ZOJ 3958 Cooking Competition