Spring Advice
- 通知(Advice)之前 - 该方法执行前运行
- 通知(Advice)返回之后 – 运行后,该方法返回一个结果
- 通知(Advice)抛出之后 – 运行方法抛出异常后,
- 环绕通知 – 环绕方法执行运行,结合以上这三个通知。
接口
public interface UserService {
public void addUser();
public String eat();
}
实现类
public class UserServiceImpl implements UserService {
public void addUser() {
System.out.println("添加用户");
} public String eat() {
String some="apple";
System.out.println("eat a little apple");
return some;
}
}
前置增强
public class BeforeAdvice implements MethodBeforeAdvice {
/**
*
* @param method 目标方法
* @param objects 目标方法的参数列表
* @param o 目标对象
* @throws Throwable
*/
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("前置增强---------------");
}
}
后置增强
public class AfterAdvice implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("后置增强");
}
}
环绕增强 (出现在前置增强之后 ,后置增强之前)
public class AroundAdvice implements MethodInterceptor {
/**
*
* @param methodInvocation
* @return
* @throws Throwable
*/
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("执行方法之前的环绕 通知");
Object result = methodInvocation.proceed();//方法的执行 主业务的执行
if(result!=null){
result="小黑";
}
System.out.println("执行方法之后的环绕 通知");
return result;
}
}
ApplicationContext.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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--目标对象-->
<bean id ="userservice" class="cn.kitty.service.UserServiceImpl"></bean>
<!--配置前置通知-->
<bean id="beforeAdvice" class="cn.kitty.service.BeforeAdvice"></bean>
<!--配置后置通知-->
<bean id="afterAdvice" class="cn.kitty.service.AfterAdvice"></bean>
<!--配置环绕通知-->
<bean id="aroundAdrice" class="cn.kitty.service.AroundAdvice"></bean> <!-- 配置代理工厂bean 生成代理类 把通知织入到目标对象-->
<bean id="userProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--注册目标对象-->
<property name="targetName" value="userservice"></property>
<!--注册通知-->
<property name="interceptorNames" >
<array>
<value>beforeAdvice</value>
<value>afterAdvice</value>
<value>aroundAdrice</value>
</array>
</property>
</bean>
</beans>
test 类
public class Test1011 {
@Test
public void yichang(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserService service= (UserService) context.getBean("userProxy");
service.addUser();
System.out.println("---------------");
service.eat();
}
@Test
public void around(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserService service= (UserService) context.getBean("userProxy");
service.addUser();
System.out.println("---------------");
service.eat();
}
@Test
public void before(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserService service= (UserService) context.getBean("userProxy");
service.addUser();
System.out.println("---------------");
service.eat();
}
@Test
public void after(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserService service= (UserService) context.getBean("userProxy");
service.addUser();
System.out.println("---------------");
service.eat();
} }
Spring Advice的更多相关文章
- 关于面试别问及Spring如何回答思路总结!
首先要知道 Spring两大核心IOC和AOP(Java轻量级业务层框架Spring两大核心IOC和AOP原理) IOC: 1.从Java最基本的创建对象开始 如Interface Driven De ...
- Spring AOP 四大通知
Spring AOP 四大通知 Spring 3.X 以前 1.前置通知,实现 MethodBeforeAdvice 接口,重写 public void before(Method metho ...
- Chapter 4: Spring and AOP:Spring's AOP Framework -- draft
Spring's AOP Framework Let's begin by looking at Spring's own AOP framework - a proxy-based framewor ...
- spring cuowu
spring常见错误总结 在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结,希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with nam ...
- Spring 使用javaconfig配置aop
1.在xml中需要配置自动代理 /** * */ package com.junge.demo.spring.dao; import org.springframework.context.annot ...
- Spring 使用xml配置aop
1.xml文件需要引入aop命名空间 2.xml内容: <?xml version="1.0" encoding="UTF-8"?> <bea ...
- spring入门常见的问题及解决办法
在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结,希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with name 'helloServi ...
- Spring知识总结
一.Spring简述 Spring是一个分层的JavaSE/EEfull-stack(一站式)轻量级开源框架,Spring致力于提供一种方法管理你的业务对象,Spring的主要目的是使JavaE ...
- spring错误汇总
在学习spring过程中遇见了种种不同的异常错误,这里做了一下总结.希望遇见类似错误的同学们共勉一下. 1. 错误一 Error creating bean with name 'helloServi ...
随机推荐
- Redis入门到高可用(三)——通用命令
通用命令 参考 http://redisdoc.com/index.html 1. keys #查看所有key 时间复杂度:O(N), N 为数据库中 key 的数量. 127.0.0.1:637 ...
- (转)fabric 一个链码如何调用另一个链码
使用开发模式测试 可以使用~/hyfa/fabric-samples/chaincode-docker-devmode/启动fabric,具体过程略 用同一个链码注册2个服务 root@2ee7b51 ...
- DLNg改善深层NN:第一周DL的实用层面
1.为什么正则化可以减少过拟合? //答:可以让模型参数变小,减小模型的方差. 在损失函数中加入正则项,在正则化时,如果参数lamda设置得足够大,那么就相当于权重系数W接近于0 ,就会减少很多隐藏单 ...
- bat脚本简单命令
1.if 判断 (1.1)判断字符串是否为空: if "%var1%" == " " ( echo null) else(echo not null ) (1. ...
- [Git/GitHub] Tutorial 1. Git download and commit first project
1. Install at https://git-scm.com/downloads 2. Set up your name and email $ git config --global user ...
- js树形结构-----(BST)二叉树增删查
function BinarySearchTree(){ var cnodes = function(key){ this.key = key; this.left = null; this.righ ...
- TensorFlow读取CSV数据(批量)
直接上代码: # -*- coding:utf-8 -*- import tensorflow as tf def read_data(file_queue): reader = tf.TextLin ...
- 关于RBAC的文章
权限系统与RBAC模型概述 RBAC权限管理模型 摘自慕课网的RBAC
- [xdoj] 1310 DSKer的卡牌游戏
http://acm.xidian.edu.cn/problem.php?id=1310 1. 这道题可以类比括号匹配,YY和yy是两组可以匹配的信号,当然要注意逻辑是否正确,一开始进行括号匹配算法的 ...
- 关于如何利用计算属性进行button的控制
element分页没用它的 (这个只要上一页下一页),比如共2页的时候,你在第一页,你肯定可以点击下一页,当你进入到第二页的时候这个button肯定就不能点击了啊,它的属性diaabled=true让 ...