绠€鍗曡В鏋怱pring鏍稿績IOC瀹瑰櫒鍘熺悊

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绠€鍗曡В鏋怱pring鏍稿績IOC瀹瑰櫒鍘熺悊相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/actor' title='actor'>actor   cat   ons   exce   classpath   map   ebe   context   鏍稿績   

灏嗗ぇ浣撴祦绋嬭В鏋愪簡涓€杈癸紝鍏蜂綋鍙互鐪嬫簮浠g爜涓€涓柟娉曚竴涓柟娉曠殑璺熶笅

XmlBeanFactory鐨勫姛鑳芥槸寤虹珛鍦?code>DefaultListableBeanFactory杩欎釜鍩烘湰瀹瑰櫒鐨勫熀纭€涓婄殑锛屽苟鍦ㄨ繖涓熀鏈鍣ㄧ殑鍩虹涓婂疄琛屼簡鍏朵粬璇稿XML璇诲彇鐨勯檮鍔犲姛鑳姐€?/p>

DefaultListableBeanFactory鏄?code>BeanFactory鐨勪竴涓粯璁ゅ疄鐜扮被銆?/p>

public class XmlBeanFactory extends DefaultListableBeanFactory {
    private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
    
    /**
     * Resource鏄疭pring涓涓庡閮ㄨ祫婧愮殑鎶借薄锛屾渶甯歌鐨勬槸瀵规枃浠剁殑鎶借薄锛岀壒鍒槸XML鏂囦欢銆?     * 鑰屼笖Resource閲岄潰閫氬父淇濆瓨浜哠pring浣跨敤鑰呯殑Bean瀹氫箟
     * 姣斿applicationContext.xml鍦ㄨ鍔犺浇鏃讹紝灏变細琚娊璞′负Resource鏉ュ鐞嗐€?     */
    public XmlBeanFactory(Resource resource) throws BeansException {
        this(resource, null);
    }
    
    public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
        super(parentBeanFactory);
        this.reader.loadBeanDefinitions(resource);
    }
}

绠€鍗曠ず渚嬶細

public class MySimpleBeanFactory {
    public static void main(String[] args) {
        ClassPathResource resource = new ClassPathResource("META-INF/beans.xml");
        BeanFactory beanFactory = new XmlBeanFactory(resource);
        User user = beanFactory.getBean("user1", User.class);
        System.out.println(user.getName()+" : "+user.getAge());
    }
}

鍒涘缓瀹瑰櫒鐨勬楠わ細

  1. 鍒涘缓bean閰嶇疆鏂囦欢鐨勬娊璞?code>Resource
  2. 鍒涘缓BeanFactory锛屼緥濡傦細XmlBeanFactory
  3. 瑙f瀽Bean锛屽垱寤轰竴涓?code>BeanDefinition璇诲彇鍣紝渚嬪锛?code>XmlBeanDefinitionReader->DefaultBeanDefinitionDocumentReader->BeanDefinitionParserDelegate
  4. 寰楀埌BeanDefinition鍚庢敞鍐屽埌DefaultListableBeanFactory涓細
// 鏈€鍚嶣eanDefinition灏辨槸鏀惧埌杩欎釜map閲岀殑
Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(256);

浠ュ悗浼氭參鎱㈠畬鍠勩€?/p>

以上是关于绠€鍗曡В鏋怱pring鏍稿績IOC瀹瑰櫒鍘熺悊的主要内容,如果未能解决你的问题,请参考以下文章

馃幆銆愭繁鍏ヨВ鏋愩€戣法绔鏋剁殑鏍稿績鎶€鏈埌搴曟槸浠€涔堬紵

娣卞害瀛︿範鍘熺悊璇﹁В鍙奝ython浠g爜瀹炵幇

涓轰粈涔堥潰璇曞畼鍠滄闂?ZooKeeper 鍩烘湰鍘熺悊锛?/h1>

Vue3 DOM Diff 鏍稿績绠楁硶瑙f瀽

銆怱pring銆?7 鍚庣画鐨勫涔犺ˉ鍏?vol1

Kafka 鏍稿績缁勪欢涔嬪崗璋冨櫒