SpringAOP进阶
利用代理工厂实现增强
com.Spring.proxyfactory中的IdoSomeService
package cn.spring.proxyfactory;
public interface IdoSomeService {
public void doSome();
}
IdoSomeServiceImpl类
package cn.spring.proxyfactory; /**
* 原始对象
*/
public class IdoSomeServiceImpl implements IdoSomeService{
public void doSome(){
System.out.println("=========真实业务===========");
}
}
MyBeforeAdavice类
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class MyBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("==============前置增强=================");
}
}
applicationContext.xml
<!--注入业务Bean-->
<bean id="idoSomeService" class="com.Spring.proxyfactory.IdoSomeServiceImpl"></bean>
<!--增强:切面-->
<bean id="myBeforeAdvice" class="com.Spring.proxyfactory.MyBeforeAdvice"></bean>
<!--使用代理工厂实现增强-->
<bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--将增强和业务织入到一起-->
<property name="target" ref="idoSomeService"></property>
<!--拦截增强类-->
<property name="interceptorNames" value="myBeforeAdvice"></property>
<!--更换代理方式 proxyTargetClass默认值为false 默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib-->
<property name="proxyTargetClass" value="true"></property>
</bean>
ProxyFactoryTest测试类
package com.Spring; import com.Spring.proxyfactory.IdoSomeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Around {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取代理工厂Bean
IdoSomeService idoSomeService = (IdoSomeService)ctx.getBean("proxyFactory");
idoSomeService.doSome();
}
}
环绕增强
com.Spring.around中的IdoSomeService接口
package cn.spring.around;
public interface IdoSomeService {
public void doSome();
}
IdoSomeServiceImpl类
package cn.spring.around;
/**
* 原始对象
*/
public class IdoSomeServiceImpl implements IdoSomeService {
public void doSome(){
System.out.println("=========真实业务===========");
}
}
MyAroundAdvice类
package cn.spring.around; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.aspectj.lang.ProceedingJoinPoint; /**
* 环绕增强
*/
public class MyAroundAdvice implements MethodInterceptor{ @Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("============环绕前============="); //调用核心业务方法 也可以获取方法内的参数 也可以获取目标对象
Object proceed = invocation.proceed();
Object aThis = invocation.getThis();
System.out.println(aThis); System.out.println("============环绕后=============");
return proceed;
}
}
AroundTest测试类
package cn.spring; import cn.spring.throwadvice.IdoSomeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AroundTest {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取代理工厂Bean
IdoSomeService idoSomeServiceProxy = (IdoSomeService) ctx.getBean("idoSomeService"); try {
idoSomeServiceProxy.doSome();
} catch (Exception e) {
e.printStackTrace();
} System.out.println("");
}
}
applicationContext.xml文件
<bean id="idoSomeServie" class="com.Spring.around.IdoSomeServiceImpl"></bean>
<bean id="myAroundAdvice" class="com.Spring.around.MyAroundAdvice"></bean>
<bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--将增强和业务织入到一起-->
<property name="target" ref="idoSomeServie"></property>
<!--拦截增强类-->
<property name="interceptorNames" value="myAroundAdvice"></property>
<!--更换代理方式 proxyTargetClass默认值为false 默认是jdk动态代理,但是当目标对象没有接口时,自动改为cglib-->
<property name="proxyTargetClass" value="true"></property>
</bean> <aop:config>
<aop:pointcut id="pointcut" expression="execution(* *..around.*.*(..))"/>
<aop:aspect ref="myAroundAdvice">
<aop:around method="around" pointcut-ref="pointcut"></aop:around>
</aop:aspect>
</aop:config>
最终异常
com.Spring.throwadvice中的IdoSomeService接口
package cn.spring.throwadvice;
public interface IdoSomeService {
public void doSome() throws Exception;
}
IdoSomeServiceImpl类
package cn.spring.throwadvice;
/**
* 原始对象
*/
public class IdoSomeServiceImpl implements IdoSomeService {
public void doSome() throws Exception{
int result=/;
System.out.println("=========真实业务===========");
}
}
MyAdvice类
package cn.spring.throwadvice; import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.ThrowsAdvice; import java.lang.reflect.Method; public class MyAdvice {
public void afterThrowing(Exception ex){
System.out.println("=====发生了异常,执行增强操作===============");
} public void afterAdvice(){
System.out.println("======执行最终异常===============");
}
}
applicationContext.xml文件
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* *..throwadvice.*.*(..))"/>
<aop:aspect ref="myAdvice">
<aop:after-throwing method="afterThrowing" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing>
<aop:after method="afterAdvice" pointcut-ref="pointcut"></aop:after>
</aop:aspect>
</aop:config>

