spring aop的幾種配置方式:

1.使用aspect配置aop:

  1. 編寫切面:
package com.aligns.spring.aop.aop.aspect;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


/**
 * 定義切面:模擬事物
 * @author kefan
 *
 */


@Aspect
public class TransactionAspect {

    /**
     * 定義切入點
     */
    @Pointcut("execution(* com.aligns.spring.aop.service.*.*(..))")
    public  void  transactionPointCut(){

    }

    /**
     * 定義前置通知:開啓事物
     */
    @Before("transactionPointCut()")
    public  void  beginTransaction(){
        System.out.println("事物開啓....");
    }

    /**
     * 定義後置通知:提交事物
     */
    @After("transactionPointCut()")
    public  void  CommitTransaction(){
        System.out.println("事物提交....");
    }

    /**
     * 定義異常通知:事物回滾
     */
    @AfterThrowing("transactionPointCut()")
    public  void  rollBackTransaction(){
        System.out.println("事物回滾....");
    }


}
  1. 在spring中配置
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"
    default-lazy-init="true">

    <description>Spring Configuration</description>


    <!-- 使用Annotation自動註冊Bean,解決事物失效問題:在主容器中不掃描@Controller註解,在SpringMvc中只掃描@Controller註解。 -->
    <context:component-scan base-package="com.aligns.spring"><!-- base-package 
            如果多個,用“,”分隔 -->
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>


    <!--使用aspectj創建aop  -->
    <aop:aspectj-autoproxy/>



    <bean id="logAspect" class="com.aligns.spring.aop.aop.aspect.TransactionAspect"></bean>


</beans>
  1. 編寫單元測試
package com.aligns.spring.aop.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import com.aligns.spring.aop.service.AccountService;
import com.aligns.spring.aop.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath*:spring-context.xml,classpath*:spring-context-jedis.xml")
@ContextConfiguration(locations = { "classpath*:/spring-context-aspect-test.xml"})  
@Transactional
public class AspectTest {

    @Resource AccountService accountService;
    @Resource UserService userService;


    @Test
    public void fun1(){

        accountService.inAccount();
        accountService.outAccount();
        userService.addUser();
        userService.deleteUser();

        System.out.println(accountService);
    }






}

2.使用編程式配置aop

  1. 定義切面
package com.aligns.spring.aop.aop.programming;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;


/**
 * 定義切面:模擬事物
 * @author kefan
 *
 */

public class TransactionAspect {

    /**
     * 定義切入點
     */
    @Pointcut("execution(* com.aligns.spring.aop.service.*.*(..))")
    public  void  transactionPointCut(){

    }

    /**
     * 定義前置通知:開啓事物
     */
    @Before("transactionPointCut()")
    public  void  beginTransaction(){
        System.out.println("事物開啓....");
    }

    /**
     * 定義後置通知:提交事物
     */
    @After("transactionPointCut()")
    public  void  CommitTransaction(){
        System.out.println("事物提交....");
    }

    /**
     * 定義異常通知:事物回滾
     */
    @AfterThrowing("transactionPointCut()")
    public  void  rollBackTransaction(){
        System.out.println("事物回滾....");
    }


}
  1. spring配置
<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"
    default-lazy-init="true">

    <description>Spring Configuration</description>


    <!-- 使用Annotation自動註冊Bean,解決事物失效問題:在主容器中不掃描@Controller註解,在SpringMvc中只掃描@Controller註解。 -->
    <context:component-scan base-package="com.aligns.spring"><!-- base-package 
            如果多個,用“,”分隔 -->
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>





    <!-- 定義切面 -->
    <bean id="logAspect" class="com.aligns.spring.aop.aop.aspect.TransactionAspect"></bean>



    <!-- aop相關的配置和說明 -->
    <aop:config>
        <!-- 定義相關的切面 -->
        <aop:aspect ref="logAspect">
            <!-- 定義切入點 -->
            <aop:pointcut expression="execution(* com.aligns.spring.aop.service.*.*(..))" id="transactionPointCut"/>
            <aop:before pointcut-ref="transactionPointCut" method="beginTransaction"/>
            <aop:after pointcut-ref="transactionPointCut" method="CommitTransaction"/>
            <aop:after-throwing pointcut-ref="transactionPointCut" method="rollBackTransaction"/>       
        </aop:aspect>

    </aop:config>

