最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

帶你了解Spring AOP的使用詳解

 更新時(shí)間:2021年07月12日 09:22:19   作者:wbcra  
這篇文章主要介紹了Spring AOP的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

springmvc.xml

<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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    https://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    https://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    https://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <bean id="adminCheck" class="cn.hp.impl.AdminCheck"></bean>
    <bean id="transmaction" class="cn.hp.impl.Transmaction"></bean>
    <bean id="bankDao" class="cn.hp.impl.BankDaoImpl">
<!--        <property name="adminCheck" ref="adminCheck"></property>-->
<!--        <property name="transmaction" ref="transmaction"></property>-->
    </bean>
<!--    <aop:config>-->
<!--&lt;!&ndash;        切入點(diǎn),什么時(shí)候,執(zhí)行什么切入進(jìn)來&ndash;&gt;-->
<!--        <aop:pointcut id="savepoint" expression="execution(* cn.hp.impl.*.*(..))"/>-->
<!--&lt;!&ndash;        切面-用來做事情-執(zhí)行業(yè)務(wù)邏輯之前-你要做或執(zhí)行什么事情&ndash;&gt;-->
<!--        <aop:aspect id="adminincepter" ref="adminCheck">-->
<!--            <aop:before method="check" pointcut-ref="savepoint"></aop:before>-->
<!--        </aop:aspect>-->
<!--        -->
<!--        <aop:aspect id="transmactionincepter" ref="transmaction">-->
<!--            <aop:around method="dointcepter" pointcut-ref="savepoint"></aop:around>-->
<!--        </aop:aspect>-->
<!--    </aop:config>-->
    <bean id="logInfo" class="cn.hp.impl.LogInfo"></bean>
    <bean id="adminCheckInterceptor" class="cn.hp.interceptor.AdminCheckInterceptor">
        <property name="adminCheck" ref="adminCheck"></property>
    </bean>
    <bean id="logInfoInceptor" class="cn.hp.interceptor.LogInfoInceptor">
        <property name="logInfo" ref="logInfo"></property>
    </bean>
    <bean id="transmactionInterceptor" class="cn.hp.interceptor.TransmactionInterceptor">
        <property name="transmaction" ref="transmaction"></property>
    </bean>
<!--    注入代理類-->
    <bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--代理對(duì)象是誰-->
        <property name="target" ref="bankDao"></property>
<!--        代理的接口-->
        <property name="proxyInterfaces" value="cn.hp.dao.BankDao"></property>
<!--        代理的通知組件-->
        <property name="interceptorNames">
            <list>
                <value>adminCheckInterceptor</value>
                <value>logInfoInceptor</value>
                <value>transmactionInterceptor</value>
            </list>
        </property>
    </bean>



</beans>

BankDao

package cn.hp.dao;
public interface BankDao {
    /**
     * 存錢
     */
    public void remit();
    /**
     * 取錢
     */
    public void withdrawMoney();
    /**
     * 轉(zhuǎn)賬
     */
    public void transferAccounts();
}

AdminCheck

package cn.hp.impl;
public class AdminCheck {
    public void check(){
        System.out.println("正在驗(yàn)證賬號(hào)信息");
    }
}

BankDaoImpl

package cn.hp.impl;
import cn.hp.dao.BankDao;
public class BankDaoImpl implements BankDao {

    @Override
    public void remit() {
//        adminCheck.check();
//        transmaction.beginTransmaction();
        System.out.println("存錢的業(yè)務(wù)邏輯");
//        transmaction.closeTransmaction();
    }
    @Override
    public void withdrawMoney() {
//        adminCheck.check();
//        transmaction.beginTransmaction();
        System.out.println("取錢的業(yè)務(wù)邏輯");
//        transmaction.closeTransmaction();
    }
    @Override
    public void transferAccounts() {
        System.out.println("轉(zhuǎn)賬的業(yè)務(wù)邏輯");
    }
}

LogInfo

package cn.hp.impl;
public class LogInfo {
    public void write(){
        System.out.println("寫日志中");
    }
}

Transmaction

package cn.hp.impl;
import org.aspectj.lang.ProceedingJoinPoint;
public class Transmaction {
    public void beginTransmaction(){
        System.out.println("開始事務(wù)");
    }
    public void closeTransmaction(){
        System.out.println("結(jié)束事務(wù)");
    }
    public void dointcepter(ProceedingJoinPoint point) throws Throwable {
        beginTransmaction();
        point.proceed();
        closeTransmaction();
    }
}

AdminCheckInterceptor

package cn.hp.interceptor;
import cn.hp.impl.AdminCheck;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
import java.util.Set;
public class AdminCheckInterceptor implements MethodBeforeAdvice {
    private AdminCheck adminCheck;
    public AdminCheck getAdminCheck() {
        return adminCheck;
    }
    public void setAdminCheck(AdminCheck adminCheck) {
        this.adminCheck = adminCheck;
    }
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        adminCheck.check();
    }
}

LogInfoInceptor

package cn.hp.interceptor;
import cn.hp.impl.LogInfo;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class LogInfoInceptor implements AfterReturningAdvice {
    private LogInfo logInfo;
    public LogInfo getLogInfo() {
        return logInfo;
    }
    public void setLogInfo(LogInfo logInfo) {
        this.logInfo = logInfo;
    }
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        logInfo.write();
    }
}

TransmactionInterceptor

