日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring18-4: spring aop

發(fā)布時(shí)間:2025/6/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring18-4: spring aop 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

切面?

import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation;public class MyAspect implements MethodInterceptor{@Overridepublic Object invoke(MethodInvocation mi) throws Throwable {System.out.println("前");// 手動(dòng)執(zhí)行目標(biāo)方法Object obj = mi.proceed();System.out.println("后");return obj;} }

?接口

public interface UserService {void addUser();void updateUser(); }

實(shí)現(xiàn)類?

public class UserServiceImpl implements UserService {@Overridepublic void addUser() {System.out.println("config adduser...");}@Overridepublic void updateUser() {System.out.println("config updateUser...");} }

?配置文件

<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 1.創(chuàng)建目標(biāo)類 --><bean id="userService" class="com.atchina.c_spring_aop.UserServiceImpl"/><!-- 2.創(chuàng)建切面類 --><bean id="myAspect" class="com.atchina.c_spring_aop.MyAspect"/><!-- 3. aop編程使用<aop:config>配置<aop:pointcut> 切入點(diǎn)<aop:advisor>特殊的切面advice-ref="" 通知的引用pointcut-ref="" 切入點(diǎn)的引用expression="" 切入點(diǎn)表達(dá)式--> <aop:config><aop:pointcut expression="execution(* *.*(..))" id="myPointCut"/><aop:advisor advice-ref="myAspect" pointcut-ref="myPointCut" /></aop:config> </beans>

測試類:

import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestFactoryBean {@Testpublic void test(){String xmlPath = "com/atchina/c_spring_aop/applicationContext.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(xmlPath);UserService userService = (UserService)ac.getBean("userService");userService.addUser();} }

?

總結(jié)

以上是生活随笔為你收集整理的spring18-4: spring aop的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。