spring的aop编程(半自动、全自动)
1、spring的半自动代理(从spring中获取代理对象)
(1)spring中的通知类型
spring中aop的通知类型有五种:
前置:在目标方法执行前实施加强
后置:在目标方法执行后实施加强
环绕:在目标方法执行前后实施加强,必须手动执行目标方法,如果只在目标方法前面书写方法,就叫前置通知,前置通知可以阻止目标方法的执行,因为抛出异常后进入catch块,后置通知可以获得方法的返回值。
异常:在方法抛出异常后实施加强
引介:在目标类中添加一些新的方法和属性
(2)导入jar包
核心:4+1
aop:aop联盟(规范)、spring-aop(实现)
(3)创建目标类接口和接口的实现类:
public interface StudentService {
void addStudent();
void updateStudent();
void deleteStudent();
}
public class StudentServiceImpl implements StudentService{
@Override
public void addStudent() {
System.out.println("addStudent");
} @Override
public void updateStudent() {
System.out.println("updateStudent");
} @Override
public void deleteStudent() {
System.out.println("deleteStudent");
}
}
(4)创建切面:
public class MyAspect implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("前");
methodInvocation.proceed();
System.out.println("后");
return null;
}
}
(5)配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
<!--目标类-->
<bean id="studentService" class="pers.zhb.proxy.StudentServiceImpl"></bean>
<!--切面类-->
<bean id="myAspect" class="pers.zhb.proxy.MyAspect"></bean>
<!--代理类
用于创建代理工厂bean,生成特殊代理对象
-->
<bean id="proxystudentService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interfaces" value="pers.zhb.proxy.StudentService"> </property><!--确定接口-->
<property name="target" ref="studentService"></property><!--目标类-->
<property name="interceptorNames" value="myAspect"></property><!--切面类的名称-->
</bean>
</beans>
<property name="optimize" value="true"></property><!--强制使用cglib-->
如果目标类有接口使用jdk动态代理,没有接口的话采用cglib字节码增强。如果声明为true,则无论是否有接口都是采用cglib
(6)测试:
public class TestFactoryBean {
public static void main(String[] args) {
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService= (StudentService) applicationContext.getBean("proxystudentService");
studentService.addStudent();
studentService.deleteStudent();
studentService.updateStudent();
}
}
前
addStudent
后
前
deleteStudent
后
前
updateStudent
后
2、全自动(从spring容器获得目标类,如果配置aop,spring将自动生成代理)
(1)导包:
要确定目标类,aspectj切入点表达式,导入jar包:
(2)创建目标类接口和接口的实现类:
public interface StudentService {
void addStudent();
void updateStudent();
void deleteStudent();
}
public class StudentServiceImpl implements StudentService{
@Override
public void addStudent() {
System.out.println("addStudent");
} @Override
public void updateStudent() {
System.out.println("updateStudent");
} @Override
public void deleteStudent() {
System.out.println("deleteStudent");
}
}
(3)切面:
public class MyAspect implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("前");
methodInvocation.proceed();
System.out.println("后");
return null;
}
}
(4)配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns="http://www.springframework.org/schema/beans"
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="studentService" class="pers.zhb.proxy.StudentServiceImpl"></bean>
<!--切面类-->
<bean id="myAspect" class="pers.zhb.proxy.MyAspect"></bean>
<!--aop编程-->
<aop:config >
<!--从目标对象获得具体方法,使用了切入点表达式-->
<aop:pointcut id="myPointCut" expression="execution(* pers.zhb.proxy.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="myAspect" pointcut-ref="myPointCut"></aop:advisor><!--特殊的通知,只有一个通知和切入点-->
</aop:config>
</beans>
(5)测试类:
public class TestFactoryBean {
public static void main(String[] args) {
ApplicationContext applicationContext=new
ClassPathXmlApplicationContext("applicationContext.xml");
//获得目标类
StudentService studentService= (StudentService) applicationContext.getBean("studentService");
studentService.addStudent();
studentService.deleteStudent();
studentService.updateStudent();
}
}
前
addStudent
后
前
deleteStudent
后
前
updateStudent
后
spring的aop编程(半自动、全自动)的更多相关文章
- Java基础-SSM之Spring的AOP编程
Java基础-SSM之Spring的AOP编程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Spring的本质说白了就是动态代理,接下来我们会体验AOP的用法.它是对OOP的 ...
- Spring之AOP编程
一.AOP简介 AOP的英文全称是Aspect Oriented Programming,意为:面向切面编程. AOP采取横向抽取的机制,取代了传统纵向继承体系的代码复用.AOP常用于 ...
- Spring框架--AOP编程
2 手动实现AOP编程 AOP 面向切面的编程, AOP可以实现"业务代码"与"关注点代码"分离 // 保存一个用户 public void add(User ...
- Spring的AOP编程
1.手动实现AOP编程(代理模式) AOP是面向切面的编程,主要功能就是实现"业务代码"和辅助业务代码的"关注点代码"分离.在一个方法中,出了核心的业务代码,其 ...
- Java进阶知识21 Spring的AOP编程
1.概述 Aop:(Aspect Oriented Programming)面向切面编程 功能: 让关注点代码与业务代码分离! 关注点:重复代码就叫做关注点:切面: 关注点形成的类, ...
- 【AOP】spring 的AOP编程报错:[Xlint:invalidAbsoluteTypeName]error
AOP来发过程中,报错如下: warning no match for this type name: net.shopxx.wx.institution.controller [Xlint:inva ...
- spring相关—AOP编程—切入点、连接点
1 切入点表达式 1.1 作用 通过表达式的方式定位一个或多个具体的连接点. 1.2 语法细节 ①切入点表达式的语法格式 execution([权限修饰符] [返回值类型] [简单类名/全类名] [方 ...
- Spring AOP编程(二)-AOP实现的三种方式
AOP的实现有三种方式: l aop底层将采用代理机制进行实现. l 接口 + 实现类 :spring采用 jdk 的动态代理Proxy. l 实现类: ...
- Spring基础篇——Spring的AOP切面编程
一 基本理解 AOP,面向切面编程,作为Spring的核心思想之一,度娘上有太多的教程啊.解释啊,但博主还是要自己按照自己的思路和理解再来阐释一下.原因很简单,别人的思想终究是别人的,自己的理解才是 ...
随机推荐
- tp6 不能使用vendor
从thinkphp 5.1.x后vendor的使用方法发生变化,文档又没有详细说明.官方真的太坑了! 在thinkPHP 5.1.X后新版取消了Loader::import方法以及import和ven ...
- influxDB初步学习
influxdb的安装等操作在我的文章. 首先得装influxdb,其次操作如下. application.properties spring.datasource.test1.jdbc-url=jd ...
- LeetCode 95 | 构造出所有二叉搜索树
今天是LeetCode专题第61篇文章,我们一起来看的是LeetCode95题,Unique Binary Search Trees II(不同的二叉搜索树II). 这道题的官方难度是Medium,点 ...
- javaScript 推荐书籍 由简单到入门,初级到高级。
- Codeforces1312E Array Shrinking 区间DP
题意 给你一个数组\(a\),只要满足\(a_i=a_{i+1}\)就可以将这两个元素合并成一个值为\(a_i+1\)的元素,问数组最小长度. 解题思路 记得之前某场的F和这题差不多,当时好像是相邻且 ...
- 转载:SQL语句执行顺序
转载地址:https://database.51cto.com/art/202001/609727.htm
- Cassandra使用的各种策略
1. 背景介绍 Cassandra 使用分布式哈希表(DHT)来确定存储某一个数据对象的节点.在 DHT 里面,负责存储的节点以及数据对象都被分配一个 token.token 只能在一定的范围内取值, ...
- Python爬Boss,找工作,快人一步!!!
通过职位搜索"Python开发",看下搜索的结果: https://www.zhipin.com/job_detail/?query=python开发&city=10102 ...
- Linux系统安装01-centos7系统安装
2020注定是一个不平凡的年份,对于各行各业都是不小的波动.话说回来,从当前的互联网趋势来看,linux的使用以后会越来越广泛,既然之前不懂linux,那么我们就从头开始,先学习Linux的系统安装. ...
- synchronized底层是怎么实现的?
前言 面试的时候有被问到,synchronized底层是怎么实现的,回答的比较浅,面试官也不是太满意,所以觉得要好好总结一下,啃啃这个硬骨头. synchronized使用场景 我们在使用synchr ...