首页 > 其他分享 >Spring AOP的三种方法

Spring AOP的三种方法

时间:2022-10-30 20:58:14浏览次数:57  
标签:Spring void System 三种 AOP println import public out

方法一  使用API接口实现(每个方法都需要配置aop约束)

 Log.java

import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;

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

 

 After.java

import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;

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

 

 UserService接口类和UserServiceImpl.java(三个方法都是从用这个)

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

-------------------------------------------------------
public class UserServiceImpl implements Userservice{
    @Override
    public void add() {
        System.out.println("add了一个用户");
    }

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

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

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

 

 applicationContext.xml

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

    <bean id="userService" class="com.zxy.service.UserServiceImpl"/>
    <bean id="log" class="com.zxy.log.Log"/>
    <bean id="afterlog" class="com.zxy.log.AfterLog"/>

<!--     配置AOP  配置约束 -->
<!--     方法一   使用API接口实现-->
    <aop:config>
<!--          切入点      -->
        <aop:pointcut id="pointcut" expression="execution(* com.zxy.service.UserServiceImpl.*(..))"/>

        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
    </aop:config>

 

 方法二    自定义切面

 DiyPointCut.java

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

    }
}

 

  applicationContext.xml

<bean id="diy" class="com.zxy.diy.DiyPointCut"/>
        <aop:config>
            <aop:aspect ref="diy">
                <aop:pointcut id="point" expression="execution(* com.zxy.service.UserServiceImpl.*(..))"/>

                <aop:before method="before" pointcut-ref="point"/>

                <aop:after method="after" pointcut-ref="point"/>
            </aop:aspect>
        </aop:config>

 

 方法三  注解开发

AnnotationPointCut.java

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

@Aspect
public class AnnotationPointCut {

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

    }
}

 

 applicationContext.xml

<!-- 方式三      使用注解 -->
        <bean id="annotationPointCut" class="com.zxy.diy.AnnotationPointCut"/>
        <!-- 开启注解支持       -->
        <aop:aspectj-autoproxy/>

 

 公共测试类:

import com.zxy.service.Userservice;
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();
    }
}

 

标签:Spring,void,System,三种,AOP,println,import,public,out
From: https://www.cnblogs.com/kidzxy/p/16842183.html

相关文章