日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Spring的AOP使用xml配置

發布時間:2025/6/15 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring的AOP使用xml配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

需要使用spring的包,大家自己全部導入進去即可。省4........

?

用戶管理接口

  • package?com.rx.spring;

  • public?interface?UserManager?{

  • ????public?void?addUser(String?username,?String?password);
  • ????
  • ????public?void?deleteUser(int?id);
  • ????

  • }
  • 用戶管理實現

  • package?com.rx.spring;

  • public?class?UserManagerImpl?implements?UserManager?{

  • ????public?void?addUser(String?username,?String?password)?{
  • ????????System.out.println("-------UserManagerImpl.addUser()----------");
  • ????}

  • ????public?void?deleteUser(int?id)?{
  • ????????System.out.println("-------UserManagerImpl.deleteUser()----------");
  • ????}

  • }
  • ?

    切面

  • package?com.rx.spring;

  • public?class?SecurityHandler?{
  • ????
  • ????private?void?checkSecurity()?{
  • ????????System.out.println("----------checkSecurity()---------------");
  • ????}
  • ????
  • }
  • ?

    applicationContext.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:tx="http://www.springframework.org/schema/tx"
  • ?????????xsi:schemaLocation="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  • ???????????http://www.springframework.org/schema/aop?http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  • ???????????http://www.springframework.org/schema/tx?http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  • ????
  • ????<bean?id="securityHandler"?class="com.rx.spring.SecurityHandler"/>???????????
  • ????
  • ????<bean?id="userManager"?class="com.rx.spring.UserManagerImpl"/>
  • ????
  • ????<aop:config>
  • ????????<aop:aspect?id="security"?ref="securityHandler">
  • ????????????<aop:pointcut?id="allAddMethod"?expression="execution(*?com.rx.spring.UserManagerImpl.add*(..))"/>
  • ????????????<aop:before?method="checkSecurity"?pointcut-ref="allAddMethod"/>
  • ????????????<aop:after?method="checkSecurity"?pointcut-ref="allAddMethod"/>
  • ????????</aop:aspect>
  • ????</aop:config>???
  • </beans>
  • 客戶端調用:

  • package?com.rx.spring;

  • import?org.springframework.beans.factory.BeanFactory;
  • import?org.springframework.context.support.ClassPathXmlApplicationContext;

  • public?class?Client?{

  • ????public?static?void?main(String[]?args)?{
  • ????????BeanFactory?factory?=?new?ClassPathXmlApplicationContext("applicationContext.xml");
  • ????????
  • ????????UserManager?userManager?=?(UserManager)factory.getBean("userManager");
  • ????????
  • ????????userManager.addUser("sd",?"123");
  • ????????userManager.deleteUser(1);
  • ????}
  • }
  • ?

    運行結果:

    ?

    ----------checkSecurity()---------------
    -------UserManagerImpl.addUser()----------
    ----------checkSecurity()---------------
    -------UserManagerImpl.deleteUser()----------

    ?

    總結

    以上是生活随笔為你收集整理的Spring的AOP使用xml配置的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。