栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringAop的三种实现方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringAop的三种实现方式

一. 什么是Aop(定义)
AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
二. Aop在Spring中的作用(定义)
提供声明式事务;允许用户自定义切面
横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等。
切面(ASPECT): 横切关注点 被模块化的 特殊对象。即,他是一个类。
通知(Advice): 切面必须要完成的工作。即,它是类中的一个方法。
目标(Target): 被通知对象。
代理(Proxy): 向目标对象应用通知之后创建的对象。
切入点(PointCut): 切面通知 执行的“地点”的定义。
连接点(JointPoint): 与切入点匹配的执行点。
三. 三种实现方式
【重点】使用AOP织入,需要导入一个依赖包


    org.aspectj
    aspectjweaver
    1.9.4

方式一:使用Spring的API接口(主要是SpringApi接口实现)
UserService

	package com.aop.service;


public interface UserService {
    void add();
    void delete();
    void update();
    void query();
}

UserServiceImpl
package com.aop.service;


public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("增加一个用户");
    }

    @Override
    public void delete() {
        System.out.println("删除一个用户");
    }

    @Override
    public void update() {
        System.out.println("修改一个用户");
    }

    @Override
    public void query() {
        System.out.println("查询一个用户");
    }
}

BeforeLog

package com.aop.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;



public class BeforeLog implements MethodBeforeAdvice {

    @Override
    
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
    }
}

AfterLog

package com.aop.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;


public class AfterLog implements AfterReturningAdvice {
    @Override
    //returnValue ; 返回值
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了"+method.getName() + "返回结果为"+returnValue);
    }
}

ApplicationContext.xml




    
    
    
    

    
    
        
        

        
        
        
    

MyTest

import com.aop.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context= new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

方式二:自定义来实现AOP(主要是切面定义)

UserService

package com.aop.service;


public interface UserService {
    void add();
    void delete();
    void update();
    void query();
}

UserServiceImpl

package com.aop.service;


public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("增加一个用户");
    }

    @Override
    public void delete() {
        System.out.println("删除一个用户");
    }

    @Override
    public void update() {
        System.out.println("修改一个用户");
    }

    @Override
    public void query() {
        System.out.println("查询一个用户");
    }
}

DiyPointCut

package com.aop.diy;


public class DiyPointCut {
    public void before(){
        System.out.println("======方法执行前=====");
    }


    public void alter(){
        System.out.println("======方法执行后=====");
    }
}

ApplicationContext




    
    
    
    

    
        
        
            
            
            
            
            
            
        

    

MyTest

import com.aop.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context= new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

方式三:使用注解实现

UserService

package com.aop.service;


public interface UserService {
    void add();
    void delete();
    void update();
    void query();
}

UserServiceImpl

package com.aop.service;


public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("增加一个用户");
    }

    @Override
    public void delete() {
        System.out.println("删除一个用户");
    }

    @Override
    public void update() {
        System.out.println("修改一个用户");
    }

    @Override
    public void query() {
        System.out.println("查询一个用户");
    }
}

AnnotationPointCut

package com.aop.diy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;



@Aspect//标注这个类是一个切面
public class AnnotationPointCut {

    @Before("execution(* com.aop.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("====方法执行前=======");
    }

    @After("execution(* com.aop.service.UserServiceImpl.*(..))")
    public void alter(){
        System.out.println("====方法执行后=======");
    }

    @Around("execution(* com.aop.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint pj) throws Throwable {
        System.out.println("环绕前");
        Object proceed = pj.proceed();
        System.out.println("环绕后");
    }

}

ApplicationContext




    
    
    
    
    
    

MyTest

import com.aop.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context= new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/397141.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号