分类: Java
2008-03-19 10:35:20
#javabean文件 package com.biaoflying; public class HelloBean { private String helloWord; public void setHelloWord(String helloWord){ this.helloWord=helloWord; } public String getHelloWord(){ return helloWord; } } |
#SpringDemo.java主程序 package com.biaoflying; import org.springframework.core.io.*; import org.springframework.beans.factory.*; import org.springframework.beans.factory.xml.*; public class SpringDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Resource rs=new FileSystemResource("beans-config.xml"); BeanFactory factory=new XmlBeanFactory(rs); HelloBean hello=(HelloBean)factory.getBean("helloBean"); System.out.println(hello.getHelloWord()); } } |
#beans-config.xml文件 ""> |
hello!I'm back |
package com.biaoflying; import org.springframework.context.*; import org.springframework.context.support.*; public class SpringDemo2 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context= new FileSystemXmlApplicationContext( "beans-config2.xml"); HelloBean2 hello=(HelloBean2)context.getBean("helloBean2"); System.out.print("Name: "); System.out.println(hello.getName()); System.out.print("Word: "); System.out.println(hello.getHelloWord()); } } |
package com.biaoflying; public class HelloBean2 { private String name; private String helloWord; public HelloBean2(){ } public HelloBean2(String name,String helloWord){ this.name=name; this.helloWord=helloWord; } public void setName(String name){ this.name=name; } public String getName(){ return name; } public void setHelloWord(String helloWord){ this.helloWord=helloWord; } public String getHelloWord(){ return helloWord; } } |
#beans-config2.xml文件 ""> |
#输出: Name: Justin Word: Hello |
package com.biaoflying; import org.springframework.context.*; import org.springframework.context.support.*; public class RefBeanDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context= new FileSystemXmlApplicationContext( "beans-config.xml"); HelloBean hello= (HelloBean)context.getBean("helloBean"); System.out.println(hello.getHelloWord()); System.out.print(" it's "); System.out.println(hello.getDate()); System.out.println("."); } } |
package com.biaoflying; import java.util.*; public class HelloBean { private String helloWord; private Date date; public void setHelloWord(String helloWord){ this.helloWord=helloWord; } public String getHelloWord(){ return helloWord; } public void setDate(Date date){ this.date=date; } public Date getDate(){ return date; } } |
""> |
#输出 hello!I'm back it's Wed Mar 19 10:51:56 CST 2008 |
#byType实现自动绑定 #beans-config2.xml ""> |
ApplicationContext context= new FileSystemXmlApplicationContext( "beans-config2.xml"); |
#byName实现自动绑定 ""> |
#constructor自动绑定 #在HelloBean文件中加入以下代码 public HelloBean(Date date){ this.date=date; } #修改beans-config2.xml文件 ""> |