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 ...
随机推荐
- Stylus的使用
vue-cli项目安装使用stylus步骤:1. npm install stylus -D命令,在项目内安装stylus.(注意:命令结尾 -D 即是 --save-dev 的简写形式) 2.需要安 ...
- Mongodb 基础 复制集原理和搭建
数据复制原理 开启复制集后,主节点会在local库下生成一个集合叫 oplog.rs,这是一个有限的集合,即大小固定.这个集合记入了整个mongod实例一段时间内数据库的所有变更操作(如:增/删/改) ...
- 【LeetCode每天一题】4Sum(4数之和)
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- Vbox下的linux和windows共享文件设置
参考链接: https://jingyan.baidu.com/article/2fb0ba40541a5900f2ec5f07.html
- python os.path模块常用方法详解
os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.ht ...
- ROSETTA使用技巧随笔--PyMOL实时观测ROSETTA模拟过程中的结构变化
没有梦想的人,就是一只咸鱼,像我,就有一个梦想,就是让蛋白模拟过程变成动画,动起来! 虽然MD中有很多方法可以方模拟过程像动画一样播放出来,但是我一直想在ROSETTA中也找一个这样的功能,这不,我发 ...
- selenium元素单击不稳定解决方法
selenium自动化测试过程中,经常会发现某一元素单击,很不稳定,有时候执行了点击没有反映. 以下总结两种解决方法:都是通过js注入的方式去点击. 1.F12查一看,要点击的按钮,或连接,有没有on ...
- bat cmd 删除文件或文件夹 删除注册表
@echo off rd "C:\Documents and Settings\Administrator\桌面\新建文件夹" del 是用来删除文件的,删除文件夹的命令是rd 注 ...
- Spring.之.jar包官网下载
jar包官网下载 1. 官网下载网址:http://repo.spring.io/libs-release 2. 用到的jar包所在路径:org/springframework/spring/ ,在此 ...
- Vue+webpack项目中实现跨域的http请求
目前Vue项目中对json数据的请求一般使用两个插件vue-resource和axios, 但vue-resource已经不再维护, 而axios是官方推荐的且npm下载量已经170多万,github ...