绠€鍗曡В鏋怱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());
}
}
鍒涘缓瀹瑰櫒鐨勬楠わ細
- 鍒涘缓bean閰嶇疆鏂囦欢鐨勬娊璞?code>Resource
鍒涘缓BeanFactory
锛屼緥濡傦細XmlBeanFactory
瑙f瀽Bean锛屽垱寤轰竴涓?code>BeanDefinition
璇诲彇鍣紝渚嬪锛?code>XmlBeanDefinitionReader
->DefaultBeanDefinitionDocumentReader
->BeanDefinitionParserDelegate
BeanDefinition
鍚庢敞鍐屽埌DefaultListableBeanFactory
涓細// 鏈€鍚嶣eanDefinition灏辨槸鏀惧埌杩欎釜map閲岀殑
Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(256);
浠ュ悗浼氭參鎱㈠畬鍠勩€?/p>
以上是关于绠€鍗曡В鏋怱pring鏍稿績IOC瀹瑰櫒鍘熺悊的主要内容,如果未能解决你的问题,请参考以下文章