Spring framework

Table of Contents

1 Spring简介

The Spring Framework is an application framework and inversion of control container(IoC) for the Java platform. It help to decouple your project components' dependencies.

It has become popular as an alternative to, replacement for, or even addition to the Enterprise JavaBeans (EJB) model.

Spring是轻量级的,它的一个优点是可以接受POJO (Plain Old Java Object)作为bean,不强制用户遵循笨重的EJB规范。

参考:
Spring Framework: http://projects.spring.io/spring-framework/
Spring Tutorial: http://www.tutorialspoint.com/spring/index.htm
Spring in Action: http://book.douban.com/subject/5283241/
Spring实现用户管理类的入门例子:http://blog.csdn.net/jnqqls/article/details/8622020

1.1 Spring和工厂模式

有人说:反转控制容器(如Spring框架)是最好的工厂模式。各个类中不需要生成其他类的实例,完全由XML配置文件指定。Spring的反转控制容器为你生成对象,把对象的引用塞给Spring管理的类中。这样,类之间就完全解耦了。
但也有人说:虽然Spring美其名曰解藕了,但实际上是把原来工厂模式中的耦合性转移到了XML文件中。

1.2 环境配置

Spring framework依赖于org.apache.commons.logging package,可从这里下载:http://commons.apache.org/proper/commons-logging/
Spring framework的库文件可以从这里下载:http://repo.spring.io/release/org/springframework/spring/

下载上面的库文件后,把所有jar包配置到环境变量CLASSPATH中即可。

参考:http://www.tutorialspoint.com/spring/spring_environment_setup.htm

2 Hello World

下面演示利用Spring写一个简单的Hello World程序,消息"Hello World"将从XML文件中获取。

Following is the content of HelloWorld.java:

package com.xyz;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }

   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

Following is the content of MainApp.java:

package com.xyz;

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("MyFirstBean");
      obj.getMessage();
   }
}

Following is the content of 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="MyFirstBean" class="com.xyz.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

编译运行如下:

$ find .
.
./Beans.xml
./com
./com/xyz
./com/xyz/HelloWorld.java
./com/xyz/MainApp.java
$ export CLASSPATH=.:~/Downloads/spring-framework-4.1.0.RELEASE/libs/*:~/Downloads/commons-logging-1.2/commons-logging-1.2.jar
$ javac com/xyz/MainApp.java
$ java com/xyz/MainApp
Nov 14, 2015 9:42:56 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@23ab930d: startup date [Tue Nov 14 21:42:56 CST 2015]; root of context hierarchy
Nov 14, 2015 9:42:56 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Your Message : Hello World!

参考:http://www.tutorialspoint.com/spring/spring_hello_world_example.htm


Author: cig01

Created: <2015-11-14 Sat 00:00>

Last updated: <2016-07-08 Fri 14:15>

Creator: Emacs 25.1.1 (Org mode 9.0.7)