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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring18-3: 工厂bean代理-半自动

發布時間:2025/6/15 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring18-3: 工厂bean代理-半自动 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?切面類

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("前");// 手動執行目標方法Object obj = mi.proceed();System.out.println("后");return obj;} }

接口?

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

?實現類

public class UserServiceImpl implements UserService {@Overridepublic void addUser() {System.out.println("bean adduser...");}@Overridepublic void updateUser() {System.out.println("bean 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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 1.創建目標類 --><bean id="userService" class="com.atchina.b_factory_bean.UserServiceImpl"/><!-- 2.創建切面類 --><bean id="myAspect" class="com.atchina.b_factory_bean.MyAspect"/><!-- 3.創建代理類 ProxyFactoryBean用于創建工廠bean,生成特殊代理對象interfaces: 確定接口target: 確定目標類interceptorNames: 通知, 切面類的名稱, 類型是string[], --><bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="interfaces" value="com.atchina.b_factory_bean.UserService"></property><property name="target" ref="userService"></property><property name="interceptorNames" value="myAspect"></property></bean> </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/b_factory_bean/applicationContext.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(xmlPath);UserService userService = (UserService)ac.getBean("proxyService");userService.addUser();} }

?

總結

以上是生活随笔為你收集整理的spring18-3: 工厂bean代理-半自动的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。