67,515
社区成员
发帖
与我相关
我的任务
分享
package com.tntxia.test.spring;
import java.util.Date;
public class HelloBean {
private String h;
private Date date;
public String getH() {
return h;
}
public void setH(String h) {
this.h = h;
}
public void sayHello(){
System.out.println(date+h);
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="date" class="java.util.Date"></bean>
<bean id="hello" class="com.tntxia.test.spring.HelloBean" autowire="byName">
<property name="h">
<value>Hello,World too</value>
</property>
</bean>
</beans>
package com.tntxia.test.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String args[]){
Resource resource = new ClassPathResource("bean.xml");
BeanFactory bf = new XmlBeanFactory(resource);
HelloBean helloBean = (HelloBean)bf.getBean("hello");
helloBean.sayHello();
}
}