https://bugstack.cn娌夋穩銆佸垎浜€佹垚闀匡紝璁╄嚜宸卞拰浠栦汉閮借兘鏈夋墍鏀惰幏锛侌煒?/blockquote>銆奡pring鎵嬫捀涓撴爮銆嬬洰褰?/h1>[x]绗?1绔狅細寮"/>

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锛?的主要内容,如果未能解决你的问题,请参考以下文章

python涓疄渚嬫柟娉曪紝绫绘柟娉曪紝闈欐€佹柟娉曠殑鍖哄埆

Selenium+Headless Firefox

syslog瀹炰緥璇﹁Вrsyslog

jQuery瀹炰緥-璁颁綇鐧诲綍淇℃伅

Echarts涓お闃冲浘锛圫unburst锛夌殑瀹炰緥

python缃戠粶缂栫▼鎬荤粨