Spring 瀹炰緥鍖栨柟寮忔湁鍑犵锛熶负浠€涔堜細鐢ㄥ埌 Cglib锛?
Posted 灏忓倕鍝?,
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 瀹炰緥鍖栨柟寮忔湁鍑犵锛熶负浠€涔堜細鐢ㄥ埌 Cglib锛?相关的知识,希望对你有一定的参考价值。
浣滆€咃細灏忓倕鍝?br>鍗氬锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fbugstack.cn" rel="nofollow">https://bugstack.cn
娌夋穩銆佸垎浜€佹垚闀匡紝璁╄嚜宸卞拰浠栦汉閮借兘鏈夋墍鏀惰幏锛侌煒?/blockquote>銆奡pring 鎵嬫捀涓撴爮銆嬬洰褰?/h1>
- [x] 绗?1 绔狅細寮€绡囦粙缁嶏紝鎴戣甯︿綘鎾?Spring 鍟︼紒
- [x] 绗?2 绔狅細灏忚瘯鐗涘垁锛屽疄鐜颁竴涓畝鍗曠殑Bean瀹瑰櫒
- [x] 绗?3 绔狅細鍒濇樉韬墜锛岃繍鐢ㄨ璁℃ā寮忥紝瀹炵幇 Bean 鐨勫畾涔夈€佹敞鍐屻€佽幏鍙?/a>
- [x] 绗?4 绔狅細宕湶澶磋锛屽熀浜嶤glib瀹炵幇鍚瀯閫犲嚱鏁扮殑绫诲疄渚嬪寲绛栫暐
- [ ] 绗?5 绔狅細缁?Bean 瀵硅薄濉厖灞炴€т俊鎭?/li>
- [ ] 绗?6 绔狅細寰呭綊妗?..
涓€銆佸墠瑷€
鎶€鏈垚闀匡紝鏄鍦烘櫙璁捐缁嗚妭涓嶆柇鐨勯洉鍒伙紒
浣犺寰楄嚜宸辩殑鎶€鏈粈涔堟椂鍊欏緱鍒颁簡蹇€熺殑鎻愰珮锛屾槸CRUD鍐欑殑澶氫簡浠ュ悗鍚楋紵鎯抽兘涓嶈鎯筹紝缁濆涓嶅彲鑳斤紒CRUD鍐欑殑鍐嶅涔熷彧鏄兘婊¤冻浣犱綔涓轰竴涓惉鐮栧伐鍏蜂汉锛屾暡鍑诲皯閫昏緫娴佹按浠g爜鐨勯€熷害鑰屽凡锛岃€岀紪绋嬭兘鍔涜繖涓€鍧楋紝闄や簡鏈€寮€濮嬬殑浠庝笉鐔熺粌鍒扮啛缁冧互澶栵紝灏卞緢灏戝啀鏈夊叾浠栨彁鍗囦簡銆?/p>
閭d綘鍙兘浼氭兂浠€涔堟墠鏄紪绋嬭兘鍔涙彁鍗囷紵鍏跺疄鏇村鐨勭紪绋嬭兘鍔涚殑鎻愬崌鏄綘瀵瑰鏉傚満鏅殑鏋舵瀯鎶婃帶浠ュ強瀵规瘡涓€涓妧鏈疄鐜扮粏鑺傜偣鐨勪笉鏂敤鍏锋湁瑙勬ā浣撻噺鐨勬祦閲忓啿鍑婚獙璇佹椂锛屾槸鍚﹁兘淇濊瘉绯荤粺绋冲畾杩愯浠庤€屽喅瀹氫綘瑙佽瘑浜嗗灏戙€佸鍒颁簡澶氬皯銆佹彁鍗囦簡澶氬皯锛?/p>
鏈€缁堝綋浣犲湪鎺ヤ竴涓骇鍝侀渶姹傛椂锛屽紑濮嬫€濊€?code>绋嬪簭鏁版嵁缁撴瀯鐨勮璁?/code>銆?code>鏍稿績鍔熻兘鐨勭畻娉曢€昏緫瀹炵幇銆?code>鏁翠綋鏈嶅姟鐨勮璁℃ā寮忎娇鐢?/code>銆?code>绯荤粺鏋舵瀯鐨勬惌寤烘柟寮?/code>銆?code>搴旂敤闆嗙兢鐨勯儴缃茬粨鏋?/code>锛岄偅涔堜篃灏辨槸鐨勭紪绋嬭兘鍔涚湡姝f彁鍗囩殑鏃跺€欙紒
浜屻€佺洰鏍?/h2>
杩欎竴绔犺妭鐨勭洰鏍囦富瑕佹槸涓轰簡瑙e喅涓婁竴绔犺妭鎴戜滑
鍩嬩笅鐨勫潙
锛岄偅鏄粈涔堝潙鍛紵鍏跺疄灏辨槸涓€涓叧浜?Bean 瀵硅薄鍦ㄥ惈鏈夋瀯閫犲嚱鏁拌繘琛屽疄渚嬪寲鐨勫潙銆?/p>鍦ㄤ笂涓€绔犺妭鎴戜滑鎵╁厖浜?Bean 瀹瑰櫒鐨勫姛鑳斤紝鎶婂疄渚嬪寲瀵硅薄浜ょ粰瀹瑰櫒鏉ョ粺涓€澶勭悊锛屼絾鍦ㄦ垜浠疄渚嬪寲瀵硅薄鐨勪唬鐮侀噷骞舵病鏈夎€冭檻瀵硅薄绫绘槸鍚﹀惈鏋勯€犲嚱鏁帮紝涔熷氨鏄濡傛灉鎴戜滑鍘诲疄渚嬪寲涓€涓惈鏈夋瀯閫犲嚱鏁扮殑瀵硅薄閭d箞灏辫鎶涘紓甯镐簡銆?/p>
鎬庝箞楠岃瘉锛熷叾瀹炲氨鏄妸 UserService 娣诲姞涓€涓惈鍏ュ弬淇℃伅鐨勬瀯閫犲嚱鏁板氨鍙互锛屽涓嬶細
public class UserService { private String name; public UserService(String name) { this.name = name; } // ... }
鎶ラ敊濡備笅锛?/p>
java.lang.InstantiationException: cn.bugstack.springframework.test.bean.UserService at java.lang.Class.newInstance(Class.java:427) at cn.bugstack.springframework.test.ApiTest.test_newInstance(ApiTest.java:51) ...
鍙戠敓杩欎竴鐜拌薄鐨勪富瑕佸師鍥犲氨鏄洜涓?
beanDefinition.getBeanClass().newInstance();
瀹炰緥鍖栨柟寮忓苟娌℃湁鑰冭檻鏋勯€犲嚱鏁扮殑鍏ュ弬锛屾墍浠ュ氨杩欎釜鍧戝氨鍦ㄨ繖绛夌潃浣犱簡锛?strong>閭d箞鎴戜滑鐨勭洰鏍囧氨寰堟槑鏄句簡锛屾潵鎶婅繖涓潙濉钩锛?/strong>涓夈€佽璁?/h2>
濉钩杩欎釜鍧戠殑鎶€鏈璁′富瑕佽€冭檻涓ら儴鍒嗭紝涓€涓槸涓叉祦绋嬩粠鍝悎鐞嗙殑鎶婃瀯閫犲嚱鏁扮殑鍏ュ弬淇℃伅浼犻€掑埌瀹炰緥鍖栨搷浣滈噷锛屽彟澶栦竴涓槸鎬庝箞鍘诲疄渚嬪寲鍚湁鏋勯€犲嚱鏁扮殑瀵硅薄銆?/p>
- 鍙傝€?Spring Bean 瀹瑰櫒婧愮爜鐨勫疄鐜版柟寮忥紝鍦?BeanFactory 涓坊鍔?
Object getBean(String name, Object... args)
鎺ュ彛锛岃繖鏍峰氨鍙互鍦ㄨ幏鍙?Bean 鏃舵妸鏋勯€犲嚱鏁扮殑鍏ュ弬淇℃伅浼犻€掕繘鍘讳簡銆?/li>- 鍙﹀涓€涓牳蹇冪殑鍐呭鏄娇鐢ㄤ粈涔堟柟寮忔潵鍒涘缓鍚湁鏋勯€犲嚱鏁扮殑 Bean 瀵硅薄鍛紵杩欓噷鏈変袱绉嶆柟寮忓彲浠ラ€夋嫨锛屼竴涓槸鍩轰簬 Java 鏈韩鑷甫鐨勬柟娉?
DeclaredConstructor
锛屽彟澶栦竴涓槸浣跨敤 Cglib 鏉ュ姩鎬佸垱寤?Bean 瀵硅薄銆?em>Cglib 鏄熀浜庡瓧鑺傜爜妗嗘灦 ASM 瀹炵幇锛屾墍浠ヤ綘涔熷彲浠ョ洿鎺ラ€氳繃 ASM 鎿嶄綔鎸囦护鐮佹潵鍒涘缓瀵硅薄鍥涖€佸疄鐜?/h2>
1. 宸ョ▼缁撴瀯
small-spring-step-03 鈹斺攢鈹€ src 鈹溾攢鈹€ main 鈹? 鈹斺攢鈹€ java 鈹? 鈹斺攢鈹€ cn.bugstack.springframework.beans 鈹? 鈹溾攢鈹€ factory 鈹? 鈹? 鈹溾攢鈹€ factory 鈹? 鈹? 鈹? 鈹溾攢鈹€ BeanDefinition.java 鈹? 鈹? 鈹? 鈹斺攢鈹€ SingletonBeanRegistry.java 鈹? 鈹? 鈹溾攢鈹€ support 鈹? 鈹? 鈹? 鈹溾攢鈹€ AbstractAutowireCapableBeanFactory.java 鈹? 鈹? 鈹? 鈹溾攢鈹€ AbstractBeanFactory.java 鈹? 鈹? 鈹? 鈹溾攢鈹€ BeanDefinitionRegistry.java 鈹? 鈹? 鈹? 鈹溾攢鈹€ CglibSubclassingInstantiationStrategy.java 鈹? 鈹? 鈹? 鈹溾攢鈹€ DefaultListableBeanFactory.java 鈹? 鈹? 鈹? 鈹溾攢鈹€ DefaultSingletonBeanRegistry.java 鈹? 鈹? 鈹? 鈹溾攢鈹€ InstantiationStrategy.java 鈹? 鈹? 鈹? 鈹斺攢鈹€ SimpleInstantiationStrategy.java 鈹? 鈹? 鈹斺攢鈹€ BeanFactory.java 鈹? 鈹斺攢鈹€ BeansException.java 鈹斺攢鈹€ test 鈹斺攢鈹€ java 鈹斺攢鈹€ cn.bugstack.springframework.test 鈹溾攢鈹€ bean 鈹? 鈹斺攢鈹€ UserService.java 鈹斺攢鈹€ ApiTest.java
宸ョ▼婧愮爜锛?code>鍏紬鍙枫€宐ugstack铏礊鏍堛€嶏紝鍥炲锛歋pring 涓撴爮锛岃幏鍙栧畬鏁存簮鐮?/code>
Spring Bean 瀹瑰櫒绫诲叧绯伙紝濡傚浘 4-2
鏈珷鑺?code>鈥滃~鍧戔€?/code>涓昏鏄湪鐜版湁宸ョ▼涓坊鍔?InstantiationStrategy 瀹炰緥鍖栫瓥鐣ユ帴鍙o紝浠ュ強琛ュ厖鐩稿簲鐨?getBean 鍏ュ弬淇℃伅锛岃澶栭儴璋冪敤鏃跺彲浠ヤ紶閫掓瀯閫犲嚱鏁扮殑鍏ュ弬骞堕『鍒╁疄渚嬪寲銆?/p>
2. 鏂板 getBean 鎺ュ彛
cn.bugstack.springframework.beans.factory.BeanFactory
public interface BeanFactory { Object getBean(String name) throws BeansException; Object getBean(String name, Object... args) throws BeansException; }
- BeanFactory 涓垜浠噸杞戒簡涓€涓惈鏈夊叆鍙備俊鎭?args 鐨?getBean 鏂规硶锛岃繖鏍峰氨鍙互鏂逛究鐨勪紶閫掑叆鍙傜粰鏋勯€犲嚱鏁板疄渚嬪寲浜嗐€?/li>
3. 瀹氫箟瀹炰緥鍖栫瓥鐣ユ帴鍙?/h3>
cn.bugstack.springframework.beans.factory.support.InstantiationStrategy
public interface InstantiationStrategy { Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException; }
- 鍦ㄥ疄渚嬪寲鎺ュ彛 instantiate 鏂规硶涓坊鍔犲繀瑕佺殑鍏ュ弬淇℃伅锛屽寘鎷細beanDefinition銆?beanName銆乧tor銆乤rgs
- 鍏朵腑 Constructor 浣犲彲鑳戒細鏈変竴鐐归檶鐢燂紝瀹冩槸 java.lang.reflect 鍖呬笅鐨?Constructor 绫伙紝閲岄潰鍖呭惈浜嗕竴浜涘繀瑕佺殑绫讳俊鎭紝鏈夎繖涓弬鏁扮殑鐩殑灏辨槸涓轰簡鎷垮埌绗﹀悎鍏ュ弬淇℃伅鐩稿搴旂殑鏋勯€犲嚱鏁般€?/li>
- 鑰?args 灏辨槸涓€涓叿浣撶殑鍏ュ弬淇℃伅浜嗭紝鏈€缁堝疄渚嬪寲鏃跺€欎細鐢ㄥ埌銆?/li>
4. JDK 瀹炰緥鍖?/h3>
cn.bugstack.springframework.beans.factory.support.SimpleInstantiationStrategy
public class SimpleInstantiationStrategy implements InstantiationStrategy { @Override public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException { Class clazz = beanDefinition.getBeanClass(); try { if (null != ctor) { return clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args); } else { return clazz.getDeclaredConstructor().newInstance(); } } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new BeansException("Failed to instantiate [" + clazz.getName() + "]", e); } } }
- 棣栧厛閫氳繃 beanDefinition 鑾峰彇 Class 淇℃伅锛岃繖涓?Class 淇℃伅鏄湪 Bean 瀹氫箟鐨勬椂鍊欎紶閫掕繘鍘荤殑銆?/li>
- 鎺ヤ笅鏉ュ垽鏂?ctor 鏄惁涓虹┖锛屽鏋滀负绌哄垯鏄棤鏋勯€犲嚱鏁板疄渚嬪寲锛屽惁鍒欏氨鏄渶瑕佹湁鏋勯€犲嚱鏁扮殑瀹炰緥鍖栥€?/li>
- 杩欓噷鎴戜滑閲嶇偣鍏虫敞鏈夋瀯閫犲嚱鏁扮殑瀹炰緥鍖栵紝瀹炰緥鍖栨柟寮忎负
clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args);
锛屾妸鍏ュ弬淇℃伅浼犻€掔粰 newInstance 杩涜瀹炰緥鍖栥€?/li>5. Cglib 瀹炰緥鍖?/h3>
cn.bugstack.springframework.beans.factory.support.CglibSubclassingInstantiationStrategy
public class CglibSubclassingInstantiationStrategy implements InstantiationStrategy { @Override public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(beanDefinition.getBeanClass()); enhancer.setCallback(new NoOp() { @Override public int hashCode() { return super.hashCode(); } }); if (null == ctor) return enhancer.create(); return enhancer.create(ctor.getParameterTypes(), args); } }
- 鍏跺疄 Cglib 鍒涘缓鏈夋瀯閫犲嚱鏁扮殑 Bean 涔熼潪甯告柟渚匡紝鍦ㄨ繖閲屾垜浠洿鍔犵畝鍖栫殑澶勭悊浜嗭紝濡傛灉浣犻槄璇?Spring 婧愮爜杩樹細鐪嬪埌 CallbackFilter 绛夊疄鐜帮紝涓嶈繃鎴戜滑鐩墠鐨勬柟寮忓苟涓嶄細褰卞搷鍒涘缓銆?/li>
6. 鍒涘缓绛栫暐璋冪敤
cn.bugstack.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory { private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy(); @Override protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException { Object bean = null; try { bean = createBeanInstance(beanDefinition, beanName, args); } catch (Exception e) { throw new BeansException("Instantiation of bean failed", e); } addSingleton(beanName, bean); return bean; } protected Object createBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args) { Constructor constructorToUse = null; Class<?> beanClass = beanDefinition.getBeanClass(); Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors(); for (Constructor ctor : declaredConstructors) { if (null != args && ctor.getParameterTypes().length == args.length) { constructorToUse = ctor; break; } } return getInstantiationStrategy().instantiate(beanDefinition, beanName, constructorToUse, args); } }
- 棣栧厛鍦?AbstractAutowireCapableBeanFactory 鎶借薄绫讳腑瀹氫箟浜嗕竴涓垱寤哄璞$殑瀹炰緥鍖栫瓥鐣ュ睘鎬х被
InstantiationStrategy instantiationStrategy
锛岃繖閲屾垜浠€夋嫨浜?Cglib 鐨勫疄鐜扮被銆?/li>- 鎺ヤ笅鏉ユ娊鍙?
createBeanInstance
鏂规硶锛屽湪杩欎釜鏂规硶涓渶瑕佹敞鎰?Constructor 浠h〃浜嗕綘鏈夊灏戜釜鏋勯€犲嚱鏁帮紝閫氳繃 beanClass.getDeclaredConstructors() 鏂瑰紡鍙互鑾峰彇鍒颁綘鎵€鏈夌殑鏋勯€犲嚱鏁帮紝鏄竴涓泦鍚堛€?/li>- 鎺ヤ笅鏉ュ氨闇€瑕佸惊鐜瘮瀵瑰嚭鏋勯€犲嚱鏁伴泦鍚堜笌鍏ュ弬淇℃伅
args
鐨勫尮閰嶆儏鍐碉紝杩欓噷鎴戜滑瀵规瘮鐨勬柟寮忔瘮杈冪畝鍗曪紝鍙槸涓€涓暟閲忓姣旓紝鑰屽疄闄?Spring
婧愮爜涓繕闇€瑕佹瘮瀵瑰叆鍙傜被鍨嬶紝鍚﹀垯鐩稿悓鏁伴噺涓嶅悓鍏ュ弬绫诲瀷鐨勬儏鍐碉紝灏变細鎶涘紓甯镐簡銆?/li>浜斻€佹祴璇?/h2>
1. 浜嬪厛鍑嗗
cn.bugstack.springframework.test.bean.UserService
public class UserService { private String name; public UserService(String name) { this.name = name; } public void queryUserInfo() { System.out.println("鏌ヨ鐢ㄦ埛淇℃伅锛?quot; + name); } @Override public String toString() { final StringBuilder sb = new StringBuilder(""); sb.append("").append(name); return sb.toString(); } }
- 杩欓噷鍞竴澶氬湪 UserService 涓坊鍔犵殑灏辨槸涓€涓湁 name 鍏ュ弬鐨勬瀯閫犲嚱鏁帮紝鏂逛究鎴戜滑楠岃瘉杩欐牱鐨勫璞℃槸鍚﹁兘琚疄渚嬪寲銆?/li>
2. 娴嬭瘯鐢ㄤ緥
cn.bugstack.springframework.test.ApiTest
@Test public void test_BeanFactory() { // 1.鍒濆鍖?BeanFactory DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); // 2. 娉ㄥ叆bean BeanDefinition beanDefinition = new BeanDefinition(UserService.class); beanFactory.registerBeanDefinition("userService", beanDefinition); // 3.鑾峰彇bean UserService userService = (UserService) beanFactory.getBean("userService", "灏忓倕鍝?quot;); userService.queryUserInfo(); }
- 鍦ㄦ娆$殑鍗曞厓娴嬭瘯涓櫎浜嗗寘鎷紱Bean 宸ュ巶銆佹敞鍐?Bean銆佽幏鍙?Bean锛屼笁涓楠わ紝杩橀澶栧鍔犱簡涓€娆″璞$殑鑾峰彇鍜岃皟鐢ㄣ€傝繖閲屼富瑕佹祴璇曢獙璇佸崟渚嬪璞$殑鏄惁姝g‘鐨勫瓨鏀惧埌浜嗙紦瀛樹腑銆?/li>
- 姝ゅ涓庝笂涓€绔犺妭娴嬭瘯杩囩▼涓笉鍚岀殑鏄紝鎴戜滑鎶?UserService.class 浼犻€掔粰浜?BeanDefinition 鑰屼笉鏄儚涓婁竴绔犺妭閭f牱鐩存帴 new UserService() 鎿嶄綔銆?/li>
3. 娴嬭瘯缁撴灉
鏌ヨ鐢ㄦ埛淇℃伅锛氬皬鍌呭摜 Process finished with exit code 0
- 浠庢祴璇曠粨鏋滄潵鐪嬶紝鏈€澶х殑鍙樺寲灏辨槸鍙互婊¤冻甯︽湁鏋勯€犲嚱鏁扮殑瀵硅薄锛屽彲浠ヨ瀹炰緥鍖栦簡銆?/li>
- 浣犲彲浠ュ皾璇曞垎鍒娇鐢ㄤ袱绉嶄笉鍚岀殑瀹炰緥鍖栫瓥鐣ワ紝鏉ヨ繘琛屽疄渚嬪寲銆?code>SimpleInstantiationStrategy銆?code>CglibSubclassingInstantiationStrategy
4. 鎿嶄綔妗堜緥
杩欓噷鎴戜滑鍐嶆妸鍑犵涓嶅悓鏂瑰紡鐨勫疄渚嬪寲鎿嶄綔锛屾斁鍒板崟鍏冩祴璇曚腑锛屾柟渚垮ぇ瀹舵瘮瀵瑰涔犮€?/p>
4.1 鏃犳瀯閫犲嚱鏁?/h4>
@Test public void test_newInstance() throws IllegalAccessException, InstantiationException { UserService userService = UserService.class.newInstance(); System.out.println(userService); }
- 杩欑鏂瑰紡鐨勫疄渚嬪寲涔熸槸鎴戜滑鍦ㄤ笂涓€绔犺妭瀹炵幇 Spring Bean 瀹瑰櫒鏃剁洿鎺ヤ娇鐢ㄧ殑鏂瑰紡
4.2 楠岃瘉鏈夋瀯閫犲嚱鏁板疄渚嬪寲
@Test public void test_constructor() throws Exception { Class<UserService> userServiceClass = UserService.class; Constructor<UserService> declaredConstructor = userServiceClass.getDeclaredConstructor(String.class); UserService userService = declaredConstructor.newInstance("灏忓倕鍝?quot;); System.out.println(userService); }
- 浠庢渶绠€鍗曠殑鎿嶄綔鏉ョ湅锛屽鏋滄湁鏋勯€犲嚱鏁扮殑绫婚渶瑕佸疄渚嬪寲鏃讹紝鍒欓渶瑕佷娇鐢?
getDeclaredConstructor
鑾峰彇鏋勯€犲嚱鏁帮紝涔嬪悗鍦ㄩ€氳繃浼犻€掑弬鏁拌繘琛屽疄渚嬪寲銆?/li>4.3 鑾峰彇鏋勯€犲嚱鏁颁俊鎭?/h4>
@Test public void test_parameterTypes() throws Exception { Class<UserService> beanClass = UserService.class; Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors(); Constructor<?> constructor = declaredConstructors[0]; Constructor<UserService> declaredConstructor = beanClass.getDeclaredConstructor(constructor.getParameterTypes()); UserService userService = declaredConstructor.newInstance("灏忓倕鍝?quot;); System.out.println(userService);
- 杩欎釜妗堜緥涓叾瀹炴渶鏍稿績鐨勭偣鍦ㄤ簬鑾峰彇涓€涓被涓墍鏈夌殑鏋勯€犲嚱鏁帮紝鍏跺疄涔熷氨鏄繖涓柟娉曠殑浣跨敤
beanClass.getDeclaredConstructors()
4.4 Cglib 瀹炰緥鍖?/h4>
@Test public void test_cglib() { Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(UserService.class); enhancer.setCallback(new NoOp() { @Override public int hashCode() { return super.hashCode(); } }); Object obj = enhancer.create(new Class[]{String.class}, new Object[]{"灏忓倕鍝?quot;}); System.out.println(obj); }
- 姝ゆ渚嬫紨绀轰娇鐢ㄩ潪甯哥畝鍗曪紝浣嗗叧浜?Cglib 鍦?Spring 瀹瑰櫒涓殑浣跨敤闈炲父澶氾紝涔熷彲浠ユ繁鍏ョ殑瀛︿範涓€涓?Cglib 鐨勬墿灞曠煡璇嗐€?/li>
鍏€佹€荤粨
- 鏈珷鑺傜殑涓昏浠ュ畬鍠勫疄渚嬪寲鎿嶄綔锛屽鍔?InstantiationStrategy 瀹炰緥鍖栫瓥鐣ユ帴鍙o紝骞舵柊澧炰簡涓や釜瀹炰緥鍖栫被銆傝繖閮ㄥ垎绫荤殑鍚嶇О涓庡疄鐜版柟寮忓熀鏈槸 Spring 妗嗘灦鐨勪竴涓缉灏忕増锛屽ぇ瀹跺湪瀛︿範杩囩▼涓篃鍙互浠?Spring 婧愮爜鎵惧埌瀵瑰簲鐨勪唬鐮併€?/li>
- 浠庢垜浠笉鏂殑瀹屽杽澧炲姞闇€姹傚彲浠ョ湅鍒扮殑锛屽綋浣犵殑浠g爜缁撴瀯璁捐鐨勮緝涓哄悎鐞嗙殑鏃跺€欙紝灏卞彲浠ラ潪甯稿鏄撲笖鏂逛究鐨勮繘琛屾墿灞曚笉鍚屽睘鎬х殑绫昏亴璐o紝鑰屼笉浼氬洜涓洪渶姹傜殑澧炲姞瀵艰嚧绫荤粨鏋勬贩涔便€傛墍浠ュ湪鎴戜滑鑷繁涓氬姟闇€姹傚疄鐜扮殑杩囩▼涓紝涔熻灏藉彲鑳界殑鍘昏€冭檻涓€涓壇濂界殑鎵╁睍鎬т互鍙婃媶鍒嗗ソ绫荤殑鑱岃矗銆?/li>
- 鍔ㄦ墜鏄涔犺捣鏉ユ渶蹇殑鏂瑰紡锛屼笉瑕佽鐪肩潧鏄劅瑙夌湅浼氫簡锛屼絾涓婃墜鎿嶄綔灏卞簾浜嗐€備篃甯屾湜鏈夐渶瑕佺殑璇昏€呭彲浠ヤ翰鎵嬫搷浣滀竴涓嬶紝鎶婁綘鐨勬兂娉曚篃铻嶅叆鍒板彲钀藉湴瀹炵幇鐨勪唬鐮侀噷锛岀湅鐪嬫兂鐨勫拰鍋氱殑鏄惁涓€鑷淬€?/li>
涓冦€佺郴鍒楁帹鑽?/h2>
- 闈㈢粡鎵嬪唽 路 绗?绡囥€婅鐭ヨ嚜宸辩殑鎶€鏈爤鐩插尯銆?/a>)
- 鍒氱伀浜嗙殑涓彴杞ご灏辨媶锛屼竴澶ф尝鍏徃鏀句笉涓嬪張鎷夸笉璧锋潵锛?/a>
- 宸ヤ綔涓や笁骞翠簡锛屾暣涓嶆槑鐧芥灦鏋勫浘閮界敾鍟ワ紵
- 鍒濊瘑棰嗗煙椹卞姩璁捐DDD钀藉湴
- 闈㈣瘯鐜板満锛氬皬浼欎即缇庡洟涓€闈㈢殑鍒嗕韩鍜屽垎鏋?鍚В绛?
以上是关于Spring 瀹炰緥鍖栨柟寮忔湁鍑犵锛熶负浠€涔堜細鐢ㄥ埌 Cglib锛?的主要内容,如果未能解决你的问题,请参考以下文章