09SpringAopAdvice
Spring原生的经典模式 实现 AOP
通知:
前置通知:在目标方法执行之前执行,不能改变方法的执行流程和结果!
实现 MethodBeforeAdvice接口!
后置通知:在目标方法执行之后执行,不能改变方法的执行流程和结果!
实现 AfterReturningAdvice接口!
环绕通知:方法的拦截器!可以改变方法的执行流程和结果!
实现 MethodInterceptor接口!
异常通知:当我们的目标方法出现异常时才执行的方法!
实现ThrowsAdvice接口! 实现各种增强(通知 advice)的步骤: 01.引入两个jar 一个是aop联盟 spring-aop
02.创建对应的接口和实现类(主业务)
03.创建对应的增强处理类(系统级业务)
04.在spring容器中配置 目标类对象和通知对象
05.配置ProxyFactoryBean(代理工厂类) 问题:
01.如果我们有多个serviceImpl! 这时候ProxyFactoryBean不能配置多个目标对象!
02.ProxyFactoryBean给目标对象中的所有主业务都做了增强!不能指定某个主业务!

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>Spring</artifactId>
<groupId>com.xdf</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>09SpringAop</artifactId>
<packaging>jar</packaging> <name>09SpringAop</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--spring对应的版本号-->
<spring.version>4.2.1.RELEASE</spring.version>
</properties> <dependencies>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency> <!--引入需要的spring 核心jar-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency> <!--引入mysql的驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency> <!--配置jdbc需要的数据源-->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency> <dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency> <dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency> <!--SpringJDBC需要的jar-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency> </dependencies>
</project>
public interface UserService {
//主业务
String eat();
//主业务
void sleep();
}
// 目标类 impl包下的UserServiceImpl
public class UserServiceImpl implements UserService{ //主业务
public String eat() {
System.out.println("正在吃小苹果......");
return "apple";
} //主业务
public void sleep() {
System.out.println("正在休息......");
}
}
定义的通知(增强)
//异常通知
public class ExceptionAdvice implements ThrowsAdvice { /**
* @param ex 用户名异常
*/
public void afterThrowing(UserNameException ex){
System.out.println(ex.getMessage());
}
/**
* @param ex 年龄异常
*/
public void afterThrowing(AgeException ex){
System.out.println(ex.getMessage());
}
}
//前置通知
public class BeforeAdvice implements MethodBeforeAdvice {
/**
* 在目标方法执行之前
* @param method 目标方法
* @param args 目标方法的参数列表
* @param target 目标对象
* @throws Throwable
*/
public void before(Method method, Object[] args, Object target) throws Throwable {
/* System.out.println("target的名称====》"+target);
System.out.println("method的名称====》"+method.getName());*/
System.out.println("执行**** 前置**** 通知");
}
}
//环绕通知
public class AroundAdvice implements MethodInterceptor { /**
* 在前置通知 之后,后置通知之前执行环绕通知!
* 可以获取方法的返回值,并且改变!
* @param methodInvocation 方法的执行器, getMethod 包含了方法中的所有方法
* @return
*/
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("执行方法之前的 环绕通知");
//执行目标方法
Object result= methodInvocation.proceed();
if (result!=null){
result="xiaoheihei";
}
System.out.println("执行方法之后的 环绕通知");
return result;
}
}
//后置通知
public class AfterAdvice implements AfterReturningAdvice {
/**
* 在目标方法执行之后
* @param returnValue 目标方法的返回值 我们可以获取返回值 但是不能操作返回值
* @param method 目标方法
* @param args 目标方法的参数列表
* @param target 目标对象
* @throws Throwable
*/
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
/* System.out.println("target的名称====》"+target);
System.out.println("returnValue====》"+returnValue);
System.out.println("method的名称====》"+method.getName());*/
System.out.println("执行**** 后置**** 通知");
}
}
定义的异常文件夹
//验证异常通知的接口
public interface ExceptionService { public boolean checkUser(String userName,int age) throws UserException;
}
public class UserServiceImpl implements ExceptionService {
public boolean checkUser(String userName, int age) throws UserException {
if (!"admin".equalsIgnoreCase(userName)){
throw new UserNameException("用户名错误");
}
if (age<20){
throw new AgeException("年龄错误");
}
return true;
}
}
//用户的异常类
public class UserException extends Exception{ public UserException(String msg){
super(msg);
} }
//用户名异常类
public class UserNameException extends UserException { public UserNameException(String msg) {
super(msg);
}
}
//年龄异常类
public class AgeException extends UserException { public AgeException(String msg) {
super(msg);
}
}
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="userService" class="cn.happy.service.impl.UserServiceImpl"/> <bean id="before" class="cn.happy.service.advices.BeforeAdvice"/> <bean id="after" class="cn.happy.service.advices.AfterAdvice"/> <bean id="around" class="cn.happy.service.advices.AroundAdvice"/> <bean id="userException" class="cn.happy.service.exceptionPackage.UserServiceImpl"/> <bean id="myException" class="cn.happy.service.advices.ExceptionAdvice"/> <!--03.通知配置代理工厂bean,生成代理类,来把通知织入到目标对象
问题:只能管理 通知!
01.只能将切面织入到目标类的所有方法中
02.只能配置一个目标对象
-->
<bean id="userProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="userService"/>
<!--前置,后置的通知: value第一种方法-->
<!--<property name="interceptorNames" value="before,after"/>-->
<!--前置,后置,环绕的通知 value第二种方法-->
<property name="interceptorNames">
<array><!--这里要定义array-->
<value>before</value>
<value>after</value>
<value>around</value>
</array>
</property>
</bean> <bean id="exceptionProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="targetName" value="userException"/>
<property name="interceptorNames">
<array>
<value>myException</value>
</array>
</property>
<!--代理类的优化 是否使用cglib动态代理-->
<property name="optimize" value="true"/>
</bean>
</beans>
<property name="proxyTargetClass" value="true"/>
proxyTargetClass:默认是false,默认执行jdk动态代理!
设置成true,强制执行cglib!
optimize:代理类的优化
有接口就是用jdk,没有接口使用cglib动态代理
我们的动态代理 (在程序运行期间,动态生成的代理类) 分为两种方式:
01.jdk 只能应用于实现接口的情况
02.cglib 应用于实现接口和类的情况
如果我们是接口的情况,使用jdk效率高!
如果我们是类的情况,必须使用cglib!
问题?
程序 spring容器怎么知道我们是用的类还是接口??
public class ProxyConfig implements Serializable
private boolean proxyTargetClass = false;
private boolean optimize = false;
spring底层默认使用cglib! 现在我们的项目中使用的是接口!
用spring默认的性能不高!
proxyTargetClass 和optimize都是用来设置 我们使用的代理模式是jdk还是cglib!
@Override
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
// 根据我们配置文件中 proxyTargetClass 和 optimize的配置
if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
Class<?> targetClass = config.getTargetClass();
if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class: " +
"Either an interface or a target is required for proxy creation.");
}
//根据目标对象返回对应的动态代理
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
return new ObjenesisCglibAopProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}
09SpringAopAdvice的更多相关文章
随机推荐
- POJ2155 Matrix(二维树状数组||区间修改单点查询)
Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row an ...
- bzoj 1049: 数字序列 dp
题目大意: 给定一个长度为n的整数序列.在改变的数最小的和改变的幅度最小的前提下把它变成一个单调严格上升的序列.求改变的最小的数和这个幅度. 题解: (貌似以前考试考过这道题) 其实这道题就是两道题拼 ...
- 监测GPU使用情况命令
每2秒监测一次:watch -n 2 nvidia-smi
- 基于STM32的uCGUI移植和优化
基于STM32的uCGUI移植和优化 首先在开始这个说明之前,要简要说明下具体的环境: 编译工具:MDK4.20 开发板:安富莱v2版开发板 调试器:JLink v8盗版 移植篇 相信大家有移植经验 ...
- 【转】Pro Android学习笔记(六):了解Content Provider(中)
Content Provider的架构 Authority类似web中的域名,每个content provider会通过AndroidManifest.xml向系统注册authority,如下.其中n ...
- 用Pyinstaller把Python3.7程序打包成可执行文件exe
1.通过pip3 install pyinstaller 安装成功 2.然后执行命令,首先:需要切换到程序所在的目录 执行命令 pyinstaller -F -w <文件名.py>,-F代 ...
- 转:Serializable---序列化
Serializable 今天在看代码的时候,看到[Serializable],不明白是什么意思.查阅了网上的一些资料,才明白这是指给类添加序列化的特性,即添加后它就可以进行序列化,那什 ...
- Crypto Challenge Set 1解题报告
1.Convert hex to base64 题意:给出一个hex编码过的字符串,将它进行base64加密 解题关键:直接利用base64库函数实现 import base64 str1=" ...
- petrozavodsk1
A 转化模型和相当于求解小于n/2的最大的和n互质的数字, 显然可以证明所求和n/2相距 O(logn) ,从 n/2 开始向下枚举然后判定即可. B 上下界网络流? C 从底层开始向上走贪心选下层节 ...
- 程序员必备,C#各类项目、开源项目插件资料收藏
一.AOP框架 Encase 是C#编写开发的为.NET平台提供的AOP框架.Encase独特的提供了把方面(aspects)部署到运行时代码,而其它AOP框架依赖配置文件的方式.这种部署方面 ...