springAop概述
SpringAop术语
SpringAop的实现机制
JDK实现动态代理
CGlib实现动态代理
springAop代理的实现(xml)
springAop代理的实现(注解)
切点修饰语法
AOP 是 OOP 的延续,是 Aspect Oriented Programming 的缩写,意思是面向切面编程。可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。AOP设计模式孜孜不倦追求的是调用者和被调用这之间的解耦,AOP 可以说也是这种目标的一种实现。本质上aop就是代理思想实现的
我们现在做的一些非业务,如:日志、事务、安全等都会写在业务代码中(也即是说,这些非业务类横切与业务类),但这些代码往往是重复,复制-粘贴式的代码会给程序带来不便,AOP 就实现了把这些业务与需求分开来做。这种解决的方式也成代理机制。
//目标对象接口
public interface TargetInterface {
public void save();
}
public class TargetInterfaceImpl implements TargetInterface {
@Override
public void save() {
System.out.println("save running >>>>>");
}
}
public class Advice {
public void before(){
System.out.println("前置增强方法>>>>");
}
public void afterRunning(){
System.out.println("后置增强方法>>>>");
}
}
//测试类
public class ProxyText {
public static void main(String[] args) {
//目标对象
final TargetInterface target = new TargetInterfaceImpl();
//获得增强对象
final Advice advice = new Advice();
//返回值就是动态生成的代理对象
TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(
target.getClass().getClassLoader(), //目标对象的类加载器
target.getClass().getInterfaces(),//目标对象相同的接口字节码对象数组
new InvocationHandler() {
//调用代理对象,实质执行的是invoke方法
public Object invoke(
Object proxy,
Method method,
Object[] args
) throws Throwable {
advice.before();//前置增强
Object invoke = method.invoke(target, args);//执行目标方法
advice.afterRunning(); //后置增强
return invoke;
}
}
);
//调用代理对象方法
proxy.save();
}
}
前置增强方法>>>>
save running >>>>>
后置增强方法>>>>
Process finished with exit code 0
public class Target {
public void save() {
System.out.println("save running >>>>>");
}
}
public class Advice {
public void before(){
System.out.println("前置增强方法>>>>");
}
public void afterRunning(){
System.out.println("后置增强方法>>>>");
}
}
public static void main(String[] args) {
//目标对象
final Target target = new Target();
//增强对象
final Advice advice = new Advice();
//返回值就是动态生成的代理对象 基于cglib
//1、创建增强器
Enhancer enhancer = new Enhancer();
//2、设置父类(目标)
enhancer.setSuperclass(Target.class);
//3、设置回调
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(
Object proxy,
Method method,
Object[] args,
MethodProxy methodProxy
) throws Throwable {
//执行前置
advice.before();
Object invoke = method.invoke(target, args);//执行目标
advice.afterRunning();//执行后置
return invoke;
}
});
//4、创建代理对象
Target target1 =(Target) enhancer.create();
target1.save();
}
}
前置增强方法>>>>
save running >>>>>
后置增强方法>>>>
Process finished with exit code 0
public interface TargetInterface {
public void save();
}
public class Target implements TargetInterface {
public void save() {
System.out.println("save running >>>>>");
}
}
public class MyAspect {
public void before(){
System.out.println("前置增强 >>>>>>");
}
public void afterReturning(){
System.out.println("后置增强 >>>>>>");
}
}
<bean id="target" class="com.liu.aop.Target">bean>
<bean id="myAspect" class="com.liu.aop.MyAspect">bean>
<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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<aop:config>
<aop:aspect ref="myAspect">
<aop:before method="before" pointcut="execution(public void
com.liu.aop.Target.save())">aop:before>
<aop:after method="afterReturning" pointcut="execution(public void
com.liu.aop.Target.save())">aop:before>
aop:aspect>
aop:config>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
@Autowired
private TargetInterface target;
@Test
public void test1(){
target.save();
}
}
前置增强 >>>>>>
save running >>>>>
后置增强 >>>>>>
public interface TargetInterface {
void method();
}
public class Target implements TargetInterface {
@Override
public void method() {
System.out.println("Target running....");
}
}
public class MyAspect {
//前置增强方法
public void before(){
System.out.println("前置代码增强.....");
}
//后置增强方法
public void afterReturning(){
System.out.println("后置代码增强.....");
}
}
Component("target")
public class Target implements TargetInterface {
@Override
public void method() {
System.out.println("Target running....");
}
}
@Component("myAspect")
public class MyAspect {
public void before(){
System.out.println("前置代码增强.....");
}
public void afterReturning(){
System.out.println("后置代码增强.....");
}
}
@Component("myAspect")
@Aspect
public class MyAspect {
//定义一个切点表达式
@Pointcut("execution(* com.liu.proxy.anno.*.*(..))")
public void pointcut(){}
@Before("MyAspect.pointcut()")
public void before(){
System.out.println("前置代码增强.....");
}
@After("MyAspect.pointcut()")
public void afterReturning(){
System.out.println("后置代码增强.....");
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
@Autowired
private TargetInterface target;
@Test
public void test1(){
target.method();
}
}
前置代码增强 >>>>>>
Target running >>>>>
后置代码增强 >>>>>>
表达式语法:
execution([修饰符] 返回值类型 包名.类名.方法名(参数))
例如:
execution(public void com.liu.aop.Target.method())
execution(void com.liu.aop.Target.*(..))
execution(* com.liu.aop.*.*(..))
execution(* com.liu.aop..*.*(..))
execution(* *..*.*(..))
* _ooOoo_
* o8888888o
* 88" . "88
* (| -_- |)
* O\ = /O
* ____/`---'\____
* .' \\| |// `.
* / \\||| : |||// \
* / _||||| -:- |||||- \
* | | \\\ - /// | |
* | \_| ''\---/'' | |
* \ .-\__ `-` ___/-. /
* ___`. .' /--.--\ `. . __
* ."" '< `.___\_<|>_/___.' >'"".
* | | : `- \`.;`\ _ /`;.`/ - ` : | |
* \ \ `-. \_ __\ /__ _/ .-` / /
* ======`-.____`-.___\_____/___.-`____.-'======
* `=---='
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
欢迎访问我的csdn博客,我们一起成长!!