在前面已經(jīng)簡(jiǎn)要的介紹了Spring的管理方式,下面對其做進(jìn)一步的解釋。在Spring中,有三種方式對Bean進(jìn)行管理,分別是BeanWrapper,BeanFactory,ApplicationContext.
下面分別對其做解釋?zhuān)?
1 BeanWrapper
先看一下代碼:
- package com.jnotnull;
-
- public class HelloWorld {
- public String message = null;
-
- HelloWorld(){
- }
-
- public String getMessage() {
- return message;
- }
-
- public void setMessage(String message) {
- this.message = message;
- }
-
- }
package com.jnotnull;public class HelloWorld {public String message = null;HelloWorld(){}public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}}在這里我們加入了一個(gè)無(wú)參數構造函數:因為
org.springframework.beans包遵循Sun發(fā)布的JavaBeans標準。 一個(gè)JavaBean是一個(gè)簡(jiǎn)單的包含無(wú)參數構造函數的類(lèi),并且包含seter和getter屬性方法。
下面看一下調用的測試類(lèi)
- package com.jnotnull;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
-
- public class Test {
- public static void main(String []args) throws Exception{
- Object object = Class.forName("com.jnotnull.HelloWorld");
- BeanWrapper bw = new BeanWrapperImpl(object);
- bw.setPropertyValue("message","HelloWorld");
- System.out.println(bw.getPropertyValue("message"));
- }
- }
package com.jnotnull;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;public class Test {public static void main(String []args) throws Exception{Object object = Class.forName("com.jnotnull.HelloWorld");BeanWrapper bw = new BeanWrapperImpl(object);bw.setPropertyValue("message","HelloWorld");System.out.println(bw.getPropertyValue("message"));}}由此我們可以看出,在BeanWrapper是不需要配置文件的。