Spring XML 기반 Aop 구현

8039 단어 SpringAopXML
프로젝트 구조
在这里插入图片描述
구체 적 절차
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
         *     :    
         */
    }
총결산
이 글 은 여기까지 입 니 다.당신 에 게 도움 을 줄 수 있 기 를 바 랍 니 다.또한 당신 이 우리 의 더 많은 내용 에 관심 을 가 질 수 있 기 를 바 랍 니 다!

좋은 웹페이지 즐겨찾기