Spring AOP定義Before增加實(shí)戰(zhà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">
<!-- 指定自動搜索Bean組件、自動搜索切面類 -->
<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>
<!-- 啟動@AspectJ支持 -->
<aop:aspectj-autoproxy />
</beans>
二 切面類
package org.crazyit.app.aspect;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.*;
// 定義一個切面
@Aspect
public class AuthAspect {
// 匹配org.crazyit.app.service.impl包下所有類的、
// 所有方法的執(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 {
// 定義一個簡單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
void foo();
// 定義一個addUser()方法,模擬應(yīng)用中的添加用戶的方法
int addUser(String name, String pass);
}
World
package org.crazyit.app.service;
public interface World {
// 定義一個簡單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
public void bar();
}
四 實(shí)現(xiàn)類
HelloImpl
package org.crazyit.app.service.impl;
import org.springframework.stereotype.Component;
import org.crazyit.app.service.*;
@Component("hello")
public class HelloImpl implements Hello {
// 定義一個簡單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
public void foo() {
System.out.println("執(zhí)行Hello組件的foo()方法");
}
// 定義一個addUser()方法,模擬應(yīng)用中的添加用戶的方法
public int addUser(String name, String pass) {
System.out.println("執(zhí)行Hello組件的addUser添加用戶:" + 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 {
// 定義一個簡單方法,模擬應(yīng)用中的業(yè)務(wù)邏輯方法
public void bar() {
System.out.println("執(zhí)行World組件的bar()方法");
}
}
五 測試類
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();
}
}
六 測試結(jié)果
模擬執(zhí)行權(quán)限檢查
執(zhí)行Hello組件的foo()方法
模擬執(zhí)行權(quán)限檢查
執(zhí)行Hello組件的addUser添加用戶:孫悟空
模擬執(zhí)行權(quán)限檢查
執(zhí)行World組件的bar()方法
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
使用遞歸刪除樹形結(jié)構(gòu)的所有子節(jié)點(diǎn)(java和mysql實(shí)現(xiàn))
下面小編就為大家?guī)硪黄褂眠f歸刪除樹形結(jié)構(gòu)的所有子節(jié)點(diǎn)(java和mysql實(shí)現(xiàn))。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
詳解Java虛擬機(jī)30個常用知識點(diǎn)之1——類文件結(jié)構(gòu)
這篇文章主要介紹了Java虛擬機(jī)類文件結(jié)構(gòu),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Java數(shù)組中的元素刪除并實(shí)現(xiàn)向前移的代碼
這篇文章主要介紹了Java數(shù)組中的元素刪除并實(shí)現(xiàn)向前移的代碼的相關(guān)資料,需要的朋友可以參考下2016-05-05
Java FTPClient連接池的實(shí)現(xiàn)
這篇文章主要介紹了Java FTPClient連接池的實(shí)現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
Spring Boot實(shí)現(xiàn)郵件發(fā)送必會的5種姿勢
這篇文章主要給大家介紹了關(guān)于Spring Boot實(shí)現(xiàn)郵件發(fā)送必會的5種姿勢,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
java實(shí)現(xiàn)pdf文件截圖的方法【附PDFRenderer.jar下載】
這篇文章主要介紹了java實(shí)現(xiàn)pdf文件截圖的方法,結(jié)合實(shí)例形式分析了java基于PDFRenderer.jar進(jìn)行pdf文件截圖的相關(guān)操作技巧,并附帶PDFRenderer.jar文件供讀者下載使用,需要的朋友可以參考下2018-01-01
SpringBoot項(xiàng)目中如何動態(tài)切換數(shù)據(jù)源、數(shù)據(jù)庫
本文主要介紹了SpringBoot項(xiàng)目中如何動態(tài)切換數(shù)據(jù)源、數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02