SpringAOP进阶的更多相关文章
- Spring实战3:装配bean的进阶知识
主要内容: Environments and profiles Conditional bean declaration 处理自动装配的歧义 bean的作用域 The Spring Expressio ...
- Spring框架进阶3
Spring框架进阶3 测试spring_jdbc和spring对事务的管理 先配置相应的pom <?xml version="1.0" encoding="UTF ...
- nodejs进阶(6)—连接MySQL数据库
1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...
- nodejs进阶(4)—读取图片到页面
我们先实现从指定路径读取图片然后输出到页面的功能. 先准备一张图片imgs/dog.jpg. file.js里面继续添加readImg方法,在这里注意读写的时候都需要声明'binary'.(file. ...
- JavaScript进阶之路(一)初学者的开始
一:写在前面的问题和话 一个javascript初学者的进阶之路! 背景:3年后端(ASP.NET)工作经验,javascript水平一般般,前端水平一般般.学习资料:犀牛书. 如有误导,或者错误的地 ...
- nodejs进阶(3)—路由处理
1. url.parse(url)解析 该方法将一个URL字符串转换成对象并返回. url.parse(urlStr, [parseQueryString], [slashesDenoteHost]) ...
- nodejs进阶(5)—接收请求参数
1. get请求参数接收 我们简单举一个需要接收参数的例子 如果有个查找功能,查找关键词需要从url里接收,http://localhost:8000/search?keyword=地球.通过前面的进 ...
- nodejs进阶(1)—输出hello world
下面将带领大家一步步学习nodejs,知道怎么使用nodejs搭建服务器,响应get/post请求,连接数据库等. 搭建服务器页面输出hello world var http = require ...
- [C#] 进阶 - LINQ 标准查询操作概述
LINQ 标准查询操作概述 序 “标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法.大多数这些方法都在序列上运行,其中的序列是一个对象,其类型实现了IEnumerable<T> ...
随机推荐
- lnmp二级域名配置相关
阿里云那域名解析那有误读 我在偏远的电信网选择中国联通解析死活解析不出来 以上这么配置就对了....选择默认.瞬间解析出来.... 出于对nginx 配置不够熟悉 后来一点点理出来. 不带www 也正 ...
- (4)Spring Boot Web开发---静态资源
文章目录 对静态资源的映射规则 模板引擎 Thymeleaf 使用 & 语法 使用之前将的快速创建项目的方法,勾选我们需要的场景,这里我需要 web --> web.sql --> ...
- Keras中图像维度介绍
报错问题: ValueError: Negative dimension size caused by subtracting 5 from 1 for 'conv2d_1/convolution' ...
- C++根据用户输入打印对应的金层塔层数
#include <iostream> #include <Windows.h> using namespace std; int main(void) { int row; ...
- vagrant root 登录虚拟机
这个问题本来觉得是个特别简单的问题,昨天弄的时候折腾了半晚上.所以打算记录下过程,主要也被网上的各种信息误导了. 1 先看下我这vagrant配置信息 Vagrant.configure(" ...
- spring的事务解决方案之@Transactional注解
首先此注解位于 org.springframework.transaction.annotation 这个包路径下面, 事务有两种类别,一种是编程式事务,另一种是声明式事务,显然此注解是声明式事务,这 ...
- Angular 学习笔记 (cdk focus monitor 和一些 focus tabindex 的基础)
更新 : 2019-12-22 focusInitialElementWhenReady 我们经常会调用到这个方法, 它的逻辑是这样 先看有没有 cdkFocusInitial 有的就 focus ...
- DropDownList下拉控件
<asp:DropDownList ID="DropDownList1" runat="server" Width="177px" ...
- Windows Mobile设备中心不能正常运行
1.开始-->运行,输入services.msc回车 2.在打开的服务界面中,找到“基于Windows Mobile 2003的连接设备” 3.打开的属性 ,找到登录项,登录身份选择“本地系统账 ...
- entity-framework-core – 实体框架核心RC2表名称复数
参考地址:https://docs.microsoft.com/zh-cn/ef/core/modeling/relational/tables http://www.voidcn.com/artic ...