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 ...
随机推荐
- PHP策略模式1
[IUser.php] <?php /** * 策略模式 * 将一组特定的行为和算法封装成类,用来适应某些特定的上下文环境,实现从硬编码到解耦 * 应用举例:电商系统针对不同性别跳转到不同的商品 ...
- [LeetCode] 339. Nested List Weight Sum_Easy tag:DFS
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- [Java in NetBeans] Lesson 08. If: conditional statement
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: 1. If-else statement if (x > 5) { System.out.println("Inpu ...
- Elasticsearch集群监控工具bigdesk插件安装
bigdesk是elasticsearch的一个集群监控工具,可以通过它来查看es集群的各种状态,如:cpu.内存使用情况,索引数据.搜索情况,http连接数等. 项目git地址: https://g ...
- Hybrid设计--如何落地一个Hybrid项目
前后分离 -> 统一前端框架 -> 同一个账号体系 -> 登录注册的公共页 -> 有了这些公共业务后 推行 -> Hybrid 技术 底层容器开发出来后 -&g ...
- webpack使用六
插件(Plugins) 插件(Plugins)是用来拓展Webpack功能的,它们会在整个构建过程中生效,执行相关的任务. Loaders和Plugins常常被弄混,但是他们其实是完全不同的东西,可以 ...
- python--教你做个最简单的tcp通信。。
TCP协议:建立在IP协议之上的,TCP协议,传输控制协议(英语:Transmission Control Protocol,缩写为 TCP)是一种面向连接的.可靠的.基于字节流的传输层通信协议,由I ...
- springboot整合webSocket的使用
引入jar包 <dependency><!-- 5.引入websocket--> <groupId>org.springframework.boot</gro ...
- 学习笔记<1>技术体系结构
Android的系统架构采用了分层架构的思想,如上图所示.从上层到底层共包括四层,分别是 1.应用程序程序层 2.应用框架层 3.系统库和Android运行时 4.Linux内核. 每 ...
- sql server得到某个数据库的所有表和所有字段
select b.name tablename,a.name as columnnamefrom sys.columns a,sys.objects b,sys.types cwhere a.obje ...