Spring XMLに基づいてAopを実現

7983 ワード

プロジェクト構造
在这里插入图片描述
具体的な手順
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>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.6</version>
        </dependency>
    </dependencies>
2、TestDaoインターフェースと実現類を書く

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

/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-05 10:27
 */
public class TestDaoImpl implements TestDao {
    public void sayHello() {
        System.out.println("       -->    !    !");
    }
    public void save() {
        System.out.println("       -->  ");
    }
    public void modify() {
        System.out.println("       -->  ");
    }
    public void delete() {
        System.out.println("       -->  ");
    }
}
3、切麺類の作成

/**
 * @version 1.0
 * @author: crush
 * @date: 2021-03-10 17:12
 */
public class MyAspectXml {
    /**
     *        JoinPoint                
     **/
    public void before(JoinPoint jp){
        System.out.print("    :         ");
        System.out.println("    :"+jp.getTarget()+",      :"+jp.getSignature().getName());
    }
    public void afterReturning(JoinPoint jp){
        System.out.print("      :"+"        "  );
        System.out.println(",      "+jp.getSignature().getName());
    }
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("    :       ,      ");
        Object obj = pjp.proceed();
        System.out.println("    :       ,      ");
        return obj;
    }
    public void except(Throwable throwable){
        System.out.println("    :"+"      "+throwable.getMessage());
    }
    public void after(){
        System.out.println("    :    ");
    }
}```
### 4、application.xml  
```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
       http://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">
    <!--
    <aop:aspectj-autoproxy />
         spring       @aspectJ   bean    ,    。
    proxy-target-class  ,   false,    jdk        ,
     true :     CGLib          。
    -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <bean id="testDaoImpl" class="com.dao.TestDaoImpl"/>
    <bean id="myAspectXML" class="com.aspect.MyAspectXml"/>
<!--    <bean id="myAspectXML2" class="com.aspect.MyAspectXml2"/>-->
    <!--
          :<aop:pointcut>    <aop:aspect>   ,          <aop:aspect>         ,
              <aop:config>            ,    <aop:config>     。
    -->
    <aop:config>
        <!--     execution                  -->
        <aop:pointcut id="myPointCut" expression="execution(* com.dao.*.*(..))"/>
        <aop:aspect ref="myAspectXML">
			           <!--aop:after                after           
           method="after"   after          -->
            <aop:after method="after" pointcut-ref="myPointCut"/>
            <aop:before method="before" pointcut-ref="myPointCut"/>
            <aop:after-returning method="afterReturning" pointcut-ref="myPointCut"/>
            <aop:after-throwing method="except" throwing="throwable" pointcut-ref="myPointCut"/>
            <aop:around method="around" pointcut-ref="myPointCut"/>
        </aop:aspect>
    </aop:config>
</beans>
テスト

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