Spring 的配置

  • 安装JDK环境,并配置环境变量。(参考上一篇:)
  • 安装 Apache Commons Logging API
    1.从http://commons.apache.org/proper/commons-logging/官网上下载Apache Commons Logging API的最新版本。下载完安装包后,将二进制文件的发行版本解压。例如我的目录是:D:\Spring\commons-logging-1.2,并将其路径加入CLASSPATH环境变量中。
  • 安装 Eclipse
    1.从 http://www.eclipse.org/downloads/ 上下载最新的eclipse的IDE安装包,解压后安装。
  • 安装 Spring 框架库
    1.如果以前的这三个步骤没有出错,请继续设置你的 Spring 框架。
    2.从http://repo.spring.io/release/org/springframework/spring/ 上下载你所需要的 Spring 框架的二进制文件。
    3.我下载的是 spring-framework-4.1.6.RELEASE-dist.zip 这个版本,内部结构如下图所示。
    其中 libs 的目录下的文件如下图所示:

    -至此,Spring的环境已经配置成功。

第一个Spring程序HelloWorld

  • 创建 Java 项目HelloSpring
  • 添加必要的库。在你的项目名称 HelloSpring 上单击右键,然后在快捷菜单上按照下面可用的选项:Build Path -> Configure Build Path 显示 Java 构建路径窗口,如下所示:
    在 Libraries 标签中使用可用的 Add External JARs 按钮,添加从 Spring 框架和通用日志安装目录下面的核心 JAR 文件:
  • commons-logging-1.2
  • spring-aop-4.1.6.RELEASE
  • spring-aspects-4.1.6.RELEASE
  • spring-beans-4.1.6.RELEASE
  • spring-context-4.1.6.RELEASE
  • spring-context-support-4.1.6.RELEASE
  • spring-core-4.1.6.RELEASE
  • spring-expression-4.1.6.RELEASE
  • spring-instrument-4.1.6.RELEASE
  • spring-instrument-tomcat-4.1.6.RELEASE
  • spring-jdbc-4.1.6.RELEASE
  • spring-jms-4.1.6.RELEASE
  • spring-messaging-4.1.6.RELEASE
  • spring-orm-4.1.6.RELEASE
  • spring-oxm-4.1.6.RELEASE
  • spring-test-4.1.6.RELEASE
  • spring-tx-4.1.6.RELEASE
  • spring-web-4.1.6.RELEASE
  • spring-webmvc-4.1.6.RELEASE
  • spring-webmvc-portlet-4.1.6.RELEASE
  • spring-websocket-4.1.6.RELEASE
  • 创建源文件
    我们在HelloSpring项目下创建实际的源文件。首先,创建一个名为 com.tutorialspoint 的包。在包 com.tutorialspoint 下创建 HelloWorld.java 和 MainApp.java 文件。
    HelloWorld 源文件:
package com.tutorialspoint;

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

MainApp 源文件:

package com.tutorialspoint;

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

public class MainApp {
	public static void main(String[] args) {
	      ApplicationContext context = 
	             new ClassPathXmlApplicationContext("Beans.xml");
	      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
	      obj.getMessage();
	   }
}
  • 创建 bean 的配置文件
    创建一个Bean的配置文件,该文件是一个 XML 文件,需要在 src 目录下创建,如下图所示:
    设置文件名称为 Beans.xml,并保证这个已经配置在 CLASSPATH 环境变量之中。
    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.tutorialspoint.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>
  • 运行程序
    运行程序成功之后,你将会看到在控制台输出一下信息。