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

Spring AOP定義Before增加實(shí)戰(zhàn)案例詳解

 更新時(shí)間:2020年01月06日 08:46:48   作者:cakincqm  
這篇文章主要介紹了Spring AOP定義Before增加,結(jié)合實(shí)例形式詳細(xì)分析了Spring面向切面AOP定義Before增加相關(guān)定義與使用技巧,需要的朋友可以參考下

本文實(shí)例講述了Spring AOP定義Before增加。分享給大家供大家參考,具體如下:

一 配置

<?xml version="1.0" encoding="GBK"?>
<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"
   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/aop
   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
   <!-- 指定自動(dòng)搜索Bean組件、自動(dòng)搜索切面類(lèi) -->
   <context:component-scan
      base-package="org.crazyit.app.service
      ,org.crazyit.app.aspect">
      <context:include-filter type="annotation"
        expression="org.aspectj.lang.annotation.Aspect" />
   </context:component-scan>
   <!-- 啟動(dòng)@AspectJ支持 -->
   <aop:aspectj-autoproxy />
</beans>

二 切面類(lèi)

package org.crazyit.app.aspect;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;
// 定義一個(gè)切面
@Aspect
public class AuthAspect {
  // 匹配org.crazyit.app.service.impl包下所有類(lèi)的、
  // 所有方法的執(zhí)行作為切入點(diǎn)
  @Before("execution(* org.crazyit.app.service.impl.*.*(..))")
  public void authority() {
    System.out.println("模擬執(zhí)行權(quán)限檢查");
  }
}

三 接口

Hello

package org.crazyit.app.service;
public interface Hello {
   // 定義一個(gè)簡(jiǎn)單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
   void foo();
   // 定義一個(gè)addUser()方法,模擬應(yīng)用中的添加用戶(hù)的方法
   int addUser(String name, String pass);
}

World

package org.crazyit.app.service;
public interface World {
   // 定義一個(gè)簡(jiǎn)單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
   public void bar();
}

四 實(shí)現(xiàn)類(lèi)

HelloImpl

package org.crazyit.app.service.impl;
import org.springframework.stereotype.Component;
import org.crazyit.app.service.*;
@Component("hello")
public class HelloImpl implements Hello {
  // 定義一個(gè)簡(jiǎn)單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
  public void foo() {
    System.out.println("執(zhí)行Hello組件的foo()方法");
  }
  // 定義一個(gè)addUser()方法,模擬應(yīng)用中的添加用戶(hù)的方法
  public int addUser(String name, String pass) {
    System.out.println("執(zhí)行Hello組件的addUser添加用戶(hù):" + name);
    return 20;
  }
}

WorldImpl

package org.crazyit.app.service.impl;
import org.springframework.stereotype.Component;
import org.crazyit.app.service.*;
@Component("world")
public class WorldImpl implements World {
  // 定義一個(gè)簡(jiǎn)單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
  public void bar() {
    System.out.println("執(zhí)行World組件的bar()方法");
  }
}

五 測(cè)試類(lèi)

package lee;
import org.springframework.context.*;
import org.springframework.context.support.*;
import org.crazyit.app.service.*;
public class BeanTest {
  public static void main(String[] args) {
    // 創(chuàng)建Spring容器
    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    Hello hello = ctx.getBean("hello", Hello.class);
    hello.foo();
    hello.addUser("孫悟空", "7788");
    World world = ctx.getBean("world", World.class);
    world.bar();
  }
}

六 測(cè)試結(jié)果

模擬執(zhí)行權(quán)限檢查
執(zhí)行Hello組件的foo()方法
模擬執(zhí)行權(quán)限檢查
執(zhí)行Hello組件的addUser添加用戶(hù):孫悟空
模擬執(zhí)行權(quán)限檢查
執(zhí)行World組件的bar()方法

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Spring框架入門(mén)與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 詳解Android開(kāi)發(fā)中Fragment的使用

    詳解Android開(kāi)發(fā)中Fragment的使用

    這篇文章主要介紹了詳解Android開(kāi)發(fā)中Fragment的使用,包括Java代碼中調(diào)用Fragment的方法,需要的朋友可以參考下
    2015-07-07
  • JavaWeb項(xiàng)目中JSP訪問(wèn)的問(wèn)題解決

    JavaWeb項(xiàng)目中JSP訪問(wèn)的問(wèn)題解決

    JSP文件一般有兩個(gè)存放位置,本文主要介紹了JavaWeb項(xiàng)目中JSP訪問(wèn)的問(wèn)題解決,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • 淺談Java中Collection和Collections的區(qū)別

    淺談Java中Collection和Collections的區(qū)別

    下面小編就為大家?guī)?lái)一篇淺談Java中Collection和Collections的區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-08-08
  • java代碼實(shí)現(xiàn)斗地主發(fā)牌功能

    java代碼實(shí)現(xiàn)斗地主發(fā)牌功能

    這篇文章主要介紹了java實(shí)現(xiàn)斗地主發(fā)牌功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • MyBatis實(shí)現(xiàn)萬(wàn)能Map和模糊查詢(xún)

    MyBatis實(shí)現(xiàn)萬(wàn)能Map和模糊查詢(xún)

    本文主要介紹了MyBatis實(shí)現(xiàn)萬(wàn)能Map和模糊查詢(xún),文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • springboot如何配置ssl支持https

    springboot如何配置ssl支持https

    在SpringBoot應(yīng)用中配置SSL支持HTTPS需要?jiǎng)?chuàng)建KeyStore并在application.yml中進(jìn)行相應(yīng)配置,首先,使用java的keytool工具創(chuàng)建KeyStore,這涉及到設(shè)置密鑰對(duì)、指定密鑰算法(RSA)、密鑰大小(2048位)、密鑰庫(kù)名稱(chēng)、證書(shū)有效期等,創(chuàng)建KeyStore后
    2024-10-10
  • Java中隨機(jī)函數(shù)變換的示例詳解

    Java中隨機(jī)函數(shù)變換的示例詳解

    這篇文章主要為大家詳細(xì)介紹了Java中隨機(jī)函數(shù)的變換,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定的幫助,感興趣的可以了解一下
    2022-08-08
  • Java 函數(shù)編程詳細(xì)介紹

    Java 函數(shù)編程詳細(xì)介紹

    這篇文章主要介紹了Java函數(shù)式編程,lambda表達(dá)式可以被認(rèn)為是一個(gè)匿名函數(shù),可以在函數(shù)接口的上下文中使用。函數(shù)接口是只指定一個(gè)抽象方法的接口,下面來(lái)看文章的詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-11-11
  • Spring Boot利用@Async異步調(diào)用:ThreadPoolTaskScheduler線程池的優(yōu)雅關(guān)閉詳解

    Spring Boot利用@Async異步調(diào)用:ThreadPoolTaskScheduler線程池的優(yōu)雅關(guān)閉詳解

    這篇文章主要給大家介紹了關(guān)于Spring Boot利用@Async異步調(diào)用:ThreadPoolTaskScheduler線程池的優(yōu)雅關(guān)閉的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • Spring 配置文件字段注入到List、Map

    Spring 配置文件字段注入到List、Map

    這篇文章主要介紹了Spring 配置文件字段注入到List、Map,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10

最新評(píng)論