봄 항목 : 마무리 요점 (3) - AOP

1 : AOP : 지향 프로그래밍

AOP 용어 :

joinpoint를 연결 지점 : 원래 개체의 방법

포인트 컷 진입 점 : 에이전트가 향상되었습니다 방법

조언 통지 향상은 : 코드 강화

독자 : 프록시 개체를 원래 개체

흔들며 직물 :의 엔트리 포인트에 통지 프로세스

프록시 에이전트 : 대상체 직물은 프록시 객체가 형성된다

섹션 측면 : 키 + 알림. 일반적으로 컷 클래스

알림 개체 :

대상 메소드 코드 앞에 추가 : 전에 통보하기 전에

후면 통지 후 : 대상 메소드 코드 이후에 추가. 여부 예외가 전화를 나타납니다. 예외 파이썬 try 블록에 해당하는 마지막입니다

후면 반환 통지 AfterRetruning : 방법에 따라 대상 코드입니다. 예외가 호출되지 않습니다. 다른 사람의 예외 파이썬 try 블록에 해당합니다

이전과 대상 방법 후 호출 : 조언 AroundReturning 약

이상 통지 후 : 예외 대상 메소드 호출 발생하면

2 : 프록시 모드의 바닥

: 프록시 모드는 이전 기사 참조 프록시 모드   https://blog.csdn.net/xinyuebaihe/article/details/104202951을

3 : :

구성 모드 및 메모로 나누어

예 디렉토리 구조

디렉토리 구조

LIB

1) 구성

섹션

package com.csdn.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

@Component
public class MyAdvice {
    public void before() throws Throwable {
        System.out.println("这是前置通知");
    }
    public void after(){
        System.out.println("这是后置通知");
    }
    public void afterReturning(){
        System.out.println("这是没有异常的后置通知,相当于python中的else");
    }
    public void exception(){
        System.out.println("这是发生异常后的通知");
    }
    /**
     * 增强版的环绕通知。强大无比,可以替代其它通知
     * @param jp
     * @throws Throwable
     */
    public void around2(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("这是环绕通知前");
        try{
            //前置通知
            this.before();
            jp.proceed();

        }catch (Exception e){
            //异常通知
            this.exception();
        }finally {
            //后置通知
            this.after();
        }
        //无异常后置通知
        this.afterReturning();
        System.out.println("这是环绕通知后");
    }
}

프로필

<?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:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="com.csdn.*"/>
    <!--切面类,通知对象的bean-->
    <bean id = "myAdvice" class="com.csdn.advice.MyAdvice"/>
    <!--切面配置-->
    <aop:config>
        <!--切入点定义,目标方法,要增强哪些方法-->
        <!--第一个星号:返回值。第二个星号:匹配的实现类。第三个星号:任意方法。两个点:方法的属性-->
        <aop:pointcut id="pointcut" expression="execution(* com.csdn.service.impl.*ServiceImpl.*(..))"/>
        <!--切面引入-->
        <aop:aspect ref = "myAdvice">
            <!--不同类型的通知-->
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
            <aop:after-returning method="afterReturning" pointcut-ref="pointcut"/>
            <aop:after-throwing method="exception" pointcut-ref="pointcut"/>
            <aop:around method="around2" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>


    <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
        <constructor-arg name="ds" ref="dataSource"/>
    </bean>

    <context:property-placeholder location="classpath*:db.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${db.driver}"/>
        <property name="jdbcUrl" value="${db.url}"/>
        <property name="user" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>
</beans>

테스트 카테고리

package com.csdn.test;

import com.csdn.pojo.Student;
import com.csdn.service.StudentService;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.sql.SQLException;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class Test {
    @Autowired
    private StudentService studentService;

    @org.junit.Test
    public void test01(){
//        List<Student> all = null;
//        try {
//            all = studentService.findAll();
//
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        System.out.println(all);
        studentService.add();
    }
}

2) 주석의 방법으로

섹션

<?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:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--包扫描。会生成bean-->
    <context:component-scan base-package="com.csdn.*"/>

    <!--配置自动切面生成。一定要有,要不然只有切面类注解也无效-->
    <aop:aspectj-autoproxy/>

    <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
        <constructor-arg name="ds" ref="dataSource"/>
    </bean>

    <context:property-placeholder location="classpath*:db.properties"/>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${db.driver}"/>
        <property name="jdbcUrl" value="${db.url}"/>
        <property name="user" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>
</beans>

프로필

package com.csdn.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAdvice2 {
    @Pointcut("execution(* com.csdn.service.impl.*ServiceImpl2.*(..))")
    public void pointcut(){
        
    }
    
    @Before("MyAdvice2.pointcut()")
    public void before() throws Throwable {
        System.out.println("这是前置通知");
    }
    @After("MyAdvice2.pointcut()")
    public void after(){
        System.out.println("这是后置通知");
    }

    @AfterReturning("MyAdvice2.pointcut()")
    public void afterReturning(){
        System.out.println("这是没有异常的后置通知,相当于python中的else");
    }

    @AfterThrowing("MyAdvice2.pointcut()")
    public void exception(){
        System.out.println("这是发生异常后的通知");
    }
    /**
     * 增强版的环绕通知。强大无比,可以替代其它通知
     * @param jp
     * @throws Throwable
     */
    @Around("MyAdvice2.pointcut()")
    public void around2(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("这是环绕通知前");
        try{
            //前置通知
            this.before();
            jp.proceed();

        }catch (Exception e){
            //异常通知
            this.exception();
        }finally {
            //后置通知
            this.after();
        }
        //无异常后置通知
        this.afterReturning();
        System.out.println("这是环绕通知后");
    }

}

테스트 카테고리

package com.csdn.test;

import com.csdn.service.StudentService;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring2.xml")
public class Test2 {
    @Autowired()
    private StudentService studentService2;

    @org.junit.Test
    public void test01(){
//        List<Student> all = null;
//        try {
//            all = studentService.findAll();
//
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        System.out.println(all);
        studentService2.add();
    }
}

 

게시 된 168 개 원래 기사 · 원 찬양 12 ·은 90000 +를 볼

추천

출처blog.csdn.net/xinyuebaihe/article/details/104687407