<?xml version=”1.0″ encoding=”UTF-8″?> <beans xmlns=”http://www.springframework.org/schema/beans” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd”> <bean id=”HelloWorld” class=”com.pqf.beans.HelloWorld”> <property name=”msg”> <value>HelloWorld</value> </property> </bean> </beans>
public class HelloWorld{ public String msg=null; public Date date=null; public void init() { msg=”HelloWorld”; date=new Date(); } …… }
public class HelloWorld implement InitializingBean { public String msg=null; public Date date=null; public void afterPropertiesSet() { msg="向全世界问好!"; date=new Date(); } …… }
HelloWorld hw=new HelloWorld(); BeanWrapper bw=new BeanWrapperImpl(hw); bw.setPropertyvalue(”msg”,”HelloWorld”); system.out.println(bw.getPropertyCalue(”msg”));
InputStream is=new FileInputStream(”config.xml”); XmlBeanFactory factory=new XmlBeanFactory(is); HelloWorld hw=(HelloWorld) factory.getBean(”HelloWorld”); system.out.println(hw.getMsg());
ApplicationContext actx=new FleSystemXmlApplicationContext(”config.xml”); HelloWorld hw=(HelloWorld) actx.getBean(”HelloWorld”); System.out.println(hw.getMsg());