Spring Aop注解が実現しました。


Spring-aop-理論知識Spring-Aop-注釈実現プロジェクト構成図
在这里插入图片描述
具体的な手順:
1、mavenプロジェクトの導入依存でプロジェクト構造を作成する

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.18</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.3.4</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
        </dependency>
    </dependencies>
2、インターフェースと実現類を書く

/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-05 10:26
 */
public interface TestDao {
    public void delete();
}

/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-05 10:27
 */
@Service
public class TestDaoImpl implements TestDao {
    public void delete() {
        System.out.println("       -->  ");
    }
}
3、うどん類

/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-10 18:04
 */
@Aspect
@Component
public class MyAspectAnnotation {
    @Pointcut("execution(* com.dao.*.*(..))")
    private void myPointCut(){
    }
    /**
     *        JoinPoint                
     *    @Before("myPointCut()") ==
     *    <aop:after method="after" pointcut-ref="myPointCut"/>
     **/
    @Before("myPointCut()")
    public void before(JoinPoint jp){
        System.out.print("    :      ");
        System.out.println("    :"+jp.getTarget()+",      :"+jp.getSignature().getName());
    }
    @AfterReturning("myPointCut()")
    public void afterReturning(JoinPoint jp){
        System.out.print("      :"+"        ");
        System.out.println(",      "+jp.getSignature().getName());
    }
    @Around("myPointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("    :       ,      ");
        Object obj = pjp.proceed();
        System.out.println("    :       ,      ");
        return obj;
    }
    @AfterThrowing(value = "myPointCut()",throwing = "throwable")
    public void except(Throwable throwable){
        System.out.println("    :"+"      "+throwable.getMessage());
    }

    @After("myPointCut()")
    public void after(){
        System.out.println("    :    ");
    }

}
4、appication.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: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 https://www.springframework.org/schema/aop/spring-aop.xsd">
        <context:component-scan base-package="com.dao"/>
        <context:component-scan base-package="com.aspect"/>
        <!--       AspectJ  -->
        <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
テスト

    @Test
    public void Test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        TestDao testDao = applicationContext.getBean("testDaoImpl", TestDaoImpl.class);
        testDao.delete();
        /**
         *   :
         *     :       ,      
         *     :          :com.dao.TestDaoImpl@2bef51f2,      :delete
         *        -->  
         *       :        ,      delete
         *     :    
         *     :       ,      
         */
    }
締め括りをつける
この文章はここまでです。あなたに助けを与えたいです。私たちのもっと多い内容に注目してください。