【Spring学习笔记一】-Spring配置和简单实例

来源:互联网 发布:中国社交网络排名 编辑:程序博客网 时间:2024/06/05 10:28

一、Spring下载

官方直接下载地址如下:http://repo.springsource.org/libs-release-local/org/springframework/spring/,从3.2到4.2之间的版本都有。以3.2版本为例,下载spring-framework-3.2.0.RELEASE-dist.zip压缩文件。
二、Spring配置
将下载的压缩文件解压缩,里面有三个文件夹,其中,libs是用来存放spring框架的类库。下面在eclipse配置spring:
1.在eclipse中new-> Java Project,输入文件名mySpring,新建一个java工程文件。
2.在mySpring上右击,点击new->Folder,新建一个文件夹,输入文件夹名lib。
3.复制libs下面的所有jar文件,在lib中右击,然后粘贴。
4.在mySpring上右击,点击Build Path->Configure build Pat..,点击Add Jars..,选择mySpring->lib,选中所有jar文件,然后确定。
5.除了这些jar文件,还需要commons-logging.jar文件,下载地址为http://download.csdn.net/download/readgo_xxg/5339124,将此jar文件导入eclipse的步骤同上。
三、helloworld 实例
1.在src下新建package,名字为com.ceshi。

2.新建一个HelloWorld类文件,代码如下:

package com.ceshi;public class HelloWorld {private String message;public void setMessage(String message) {this.message = message;}public void getMessage() {System.out.println("Your Message" + message);}}

3.创建bean配置文件:新建一个xml文件,文件名为beans.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.0.xsd">   <bean id="helloWorld" class="com.ceshi.HelloWorld">       <property name="message" value="Hello World!"/>   </bean></beans>

4.创建主程序文件:新建一个类文件,文件名为MainAPP,代码如下:
package com.ceshi;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("com/ceshi/Beans.xml");HelloWorld obj = (HelloWorld)context.getBean("helloWorld");obj.getMessage();}}
5.执行run,输出结果Your MessageHello World!</span>

1 0
原创粉丝点击