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。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值