在Spring中如何使用動(dòng)態(tài)代理?
Spring動(dòng)態(tài)代理
定義自定義切面 - diyNodePoint
package com.lxc.diy;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
/**
* @Aspect 標(biāo)注這個(gè)了是一個(gè)切面
* @Before("切入點(diǎn)") === <aop:before method="beforeLog" pointcut-ref="point" />
* @After("切入點(diǎn)") === <aop:after method="afterLog" pointcut-ref="point" />
*/
@Aspect
public class diyNotePoint {
@Before("execution(* com.lxc.service.UserServiceImp.*(..))")
public void before() {
System.out.println("前置切面");
}
@After("execution(* com.lxc.service.UserServiceImp.*(..))")
public void after() {
System.out.println("后置切面");
}
}
定義接口 - UserService
package com.lxc.service;
public interface UserService {
public void query();
public void delete();
public void edit();
public void add();
}
重寫接口類 - UserServiceImp
package com.lxc.service;
public class UserServiceImp implements UserService{
@Override
public void query() {
System.out.println("query");
}
@Override
public void delete() {
System.out.println("delete");
}
@Override
public void edit() {
System.out.println("edit");
}
@Override
public void add() {
System.out.println("add");
}
}
beans.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:context="http://www.springframework.org/schema/context"
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">
<!--定義bean-->
<bean id="imp" class="com.lxc.service.UserServiceImp"/>
<bean id="diyNotePoint" class="com.lxc.diy.diyNotePoint" />
<!--添加:注解支持-->
<aop:aspectj-autoproxy />
</beans>
測(cè)試:
import com.lxc.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = ctx.getBean("imp", UserService.class);
userService.add();
}
}
輸出如下:

到此這篇關(guān)于在Spring中如何使用動(dòng)態(tài)代理?的文章就介紹到這了,更多相關(guān)Spring動(dòng)態(tài)代理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Web 簡(jiǎn)單的分頁(yè)顯示實(shí)例代碼
這篇文章主要介紹了Java Web 簡(jiǎn)單的分頁(yè)顯示實(shí)例代碼的相關(guān)資料,本文通過(guò),計(jì)算總的頁(yè)數(shù)和查詢指定頁(yè)數(shù)據(jù)兩個(gè)方法實(shí)現(xiàn)分頁(yè)效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
關(guān)于mybatis mapper類注入失敗的解決方案
這篇文章主要介紹了關(guān)于mybatis mapper類注入失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
給JavaBean賦默認(rèn)值并且轉(zhuǎn)Json字符串的實(shí)例
這篇文章主要介紹了給JavaBean賦默認(rèn)值并且轉(zhuǎn)Json字符串的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot項(xiàng)目解決跨域的四種方案分享
在用SpringBoot開(kāi)發(fā)后端服務(wù)時(shí),我們一般是提供接口給前端使用,但前端通過(guò)瀏覽器調(diào)我們接口時(shí),瀏覽器會(huì)有個(gè)同源策略的限制,即協(xié)議,域名,端口任一不一樣時(shí)都會(huì)導(dǎo)致跨域,這篇文章主要介紹跨域的幾種常用解決方案,希望對(duì)大家有所幫助2023-05-05
基于opencv+java實(shí)現(xiàn)簡(jiǎn)單圖形識(shí)別程序
這篇文章主要給大家介紹了如何基于opencv+java實(shí)現(xiàn)簡(jiǎn)單圖形識(shí)別程序的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01
Java中如何使用正則表達(dá)式提取各種類型括號(hào)中的內(nèi)容
最近在工作中遇到一個(gè)問(wèn)題,就是需要一個(gè)字符串中每一個(gè)中括號(hào)里的內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于Java中如何使用正則表達(dá)式提取各種類型括號(hào)中的內(nèi)容,需要的朋友可以參考下2023-06-06
Java解析zip文件,并識(shí)別壓縮包里面的文件轉(zhuǎn)換成可操作的IO流方式
這篇文章主要介紹了Java解析zip文件,并識(shí)別壓縮包里面的文件轉(zhuǎn)換成可操作的IO流方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Java org.w3c.dom.Document 類方法引用報(bào)錯(cuò)
這篇文章主要介紹了Java org.w3c.dom.Document 類方法引用報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

