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

使用Spring实现AOP

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

使用Spring实现AOP

要使用AOP,首先要导入依赖包


            org.aspectj
            aspectjweaver
            1.9.4
        

我们先写一个业务:

UserService:

package com.cyf.service;

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

UserServiceimpl:

package com.cyf.service;

public class UserServiceImpl implements UserService{
    public void add() {
        System.out.println("增加一个数据");
    }

    public void delete() {
        System.out.println("删除一个数据");
    }

    public void update() {
        System.out.println("修改一个数据");
    }

    public void select() {
        System.out.println("查询一个数据");
    }
}

方法一、使用SpringAPI接口:

先写两个日志类:

Log类:

package com.cyf.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"的"+method.getName()+"方法");
    }
}

AfterLog类:

package com.cyf.log;


import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AafterLog implements AfterReturningAdvice {
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("方法"+method.getName()+"被执行,返回值是"+returnValue);
    }
}

建一个application.xml配置文件:

    
    
    

    

        
        
        

    
    

测试类:

import com.cyf.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("application.xml");
        UserService userService = (UserService) context.getBean("userserviceimpl");
        userService.select();
    }
}


方法二、自定义来实现AOP(切面定义):

先写一个自定义日志类:

DiyLog类:

package com.cyf.diy;

import com.cyf.service.UserService;

public class DiyLog  {
    public void Before(){
        System.out.println("------执行前------");
    }
    public void After(){
        System.out.println("------执行后------");
    }
}

写application.xml配置文件:


  
   
    
        

          

            
            
        
    

测试类:

import com.cyf.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("application.xml");
        UserService userService = (UserService) context.getBean("userserviceimpl");
        userService.select();
    }
}


方法三、使用注解实现:

写一个annotation类:

package com.cyf.diy;

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

public class anntationPoint {
    @Before("execution(* com.cyf.service.UserServiceImpl.*(..))")
    public void Before(){
        System.out.println("-----方法前------");
    }
    @After("execution(* com.cyf.service.UserServiceImpl.*(..))")
    public void After(){
        System.out.println("-----方法后------");
    }
    @Around("execution(* com.cyf.service.UserServiceImpl.*(..))")
    public void Arround(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕前");
        Signature signature = jp.getSignature(); //获得签名
        Object proceed = jp.proceed(); //执行方法
        System.out.println("环绕后");
        System.out.println(proceed);
    }
}

写application.xml配置文件:


    
    
    

测试:

import com.cyf.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("application.xml");
        UserService userService = (UserService) context.getBean("userserviceimpl");
        userService.select();
    }
}

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

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

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