当前位置:   article > 正文

007 spring aop(通知)(xml)

007 spring aop(通知)(xml)

pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.aistart</groupId>
        <artifactId>Spring_demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Aspect_demo</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.3.30</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
    </dependencies>

</project>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

LogAspect.java



package com.aistart.aspect;

import org.aspectj.lang.ProceedingJoinPoint;


public class LogAspect {

    public void LogInfoAfter(){
        System.out.println("后置日志");
    }
    public void LogInfoBefore(){
        System.out.println("前置日志");
    }

    public void doAfterReturning(String result) {
        System.out.println("后置通知, 返回值: " + result);
    }
    public void doAfterThrowing(Exception e) {
        System.out.println("后置通知, 异常是: " + e);
    }
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("-----------------------");
        System.out.println("环绕通知: 进入方法");
        Object o = pjp.proceed();
        System.out.println("环绕通知: 退出方法");
        return o;
    }
}



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

StudentServiceImpl.java



package com.aistart.service.impl;

import com.aistart.service.StudentService;
import org.springframework.stereotype.Service;


@Service
public class StudentServiceImpl implements StudentService {


    @Override
    public void insertStudent() {

        System.out.println("加入一个学生的业务");
//        System.out.println(1/0);

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

StudentService.java


package com.aistart.service;

public interface StudentService {

    void insertStudent();
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

applicationContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">


    <context:component-scan base-package="com.aistart.service"></context:component-scan>
    <context:annotation-config></context:annotation-config>
<!--    <bean id="studentService" class="com.aistart.service.impl.StudentServiceImpl"/>-->

    <!--增强类/切面对象-->
    <bean id="logAspect" class="com.aistart.aspect.LogAspect"/>
    <!-- bean definitions here -->

    <aop:config>
        <aop:aspect ref="logAspect">

            <!--代理的功能-->
            <!--execution([修饰符] 返回类型 全限类名.函数名() )-->
            <aop:pointcut id="studentServiceCut" expression="execution(* com.aistart.service.impl.StudentServiceImpl.*(..))"/>
            <aop:pointcut id="studentServiceCut1" expression="execution(* com.aistart.service.impl.StudentServiceImpl.*(..))"/>

            <aop:before method="LogInfoBefore" pointcut-ref="studentServiceCut"></aop:before>
            <aop:after method="LogInfoAfter" pointcut-ref="studentServiceCut"></aop:after>

            <aop:around method="doAround" pointcut-ref="studentServiceCut"></aop:around>
            <aop:after-throwing throwing="e" method="doAfterThrowing" pointcut-ref="studentServiceCut"></aop:after-throwing>
            <aop:after-returning returning="result" method="doAfterReturning" pointcut-ref="studentServiceCut"></aop:after-returning>

        </aop:aspect>

    </aop:config>


</beans>




  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

StudentServiceImplTest.java


package com.aistart.service.impl;

import com.aistart.service.StudentService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.Assert.*;

public class StudentServiceImplTest {

    @Test
    public void insertStudent() {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");


        StudentService studentService = context.getBean( StudentService.class);

        studentService.insertStudent();
    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/430413
推荐阅读
相关标签
  

闽ICP备14008679号