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 ...
随机推荐
- asxios--form data提交,setcookie
React native 项目,部分接口用form data 提交,以及在Android端,虽然设置了请求携带cookie,但每次请求携带的cookie跟初始化时都不一样,目前做法是去到初始化中返回的 ...
- 【4】axios 获取数据
API:https://www.kancloud.cn/yunye/axios/234845 基于axios进行二次封装 安装axios npm install axios --save 安装成功 [ ...
- golang fmt格式“占位符”
# 定义示例类型和变量 type Human struct { Name string } var people = Human{Name:"zhangsan"} 普通占位符 占位 ...
- 根据构建类型动态设置AndroidManifest.xml文件中的meta-data
当debug和release版本使用不同的值时,使用Gradle设置相应的值. Android主配置文件 <meta-data android:name="com.amap.api.v ...
- Gradle全局变量定义及引用
在Project的build.gradle脚本中定义一些全局变量 ext { compileSdkVersion = 21 buildToolsVersion = "24.0.1" ...
- Python itsdangerous 生成token和验证token
代码如下 class AuthToken(object): # 用于处理token信息流程: # 1.更加给定的用户信息生成token # 2.保存生成的token,以便于后面验证 # 3.对用户请求 ...
- vue的图片路径,和背景图片路径打包后错误解决
最近在研究vue,老实的按照官网提供的,搭建的了 webpack+vue+vuex+vue-router,,因为是自己搭建的,所以踩了不少坑,一般问题百度都有,这个背景图片的问题,查了很久才解决. 1 ...
- Kotlin 范型约束
官方的示意及其简约,该说的一概没说 我在这里给大家一个完整的例子 //test.kt fun <T> cloneWhenGreater(list: List<T>, thres ...
- come on! linux install JDK9.0.1
哈哈,JDK9已经出来了 我们把它安装到Linux上吧! 一下载JDK9 http://www.oracle.com/technetwork/java/javase/downloads/index.h ...
- java随机排座位
//打乱学生顺序 Collections.shuffle(); 容我记个单词 peer: vi.凝视; 盯着看; 隐退,若隐若现; 同等,比得上;n.同辈,同等的人; 贵族; 同伴,伙伴;adj.贵族 ...