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

Spring:AOP 在不影响原来业务的情况下实现动态增强

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

Spring:AOP 在不影响原来业务的情况下实现动态增强

1、什么是AOP

11.2、Aop在Spring中的作用 动态代理代理的是接口


11.3、使用spring实现AOP


aop需要导入的包


        
            org.aspectj
            aspectjweaver
            1.9.4
        
    
方式1:使用Spring的API接口

配置文件applicationContext.xml




    
    
    


    

        

        
        
        
    


UserServiceImpl

package com.lan.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 select() {
        System.out.println("查询了一个用户");
    }
}

Log

package com.lan.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {
    @Override
    //Method method:要执行的目标对象的方法
    //Object[] args:参数
    //Object target:目标对象
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行了");
    }
}

AfterLog

package com.lan.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

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

MyTest

import com.lan.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();
    }
}

方式2:自定义来实现aop
package com.lan.diy;

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

applicationContext.xml


    
    
        
            
            
            
            
            
        
    
方式3:注解实现AOP


AnnotationPointCat

package com.lan.diy;

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

@Aspect//标注这个类是一个切面
public class AnnotationPointCat {
    @Before("execution(* com.lan.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("======方法执行前=====");
    }
    @After("execution(* com.lan.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("======方法执行后======");
    }
}

applicationContext.xml



    

MyTest

import com.lan.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();
    }
}

动态代理实现方式有两种:基于接口jdk(默认)、基于类cglib


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

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

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