SpringAOP思想以及简单实现

Spring AOP思想

AOP  面向切面编程 

那么如何理解面向切面编程这个概念

就像下面的正方体一样,横着切一刀

那么,如何理解编程中的这个概念

正方体,就像一个很多类的集合体

切的一刀,切出来的切面,就是这些类都需要进行的操作

JavaWeb中的过滤器,就是面向切面编程

设置好过滤器后,在访问过滤器中需要过滤的Servlet时

会执行某些操作,例如判断用户是否登录,不登录就无法继续访问

JavaWeb中Service层需要进行事务操作时

每一个Service实现类都需要实现事务操作时

要为Service中的每一个实现类中的每个方法添加事务操作

代码重复量太大了,使用AOP思想,可以很大程度上实现代码的重用

即为需要添加事务操作的方法添加事务操作,这个过程称为 增强。

Spring实现AOP的原理

(1)动态代理(优先)

被代理的对象必须要实现接口,才能实现产生代理对象,如果没有接口将不能实现动态代理实现。

(2)cglib代理(没有接口)

第三方代理技术,cglib代理可以对任何类生产代理,代理的原理是对目标对象进行继承

如果目标对象被final修饰将无法被cglib代理。

AOP相关基本概念

joinPoint 连接点 :目标对象中,所有可以增强的方法

pointCut  切入点:目标对象中已经被加强的方法

advice     通    知:增强的代码

target  目标对象 :被代理的对象

weaving   织    入:将通知织入到目标对象的过程

proxy       代    理:将通知织入到目标对象之后,形成代理对象

aspect      切    面:切入点+通知

Java实现AOP

详细代码如下:

UserService

package com.hyx.f_proxy.service;

public interface UserService {
    public void add();
    public void del();
    public void modify();
}

UserServiceImpl

package com.hyx.f_proxy.service.impl;

import com.hyx.f_proxy.service.UserService;

public class UserServiceImpl implements UserService {
    @Override
    public void add() {
        System.out.println("add");
    }

    @Override
    public void del() {
        System.out.println("del");
    }

    @Override
    public void modify() {
        System.out.println("modify");
    }
}

UserServiceFactory

package com.hyx.f_proxy;

import com.hyx.f_proxy.service.UserService;
import com.hyx.f_proxy.service.impl.UserServiceImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class UserServiceFactory {
    private UserServiceImpl userService;

    public UserServiceFactory(UserServiceImpl userService) {
        this.userService = userService;
    }

    public UserService createUserServiceProxy(){
        InvocationHandler h = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args)
                    throws Throwable {
                System.out.println("开启事务");
                Object invoke = method.invoke(userService, args);
                System.out.println("提交事务");
                return invoke;
            }
        };

        UserService us = (UserService) Proxy.newProxyInstance
                (this.getClass().getClassLoader(),
                userService.getClass().getInterfaces(),
                        h);
        return us;
    }
}

测试类

package com.hyx.f_proxy;

import com.hyx.f_proxy.service.UserService;
import com.hyx.f_proxy.service.impl.UserServiceImpl;
import org.junit.Test;

public class Demo {
    @Test
    public void fun(){
        UserServiceFactory usFactory =
                new UserServiceFactory(new UserServiceImpl());
        UserService userServiceProxy = usFactory.createUserServiceProxy();
        userServiceProxy.add();
    }
}

Spring实现AOP

Spring实现AOP时,封装了上面做的操作

需要做的操作就是告诉Spring为哪个类中的哪个方法进行增强

上述案例中的Service层不用修改,需要新建一个类,类中的方法就是增强的代码

MyAdvice中的方法

//通知
public class MyAdvice {
    public void  before(){

        System.out.println("这是前置通知");

    }
    public void after(){
        System.out.println("这是后置通知");
    }
}

Spring配置文件

 Spring配置文件具体内容如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
							http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
							http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
							http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">


    <context:component-scan
            base-package="com.hyx.h_spring_aop">
    </context:component-scan>

    <bean name="userService" class="com.hyx.h_spring_aop.service.impl.UserServiceImpl"></bean>

    <bean name="myAdvice" class="com.hyx.h_spring_aop.MyAdvice"></bean>

    <aop:config>
        <aop:pointcut id="pc"
                      expression="execution(* com.hyx.h_spring_aop.service.impl.*ServiceImpl.*(..))"
        ></aop:pointcut>

        <aop:aspect ref="myAdvice">
            <aop:before method="before" pointcut-ref="pc"></aop:before>
            <aop:after method="after" pointcut-ref="pc"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

 测试类代码,使用了注解的方式进行了测试

package com.hyx.h_spring_aop;



import com.hyx.h_spring_aop.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:h_spring_aop.xml")
public class Demo {
    @Autowired
    private UserService userService;

    @Test
    public void fun(){
        userService.add();
    }
}

 这样就简单实现了AOP

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无名一小卒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值