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 ...
随机推荐
- axios post、get 请求参数和headers配置
axios.post("http://xxx.com/xxx/xxx/xxx?", { 'queslistid':this.kemuid }, { headers: {'token ...
- HBuilder 自动整理代码格式快捷键设置
工具 ->选项
- drf权限组件
1.简介 设置哪种用户的权限可以做什么事 2.用法 在MyAuth文件编写权限类, from rest_framework.permissions import BasePermission 代码如下 ...
- 万恶之源 - Python模块二
shelve 我们之前学了json和pickle模块 这些都是序列化的模块,咱们进行在讲一个序列化的东西 叫做shelve 你们肯定有个疑问,这个东西和那个类似为什么要讲.是因为这个模块比较简单的,并 ...
- 网络基础之2——TCP/IP参考模型
本内容主要来源于<看透Spring MVC源码分析与实践——韩路彪>一书 BS结构网络传输的分解方式有两种: 1.OSI参考模型. 2.TCP/IP参考模型. OSI和TCP/IP分层模型 ...
- python 美化打印json数据
#!/usr/bin/python3 # -*- coding: utf-8 -*- import json data = {'name':'张森','email':'zhangsen@qq.com' ...
- 15个Node.js项目列表
前言: Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台,是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎,V8引擎执行Javascri ...
- Linux 中的 tar
tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...
- Linux 查找安装包所在目录的常用方法
1. which命令查找出相关命令是否已经在搜索路径中: $which gcc //显示出GNC的C编译器安装在哪个目录 返回: /usr/bin/gcc 注意:如果which没有找到要找的命令,可以 ...
- .NET拾忆:FileSystemWatcher 文件监控
资源: https://msdn.microsoft.com/zh-cn/library/system.io.filesystemwatcher_properties(v=vs.110).aspx F ...