欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

帶你了解Spring AOP的使用詳解

 更新時間:2021年07月12日 09:22:19   作者:wbcra  
這篇文章主要介紹了Spring AOP的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(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;        切入點,什么時候,執(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">
<!--代理對象是誰-->
        <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("正在驗證賬號信息");
    }
}

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)文章

  • Java實現(xiàn)折半插入排序算法的示例代碼

    Java實現(xiàn)折半插入排序算法的示例代碼

    折半插入排序(Binary Insertion Sort)是對插入排序算法的一種改進(jìn)。不斷的依次將元素插入前面已排好序的序列中。本文將利用Java語言實現(xiàn)這一排序算法,需要的可以參考一下
    2022-08-08
  • springboot實現(xiàn)發(fā)送QQ郵箱

    springboot實現(xiàn)發(fā)送QQ郵箱

    這篇文章主要為大家詳細(xì)介紹了springboot實現(xiàn)發(fā)送QQ郵箱,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Spring中的策略模式簡單實現(xiàn)與使用分析

    Spring中的策略模式簡單實現(xiàn)與使用分析

    這篇文章主要介紹了Spring中的策略模式簡單實現(xiàn)與使用分析,去初始化時除了?initMultipartResolver(上傳文件)沒有獲取?Properties?defaultStrategies;默認(rèn)策略,其他的八大件都會使用到策略模式,需要的朋友可以參考下
    2024-01-01
  • 基于SpringBoot和PostGIS的某國基地可視化實戰(zhàn)

    基于SpringBoot和PostGIS的某國基地可視化實戰(zhàn)

    本文以Java開發(fā)語言為例,使用SpringBoot框架來進(jìn)行后臺開發(fā),詳細(xì)講解如何使用Leaflet對PostGIS的全球基地信息進(jìn)行Web可視化,最后分享Web可視化結(jié)果,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • Java的Spring框架中DAO數(shù)據(jù)訪問對象的使用示例

    Java的Spring框架中DAO數(shù)據(jù)訪問對象的使用示例

    這篇文章主要介紹了Java的Spring框架中DAO數(shù)據(jù)訪問對象的使用示例,分為在Spring中DOA與JDBC以及與Hibernate的配合使用兩種情況來進(jìn)行演示,需要的朋友可以參考下
    2016-03-03
  • Idea2020.2創(chuàng)建JavaWeb項目(部署Tomcat)方法詳解

    Idea2020.2創(chuàng)建JavaWeb項目(部署Tomcat)方法詳解

    這篇文章主要介紹了Idea2020.2創(chuàng)建JavaWeb項目(部署Tomcat)方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • Java中關(guān)于泛型、包裝類及ArrayList的詳細(xì)教程

    Java中關(guān)于泛型、包裝類及ArrayList的詳細(xì)教程

    泛型可以在類或方法中預(yù)支地使用未知的類型。這篇文章主要介紹了Java中關(guān)于泛型、包裝類及ArrayList的詳細(xì)教程,需要的朋友可以參考下
    2021-12-12
  • java中switch case語句需要加入break的原因解析

    java中switch case語句需要加入break的原因解析

    這篇文章主要介紹了java中switch case語句需要加入break的原因解析的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • Java多線程之多線程異常捕捉

    Java多線程之多線程異常捕捉

    在java多線程程序中,所有線程都不允許拋出未捕獲的checked exception,也就是說各個線程需要自己把自己的checked exception處理掉,通過此篇文章給大家分享Java多線程之多線程異常捕捉,需要的朋友可以參考下
    2015-08-08
  • SpringBoot下使用自定義監(jiān)聽事件的流程分析

    SpringBoot下使用自定義監(jiān)聽事件的流程分析

    事件機(jī)制是Spring的一個功能,目前我們使用了SpringBoot框架,所以記錄下事件機(jī)制在SpringBoot框架下的使用,同時實現(xiàn)異步處理,這篇文章主要介紹了SpringBoot下使用自定義監(jiān)聽事件,需要的朋友可以參考下
    2023-08-08

最新評論