Spring学习(一)一个简单的Spring应用程序

1、在MyEclipse中创建一个web project,命名为spring
2、将spring相关的jar包放到lib目录下
2、创建con.gc.action包,com.gc.test包和com.gc.impl包。
3、创建HelloWorld类,如下:
package com.gc.action;

public class HelloWorld {

private String msg;

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

}
4、提供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"
       default-lazy-init="true">
    <bean id="helloWorld" class="com.gc.action.HelloWorld">
       <property name="msg">
         <value>Hello World</value>
       </property>
    </bean>
</beans>
并将该配置文件放到classpath下。
5、编写TestHelloWorld类:
package com.gc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.gc.action.HelloWorld;

public class TestHelloWorld {

public static void main(String[] args) {
// HelloWorld helloWorld = new HelloWorld();
// //利用set方法将hello world注入到程序中
// helloWorld.setMsg("Hello World!");
// //利用get方法获取设置的msg
// System.out.println(helloWorld.getMsg());

//通过配置文件为相关类设置信息【通过ApplicationContext获取config.xml,完成JavaBean与xml文件之间关系的建立,从而可以通过getXxx方法获取xml中定义的内容】
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:config.xml");
HelloWorld hello = (HelloWorld)context.getBean("helloWorld");
System.out.println(hello.getMsg());
}
}

第二步:

增加接口Hello,如下:
package com.gc.impl;

public interface Hello {

public String doSalutation();
}
编写ChHello类,如下:
package com.gc.action;

import com.gc.impl.Hello;

public class ChHello implements Hello {

private String msg;

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public String doSalutation() {
return "你好!"+this.msg;
}
}
编写EnHello类,如下:
package com.gc.action;

import com.gc.impl.Hello;

public class EhHello implements Hello {

private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String doSalutation() {
return "Hello "+this.msg;
}

}
修改main方法:
public static void main(String[] args) {
// HelloWorld helloWorld = new HelloWorld();
// //利用set方法将hello world注入到程序中
// helloWorld.setMsg("Hello World!");
// //利用get方法获取设置的msg
// System.out.println(helloWorld.getMsg());

//通过配置文件为相关类设置信息
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:config.xml");
Hello hello = (Hello)context.getBean("helloWorld");
System.out.println(hello.doSalutation());
}
修改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"
       default-lazy-init="true">
    <bean id="helloWorld" class="com.gc.action.EhHello">
       <property name="msg">
         <value>gf</value>
       </property>
    </bean>
</beans>
由此看到只修改config的配置文件,就可以修改程序的运行结果,以上功能展示Spring的核心即IOC。

猜你喜欢

转载自happyforever.iteye.com/blog/1485558
今日推荐