Spring的基本概念和MVC环境搭建

1、 Spring介绍

打开Spring 官网查看对 Spring 的介绍和目标 http://www.springsource.org/about

We believe that:

·  J2EE should be easier to use

·  It is best to program to interfaces, rather than classes. Spring reduces the complexity cost of

 using interfaces to zero.

·  JavaBeans offer a great way of configuring applications.

·  OO design is more important than any implementation technology, such as J2EE.

·  Checked exceptions are overused in Java. A platform shouldn't force you to catch exceptions you're 

unlikely to be able to recover from.

·  Testability is essential, and a platform such as Spring should help make your code easier to test.

Our philosophy is summarized in  Expert One-on-One J2EE Design and Development  by Rod Johnson.

We aim that:

·  Spring should be a pleasure to use

·  Your application code should  not  depend on Spring APIs

·  Spring should not compete with good existing solutions, but should foster integration.(For example, JDO, 

Toplink, and Hibernate are great O/R mapping solutions. We don't need to develop another one.)

 

首先Spring 的目的是为了让 Java 的企业开发变得更简单容易,提倡面向接口编程,依赖注入,面向对象,使用 POJO 编程,使单元测试更加简单。 Spring 的目标不是具体的解决方案,而是一个平台,整合现有的很多开源框架。

 

2、 使用Spring 的好处

解低组件间的偶合度

面向接口编程

使用Spring 提供的其它功能,如事务管理、 JMS MVC ,数据持久服务等

提供单例模式魂魄

提供AOP 支持

提供众多辅助类

提供跟其它主流框架集合支持

 

3、 依赖注入(Dependency Injection)

在运行期由外部容器动态的将依赖对象注入到组件中,也就是一个对象对另一个对象的引用,不在自己内部实现,而是交给第三方实现完后再注入到该对象中。

 

4、 重量级与轻量级概念

框架默认提供的服务多,可以理解为重理级的,提供的服务单一而简单为轻量级的

 

5、 搭建基于Maven Spring3.0 的环境

5.1 新建 Maven Project

选择File -> New -> Other ,在 New 窗口中选择  Maven -> Maven Project 。点击 next ,如下图:

 

Spring的基本概念和MVC环境搭建

 

5.2 选择默认工作空间,点击 next ,选择类目类型为 maven-archetype-webapp

 

Spring的基本概念和MVC环境搭建

 

5.3 点击 next ,填写相应的 Group ID 和  Artifact ID ,点 next ,建立好的项目结构如下:

 

Spring的基本概念和MVC环境搭建

 

把项目的JDK 环境改成 1.6 ,如果在此步骤出现红X ,则跳到 4.6 java 版本也改成 1.6

 

 

5.4 添加 Source 文件夹

添加 src/main/java src/test/java src/test/resources三个文件夹 ( Source Folder )

 

5.5 更新 class 路径

右键项目属性,Java Build Path -> Source ,下面有四个文件夹: src/main/java src/main/resources src/test/java src/test/resources

 

Spring的基本概念和MVC环境搭建

 

勾上 Allow output folders for source folders 双击每个文件夹的Output folder ,选择路径 分别为:

src/main/java src/main/resources target/classes

Src/test/java src/test/resources target/test-class

 

5.6 把项目变成 Dynamic Web 项目

 

Spring的基本概念和MVC环境搭建

Spring的基本概念和MVC环境搭建

 

 

5.7、 设置部署程序集 Web Deployment Assembly 如下:

 

Spring的基本概念和MVC环境搭建

 

6、 配置pom.xml 文件,加入 spring 依赖

<repositories>

   <repository>

      <id>com.springsource.repository.maven.release</id>

      <url>http://maven.springframework.org/release/</url>

      <snapshots><enabled>false</enabled></snapshots>

   </repository>

</repositories>

 

7、 创建spring 的配置文件

src/main/resources 文件夹中创建文件 applicationContext.xml ,从 spring 文档中找到模板内容添加进去,如下:

<?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="..." class="...">

    <!-- collaborators and configuration for this bean go here -->

  </bean>

  <bean id="..." class="...">

    <!-- collaborators and configuration for this bean go here -->

  </bean>

  <!-- more bean definitions go here -->

</beans>

 

 

8、 创建测试代码

8.1 src/main/java 中创建接口 com.chenzehe.spring.service.HelloWorld ,声明 sayHelloWorld() 方法,如下:

package com.chenzehe.spring.service;

public interface HelloWorld {

     void sayHelloWorld();

}

8.2 创建上面接口的实现类 com.chenzehe.spring.service.impl.HelloWorldImpl ,如下:

package com.chenzehe.spring.service.impl;

import com.chenzehe.spring.service.HelloWorld;

public class HelloWorldImpl implements HelloWorld {

     public void sayHelloWorld(){

         System.out.println("Hello World!");

     }

}

8.3 spring 配置文件 applicationContext.xml 文件中注册该 bean ,如:

<?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.chenzehe.spring.service.impl.HelloWorldImpl"></bean>

</beans>

8.4 、使用 junit 测试

pom.xml 文件中把 junit 依赖改到 4.8.1 版本,在 src/main/test 中创建测试类 com.chenzehe.spring.test.junit.HelloWorldTest ,如下:

package com.chenzehe.spring.test.junit;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.chenzehe.spring.service.HelloWorld;

public class HelloWorldTest {

     @Test

    public void instanceApplicationContext() {

       ApplicationContext cxt = new ClassPathXmlApplicationContext( "applicationContext.xml");

       HelloWorld helloWorld = (HelloWorld) cxt.getBean("helloWorld");

       helloWorld.sayHelloWorld();

    }

}

运行该测试类输出HelloWorld