Spring入门hello world
来源:互联网 发布:ping服务端口 编辑:程序博客网 时间:2024/05/09 06:46
下载spring框架
http://www.springsource.org/spring-community-download
另外,不知道为什么,这里没有commons logging这个jar包,要在下面的链接中下载,是下载···bin.zip文件
http://commons.apache.org/proper/commons-logging/download_logging.cgi
打开eclipse,新建一个“java Project” 然后,点击新建的项目,右键-Properties-java Builder Path,在libraries中找到“Add Library”-user library-User libraries,新建一个User Library,名为“spring3.2.2”将lib中的几个jar包都导入进去,在把commons-logging的jar包也一起导进去
当然,也可以把用到的几个jar包导入进去
接下来编写第一个组件(component),他只是一个简单的JavaBean。
package onlyfun.caterpillar;
public class HelloBean {
private String helloWorld;
public void setHelloWorld(String helloWorld){
this.helloWorld = helloWorld;
}
public String getHelloWorld(){
return this.helloWorld;
}
}
接下来,在src目录中新建一个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-3.2.xsd">
<bean id="helloBean"
class="onlyfun.caterpillar.HelloBean">
<property name="helloWorld">
<value>Hello!riseaocean!</value>
</property>
</bean>
</beans>
最后,编写一个示范程序来运行
package onlyfun.caterpillar;
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 SpringDemo {
@SuppressWarnings({ "deprecation", "resource" })
public static void main(String []args){
Resource rs = new ClassPathResource("beans-config.xml");
BeanFactory factory = new XmlBeanFactory(rs);
HelloBean hello = (HelloBean)factory.getBean("helloBean");
System.out.println(hello.getHelloWorld());
}
}
当然,由于提示The type XmlBeanFactory is deprecated,可以把程序改为:
package onlyfun.caterpillar;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo {
@SuppressWarnings({ "resource" })
public static void main(String []args){
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"beans-config.xml"});
BeanFactory factory = context;
HelloBean helloBean = (HelloBean)factory.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());
}
}
程序运行结果:
Hello!riseaocean!
Hello!riseaocean!
- Spring入门hello world
- Spring mvc ---Hello World入门
- Spring MVC 入门 - Hello World
- Spring MVC 入门--Hello World
- Spring之hello world(Spring入门)
- spring MVC入门示例(hello world demo)
- Spring Boot快速入门之Hello World
- Spring MVC 框架入门-Hello World 实例
- Spring入门hello world常见问题及解决办法
- Spring入门hello world常见问题及解决办法
- Spring入门hello world常见问题及解决办法
- Spring入门hello world常见问题及解决办法
- (2) spring boot 入门Hello World
- spring boot 入门篇Hello world
- Spring Boot快速入门(Hello World)
- Spring boot 入门第一天Hello world
- Andorid入门Hello,World
- Dojo入门Hello World!
- web.xml详解
- 数值分析之龙贝格求积法
- 让代码飞一会儿,超级有用的CSS编码工具集
- 【2013Esri全球用户大会精彩看点】ArcGIS 10.2三维新特性
- Intermediate Studio Tutorial
- Spring入门hello world
- poj 3667
- zoj1199-Point of Intersection
- 6-3 Young tableaus
- C# MediaPlayer 操作时注意
- fatal error C1189: #error : file must be compiled with _AFXDLL
- hdfs-over-ftp安装与配置
- html表格table实现鼠标移入移出行变色的一种可用方法
- 在当前目录中所有文件中,查找,存在指定内容的指定文件类型