在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();
}
重寫(xiě)接口類 - 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)文章
SpringMVC框架使用 Apache POI實(shí)現(xiàn)導(dǎo)出Excel
Excel 作為最常用的數(shù)據(jù)處理工具之一,經(jīng)常被用來(lái)存儲(chǔ)和展示數(shù)據(jù),本文將介紹如何在 SpringMVC 框架中使用 Apache POI 庫(kù)來(lái)實(shí)現(xiàn) Excel 文件的導(dǎo)出功能,有需要的可以參考一下2025-04-04
使用自定義參數(shù)解析器同一個(gè)參數(shù)支持多種Content-Type
這篇文章主要介紹了使用自定義參數(shù)解析器同一個(gè)參數(shù)支持多種Content-Type的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Spring Boot 2.4 對(duì)多環(huán)境配置的支持更改示例代碼
這篇文章主要介紹了Spring Boot 2.4 對(duì)多環(huán)境配置的支持更改,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
java實(shí)現(xiàn)數(shù)字轉(zhuǎn)換人民幣中文大寫(xiě)工具
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)數(shù)字轉(zhuǎn)換人民幣中文大寫(xiě)工具,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04

