测试之路,与你同行!
spring入门程序一
上一篇 /
下一篇 2012-03-08 19:31:48
/ 个人分类:spring
1、下载spring,解压。
2、eclipse新建普通工程,对工程添加external jar包,在spring解压后的dist/module目录下和lib目录下
3、在src下新建包,建立HelloBean.java
package com.springinaction.chapter01.hello;
public class HelloBean {
private String hello;
public String getHello() {
return hello;
}
public void setHello(String hello) {
this.hello = hello;
}
}
4、建立beans.xml,注意beans.xml一定要放在工程的根目录下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean" class="com.springinaction.chapter01.hello.HelloBean">
<property name="hello">
<value>"hello mm"</value>
</property>
</bean>
</beans>
4、建立测试类:HelloBeanTest.java
package com.springinaction.chapter01.hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class HelloBeanTest {
public static void main(String[] args){
ApplicationContext context = new FileSystemXmlApplicationContext("/beans.xml");
HelloBean hellot = (HelloBean)context.getBean("helloBean");
System.out.println(hellot.getHello());
}
}
运行即可看输出结果:
hello mm
收藏
举报
TAG: