Spring Aop基于注解&Xml

目录

1,【基于注解】

1.1,基本结构

1.2,实现步骤

2,【基于XML】

2.1,基本结构

2.2,实现步骤


1,【基于注解】

1.1,基本结构

        编写UserService,提供 insertUser 和 updateUser

        编写切面类,对两个方法进行事务管理(开始事务、结束事务)

        ​​​​​​​        Spring Aop基于注解&Xml

1.2,实现步骤

目标类

package com.czxy.demo16_aop_interface.service.impl;

import com.czxy.demo16_aop_interface.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Override
    public String insertUser() {
        System.out.println("添加");
        return "Hello";
    }

    @Override
    public String updateUser() {
        System.out.println("更新");
        return "World";
    }
}

 配置类

package com.czxy.demo16_aop_interface.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan({"com.czxy.demo16_aop_interface.service","com.czxy.demo16_aop_interface.aop"})
@EnableAspectJAutoProxy //开启AOP
public class Demo16Configuration {
}

测试类

package com.czxy.demo16_aop_interface;

import com.czxy.demo16_aop_interface.config.Demo16Configuration;
import com.czxy.demo16_aop_interface.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = Demo16Configuration.class)
public class TestDemo16 {
    @Resource
    private UserService userService;

    @Test
    public void test16(){
        userService.insertUser();
        userService.updateUser();
    }
}

切面类

package com.czxy.demo16_aop_interface.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect {
    //抽取切入点表达式
    @Pointcut("execution(* com.czxy.demo16_aop_interface..*.*(..))")
    private void myPointuct(){}

    @Before(value = "myPointuct()")
    public void start(){
        System.out.println("前置通知");
    }

    @After("myPointuct()")
    public void myAfter(JoinPoint joinPoint){
        System.out.println("最终通知");
    }
}

2,【基于XML】

2.1,基本结构

Spring Aop基于注解&Xml

2.2,实现步骤

 步骤1:UserService接口:

package com.czxy.demo21_xml_aop.service;

public interface UserService {
    Integer insertUser();
    String updateUser();
}

步骤2:UserServiceImpl实现类

package com.czxy.demo21_xml_aop.service.impl;

import com.czxy.demo21_xml_aop.service.UserService;


public class UserServiceImpl implements UserService {
    @Override
    public Integer insertUser() {
        System.out.println("Demo21 添加");
        return 1;
    }

    @Override
    public String updateUser() {
        System.out.println("Demo21 更新");
        return "更新结果";
    }
}

步骤3:MyAspect 切面类

package com.czxy.demo21_xml_aop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect {
    public void myBeforeAdvice(){
        System.out.println("开启事务");
    }

    public void myAfterReturningAdvice(JoinPoint joinPoint,Object obj){
        System.out.println("目标类:"+joinPoint.getTarget());
        System.out.println("方法名:"+joinPoint.getSignature().getName());
        System.out.println("返回值:"+obj);
        System.out.println("提交事务");
    }

    public Object myAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕前");
        //执行目标
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("环绕后");
        return proceed;
    }

    public void myAfterThrowingAdvice(Throwable e) {
        System.out.println("异常通知:" + e.getMessage());
    }

    public void myAfterAdvice() {
        System.out.println("释放资源");
    }
}

步骤4:测试类

package com.czxy.demo21_xml_aop;

import com.czxy.demo21_xml_aop.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "classpath:demo21.xml")
public class TestDemo21 {

    @Resource(name = "UserService12")
    private UserService userService;

    @Test
    public void TestDemo21(){
        userService.insertUser();
        System.out.println("-----------");
        userService.updateUser();
    }
}

xml配置文件

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

    <!--配置Service-->
    <bean id="UserService12" class="com.czxy.demo21_xml_aop.service.impl.UserServiceImpl"></bean>

    <!--aop1,配置切面类-->
    <bean id="myAspect" class="com.czxy.demo21_xml_aop.aop.MyAspect"></bean>
    <!--配置切面-->
    <aop:config>
        <aop:aspect ref="myAspect">
            <aop:pointcut id="pointcut" expression="execution(* com.czxy.demo21_xml_aop.service..*.*(..))"/>
<!--            前置通知-->
<!--            <aop:before method="myBeforeAdvice" pointcut-ref="pointcut"></aop:before>-->

<!--            后置通知-->
<!--            <aop:after-returning method="myAfterReturningAdvice" pointcut-ref="pointcut" returning="obj"></aop:after-returning>-->

<!--            环绕通知-->
<!--            <aop:around method="myAroundAdvice" pointcut-ref="pointcut"></aop:around>-->

<!--            抛出异常-->
<!--            <aop:after-throwing method="myAfterThrowingAdvice" pointcut-ref="pointcut" throwing="e"/>-->

<!--                最终通知-->
                <aop:after method="myAfterAdvice" pointcut-ref="pointcut"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

上一篇:理解python装饰器


下一篇:【Spring】AOP的作用和底层原理、AOP相关术语