1.HelloBean.java文件
package com.bean;
public class HelloBean
{
private String helloword;
public void setHelloWord(String helloWord){
this.helloWord = helloWord;
}
public String getHelloWord(){
return helloWord;
}
}
2.beans-config.xml文件
<?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="helloZbean"
class="com.bean.HelloBean">
<property name="helloWord">
<value>Hello!Justin!</value>
</property>
</bean>
</beans>
dtd验证
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean>
</bean>
</beans>
3.SpringDemo.java文件
package com.bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
public class SpringDemo
{
public static void main(String[] atgs){
resource rs = new ClassPathResource("beans-config.xml");
BeanFactory factory=new XmlBeanFactory(rs);
HelloBean hello = (HelloBean) factory.getBean("helloBean");
System.out.println(hello.getHelloWord());
}
}
4.SpringDemo.java文件
package com.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo
{
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml");
HelloBean hello = (HelloBean) context.getBean("helloBean");
System.out.println(hello.getHelloWord());
}
}
注:一般用接口定义,在xml bean中指定具体的实现类.