我以一個簡單的示例解構(gòu)spring是怎樣管理java對象的。
首先,定義一個簡單的pojo,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.jvk.ken.spring; public class Demo { private String name; public Demo() { name= "I'm Demo." ; } public void printName() { System.out.println(name); } public void setName(String name) { this .name = name; } } |
對應(yīng)的spring配置文件如下:
1
2
3
4
5
6
7
8
9
10
11
|
<? xml version = "1.0" encoding = "UTF-8" ?> xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> < bean id = "demo" class = "com.jvk.ken.spring.DemoFactory" /> </ beans > |
簡單的測試代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package com.jvk.ken.spring; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; public class Test { public static void main(String[] args) throws Exception { testSpring(); } private static void testSpring() throws Exception { BeanFactory bf = new XmlBeanFactory( new ClassPathResource( "applicationContext.xml" )); Demo bean = (Demo) bf.getBean( "demo" ); System.out.println(bean.getClass()); bean.printName(); } } |
運(yùn)行Test類,輸出如下信息,說明一個簡單的spring示例成功運(yùn)行了。
1
2
3
4
|
2012 - 3 - 28 22 : 18 : 07 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [applicationContext.xml] class com.jvk.ken.spring.Demo I'm Demo. |
從簡短的Java代碼和xml配置文件可知,XmlBeanFactory通過讀取xml配置文件組裝javabean,當(dāng)用戶調(diào)用getBean方法時(shí)返回所需對象。為了模仿它的行為,我定義一個簡單的beanFactory。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.jvk.ken.spring; import java.util.HashMap; import java.util.Map; public class MyBeanFactory { // 保存bean的定義 Map<String, Class> beans = new HashMap<String, Class>(); public Object getBean(String id) throws InstantiationException, IllegalAccessException { return beans.get(id).newInstance(); } private String xmlFile; public MyBeanFactory(String xmlFile) throws ClassNotFoundException { super (); this .xmlFile = xmlFile; init(); } private void init() throws ClassNotFoundException { // 初始化與解析XML,這里略去實(shí)際解析XML的情況,使用硬編碼模仿 System.out.println( "配置文件:" +xmlFile); String className = "com.jvk.ken.spring.Demo" ; Class<?> loadClass = this .getClass().getClassLoader().loadClass( className); beans.put( "demo" , loadClass); } } |
測試代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.jvk.ken.spring; public class Test { public static void main(String[] args) throws Exception { testNotSpring(); } private static void testNotSpring() throws Exception { MyBeanFactory bf = new MyBeanFactory( "applicationContext.xml" ); Demo bean = (Demo) bf.getBean( "demo" ); System.out.println(bean.getClass()); bean.printName(); } } |
運(yùn)行后輸出如下信息:
1
2
3
|
配置文件:applicationContext.xml class com.jvk.ken.spring.Demo I'm Demo. |
以上短簡代碼展現(xiàn)了spring是怎樣充當(dāng)起最簡單的bean工廠。下面稍微調(diào)整一下代碼,分析一下spring會出現(xiàn)怎樣的現(xiàn)象。首先把Demo類的無參構(gòu)造方法改成private。
1
2
3
|
private Demo() { name= "I'm Demo." ; } |
運(yùn)行測試代碼發(fā)現(xiàn),spring測試結(jié)果沒有任何差別,但我自定義的MyBeanFactory就報(bào)了如下錯誤信息:
1
2
3
4
5
6
7
|
Exception in thread "main" java.lang.IllegalAccessException: Class com.jvk.ken.spring.MyBeanFactory can not access a member of class com.jvk.ken.spring.Demo with modifiers "private" at sun.reflect.Reflection.ensureMemberAccess(Reflection.java: 65 ) at java.lang.Class.newInstance0(Class.java: 349 ) at java.lang.Class.newInstance(Class.java: 308 ) at com.jvk.ken.spring.MyBeanFactory.getBean(MyBeanFactory.java: 12 ) at com.jvk.ken.spring.Test.testNotSpring(Test.java: 25 ) at com.jvk.ken.spring.Test.main(Test.java: 9 ) |
spring如此神奇?非也,是我寫的代碼過于簡陋而已,稍作修改,也是可以直接運(yùn)行的。
1
2
3
4
5
6
|
public Object getBean(String id) throws Exception { Class class1 = beans.get(id); Constructor declaredConstructor = class1.getDeclaredConstructor(); declaredConstructor.setAccessible( true ); return declaredConstructor.newInstance(); } |
以上是spring容器管理的最純粹的javabean。spring還支持另外一種bean,叫工廠bean,示例勝千言,請看代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.jvk.ken.spring; import org.springframework.beans.factory.FactoryBean; public class DemoFactory implements FactoryBean { @Override public Object getObject() throws Exception { return new Demo(); } @Override public Class getObjectType() { return Demo. class ; } @Override public Boolean isSingleton() { return false ; } } |
增加了DemoFactory類后,同時(shí)修改spring的配置文件
1
|
< bean id = "demo" class = "com.jvk.ken.spring.DemoFactory" /> |
其它代碼不作修改,運(yùn)行測試代碼后,輸出結(jié)果和原來完全一致。為什么明明配置了ID為demo的class為com.jvk.ken.spring.DemoFactory,返回的結(jié)果卻是Demo實(shí)例呢,這是因?yàn)閟pring檢測到DemoFactory是一種實(shí)現(xiàn)了FactoryBean接口的特殊bean,返回結(jié)果前會調(diào)用getObject方法,所以最后得到的是Demo對象。當(dāng)然,如果我們真的需要得到工廠bean,可以這樣寫bf.getBean("&demo")。
總結(jié)
以上就是本文關(guān)于spring實(shí)現(xiàn)bean對象創(chuàng)建代碼詳解的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/hypmxy/article/details/72675520