Spring笔记1–入门helloworld项目

2016年1月29日

9:36

Spring笔记1–入门helloworld项目

 

配置环境:到eclipse上install softwear中输入http://dist.springsource.com/release/TOOLS/update/e4.3/

安装带spring IDE的4个部分,完成后重启eclipse

写一个HelloWorld项目:

1.创建一个名为Helloworld的java项目,在项目下创建一个叫做lib的文件夹,导入所需的jar包(5个),将其建立成一个libraries。

2.在src目录下创建com.firefly.spring.beans包,在里面创建一个HelloWorld.java文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
package com.firefly.spring.beans;                
public class HelloWorld {                
 private String name;                
  
 public void setName(String name) {                
  this.name = name;                
 }                
  public void hello(){                
  System.out.println(“hello:”+name);                
 }                
}

 3.创建配置文件:applicationContext.xml

?

1
2
3
4
5
6
7
8
9
10
11
<?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.xsd">                
  
  
 <bean id=”helloWorld” class=”com.firefly.spring.beans.HelloWorld”>                
  <property name=”name” value=”Spring”></property>                
 </bean>                
  
</beans>

 4.在com.firefly.spring.beans包中创建Main.java文件:

?

1
2
3
4
5
6
7
8
9
10
11
1
package com.firefly.spring.beans;                
import org.springframework.context.ApplicationContext;                
import org.springframework.context.support.ClassPathXmlApplicationContext;                
public class main {                
  
 public static void main(String[] args){                
 //创建spring容器                
  ApplicationContext ctx = new ClassPathXmlApplicationContext(“applicationContext.xml”);                
  //获得实例                
  HelloWorld helloWorld = (HelloWorld)ctx.getBean(“helloWorld”);                
  helloWorld.hello();                
 }                
}

 

已使用 Microsoft OneNote 2016 创建。