</beans>
  1. 編寫單元測試
package com.aligns.spring.aop.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import com.aligns.spring.aop.service.AccountService;
import com.aligns.spring.aop.service.UserService;



/**
 * 測試變成式的創建aop應用
 * @author kefan
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath*:spring-context.xml,classpath*:spring-context-jedis.xml")
@ContextConfiguration(locations = { "classpath*:/spring-context-programming-test.xml"})  
@Transactional
public class ProgrammingTest {


    @Resource AccountService accountService;
    @Resource UserService userService;


    @Test
    public void fun1(){

        accountService.inAccount();
        accountService.outAccount();
        userService.addUser();
        userService.deleteUser();

        System.out.println(accountService);
    }



}

3.使用工廠模式配置aop

  1. 編寫通知:(實現MethodBeforeAdvice等通知接口)
    BeforeLoginAdviser
package com.aligns.spring.aop.aop.proxyFactory;

import java.lang.reflect.Method;
import java.util.Arrays;

import org.springframework.aop.MethodBeforeAdvice;


/**
 * 用戶登錄前檢查是否有證書的情況
 * @author kefan
 *
 */
public class BeforeLoginAdviser implements  MethodBeforeAdvice  {

    @Override
    public void before(Method method, Object[] args, Object target)
            throws Throwable {
        System.out.println("執行的目標對象爲:"+target.getClass().getName());
        System.out.println("執行的目標對象的方法爲:"+method.getName());
        System.out.println("執行的目標對象的方法爲:"+Arrays.toString(args));
    }

}
  1. 編寫業務處理的接口
    UserLogin
package com.aligns.spring.aop.aop.proxyFactory;

public interface UserLogin {


    public  void  login();

}
  1. 編寫業務處理的實現類

UserLoginImpl

package com.aligns.spring.aop.aop.proxyFactory;

public class UserLoginImpl  implements UserLogin {

    @Override
    public void login() {
        System.out.println("用戶登錄....");

    }

}
  1. spring中配置代理工廠
    spring-context-factory-test.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"
    default-lazy-init="true">

    <description>Spring Configuration</description>


    <!-- 使用Annotation自動註冊Bean,解決事物失效問題:在主容器中不掃描@Controller註解,在SpringMvc中只掃描@Controller註解。 -->
    <context:component-scan base-package="com.aligns.spring"><!-- base-package 
            如果多個,用“,”分隔 -->
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>






    <!-- 編寫通知:通常需要實現 -->
    <bean id="beforeAdviser" class="com.aligns.spring.aop.aop.proxyFactory.BeforeLoginAdviser"></bean>



    <!-- 目標方法的配置 -->
    <bean id="target" class="com.aligns.spring.aop.aop.proxyFactory.UserLoginImpl"></bean>



    <bean id="userLogin" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target"  ref="target"/>
        <property name="singleton" value="false"></property>
        <property name="interceptorNames" value="beforeAdviser"></property>
        <property name="interfaces" value="com.aligns.spring.aop.aop.proxyFactory.UserLogin"></property>
    </bean>

</beans>
  1. 編寫單元測試
package com.aligns.spring.aop.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import com.aligns.spring.aop.aop.proxyFactory.UserLogin;



/**
 * 測試變成式的創建aop應用
 * @author kefan
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath*:spring-context.xml,classpath*:spring-context-jedis.xml")
@ContextConfiguration(locations = { "classpath*:/spring-context-factory-test.xml"})  
@Transactional
public class FactoryTest {


    //@Resource AccountService accountService;
    //@Resource UserService userService;

    @Resource UserLogin userLogin;


    @Test
    public void fun1(){
        userLogin.login();
    }



}
發佈了28 篇原創文章 · 獲贊 33 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章