package cn.hp.interceptor;
import cn.hp.impl.Transmaction;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.ibatis.transaction.Transaction;
public class TransmactionInterceptor implements MethodInterceptor {
    private Transmaction transmaction;
    public Transmaction getTransmaction() {
        return transmaction;
    }
    public void setTransmaction(Transmaction transmaction) {
        this.transmaction = transmaction;
    }
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        transmaction.beginTransmaction();
        Object obj = invocation.proceed();
        transmaction.closeTransmaction();
        return obj;
    }
}

Test

package cn.hp.test;
import cn.hp.dao.BankDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
    public static void main(String[] args) {
        //test1();
//        test2();
        test3();
    }
    private static void test3() {
        ApplicationContext ac= new ClassPathXmlApplicationContext("springmvc.xml");
        BankDao proxyFactory = ac.getBean("proxyFactory", BankDao.class);
        proxyFactory.remit();
    }
    private static void test2() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("springmvc.xml");
        BankDao bd = ac.getBean("bankDao",BankDao.class);
        bd.transferAccounts();
    }
    private static void test1() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("springmvc.xml");
        BankDao bd = ac.getBean("bankDao",BankDao.class);
        bd.withdrawMoney();
    }
}

總結(jié)

本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • 一文詳解MVCC的執(zhí)行原理

    一文詳解MVCC的執(zhí)行原理

    MVCC是一種并發(fā)控制機(jī)制,用于解決數(shù)據(jù)庫并發(fā)訪問中,數(shù)據(jù)一致性問題,它通過在讀寫操作期間保存多個(gè)數(shù)據(jù)版本,以提供并發(fā)事務(wù)間的隔離性,本文將和大家簡單聊聊MVCC的執(zhí)行原理,需要的朋友可以參考下
    2023-12-12
  • @Conditional注解的使用場(chǎng)景和源碼解析

    @Conditional注解的使用場(chǎng)景和源碼解析

    這篇文章主要介紹了@Conditional注解的使用場(chǎng)景和源碼解析,@Conditional是一個(gè)條件注解,它的作用是判斷Bean是否滿足條件,如果滿足條件,則將Bean注冊(cè)進(jìn)IOC中,如果不滿足條件,則不進(jìn)行注冊(cè),需要的朋友可以參考下
    2023-11-11
  • 解決工具接口調(diào)用報(bào)錯(cuò):error:Unsupported Media Type問題

    解決工具接口調(diào)用報(bào)錯(cuò):error:Unsupported Media Type問題

    當(dāng)遇到"UnsupportedMediaType"錯(cuò)誤時(shí),意味著HTTP請(qǐng)求的Content-Type與服務(wù)器期望的不匹配,比如服務(wù)器期待接收J(rèn)SON格式數(shù)據(jù),而發(fā)送了純文本格式,常見的Content-Type類型包括text/html、application/json、multipart/form-data等
    2024-10-10
  • JMagick實(shí)現(xiàn)基本圖像處理的類實(shí)例

    JMagick實(shí)現(xiàn)基本圖像處理的類實(shí)例

    這篇文章主要介紹了JMagick實(shí)現(xiàn)基本圖像處理的類,實(shí)例分析了java圖像處理的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • 詳解java定時(shí)任務(wù)

    詳解java定時(shí)任務(wù)

    這篇文章主要為大家詳細(xì)介紹了java定時(shí)任務(wù),使用JDK中的Timer定時(shí)任務(wù)來實(shí)現(xiàn),感興趣的小伙伴們可以參考一下
    2016-03-03
  • Reactive反應(yīng)式編程及使用介紹

    Reactive反應(yīng)式編程及使用介紹

    這篇文章主要介紹了為什使用Reactive反應(yīng)式編程的原因分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進(jìn)步,早日升職加薪
    2022-02-02
  • SpringMVC中攔截器的實(shí)現(xiàn)

    SpringMVC中攔截器的實(shí)現(xiàn)

    SpringMVC 中的 Interceptor 攔截器是非常重要和相當(dāng)有用的,它的主要作用是攔截指定的用戶請(qǐng)求,并進(jìn)行相應(yīng)的預(yù)處理與后處理,這篇文章主要介紹了SpringMVC的攔截器相關(guān)知識(shí),需要的朋友可以參考下
    2022-01-01
  • Java順時(shí)針打印矩陣

    Java順時(shí)針打印矩陣

    這篇文章主要為大家詳細(xì)介紹了Java順時(shí)針打印矩陣,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • svn 清理失敗 (cleanup 失敗) 的快速解決方法

    svn 清理失敗 (cleanup 失敗) 的快速解決方法

    下面小編就為大家?guī)硪黄猻vn 清理失敗 (cleanup 失敗) 的快速解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-10-10
  • 詳解java封裝返回結(jié)果與RestControllerAdvice注解

    詳解java封裝返回結(jié)果與RestControllerAdvice注解

    這篇文章主要為大家介紹了java封裝返回結(jié)果與RestControllerAdvice注解實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09

最新評(píng)論

永州市| 宁武县| 隆化县| 茶陵县| 吉木乃县| 绥中县| 沙田区| 西昌市| 交口县| 苏尼特左旗| 红河县| 库尔勒市| 江门市| 仁怀市| 渭南市| 怀远县| 木兰县| 喜德县| 平度市| 临泽县| 恭城| 淳化县| 星座| 屯门区| 米泉市| 双江| 融水| 翼城县| 随州市| 马边| 新乡市| 浦城县| 龙口市| 博乐市| 万州区| 罗平县| 平顺县| 宜兰县| 丽江市| 汾西县| 东至县|