Spring学习之路-(1)Spring简介以及HelloWorld

1.1 Spring简介及优点

    简介: Spring提取实际开发中需要重复解决的步骤,因此他完成大量开发中的通用步骤,留给开发者的仅仅是与特定应用相关的部分,从而大大提高开发效率。

    Spring为开发提供一个轻量级的解决方案。解决方案包括1.基于依赖注入的核心机制、2.基于AOP的声明式事务管理、3.与多种持久层技术的整合、4.优秀的的Web MVC框架等。Spring致力于Java EE应用各层的解决方案,是企业应用开发的“一站式”选择,贯穿表现层、业务层、持久层。

    优点: 1. 低侵入式设计,代码的污染极低。

               2. 独立于各种应用服务器,基于Spring框架的应用,可以实现Write Once,RunAnywhere的目的。

               3. Spring的Ioc容器降低了业务对象替换的复杂性,提高了组件之间的解耦。

               4. Spring的Aop支持允许将一些通用任务如安全监测、日志等进行集中式处理,提高代码的复用。

               5. Spring的ORM和DAO提供了与第三方持久层框架(例:mybatis)的良好的整合,并简化了底层的数据库访问。

               6. Spring高度的开放性,并不强制应用完全依赖于Spring,开发者可以自由秀选择Spring框架的部分或全部。

1.2 Spring下载和安转

    本人用到的版本是Spring4.2.0,下载和安装Spring框架的可按照一下步骤进行。

    1. 登录http://repo.springsource.org/libs-release-local/org/springframework/spring可选择下载的对应版本,选择对应的版本,下载对应的压缩包(spring-framework-                       4.2.0.RELEASE-dist.zip)

    2.下载完成后是一个spring-framework-4.2.0.RELEASE-dist.zip  压缩包,解压后是spring-framework-4.2.0.RELEASE文件夹,问价夹下的几个子文件夹分别为:

        (1) doc: 该文件夹存放Spring的相关文档,包含开发指南。API文档。

        (2) libs: 包含三部分的jar包:a.Spring框架calss文件的jar包; b.Spring框架的源文件压缩包,文件已-source结尾; c.Spring框架的API文档的压缩包,文件以-javadoc结尾.。

        (3)schema: 包含Spring各种配置文件的XMLSchema文档。

        (4)其他说明性文档。

   3.下载commons-logging(Spring核心容器必须依赖的日志包) http://commons.apache.org/proper/commons-logging/download_logging.cgi 选择commons-logging-1.2 

1.3 HelloWorld

       注:  在eclipse上安装springsource-tool-suite Spring IDE开发效率提高

    1.新建一个动态web工程,新建lib文件夹,导入jar包

        commons-logging-1.2.jar
        spring-beans-4.2.0.RELEASE.jar
        spring-context-4.2.0.RELEASE.jar
        spring-core-4.2.0.RELEASE.jar
        spring-expression-4.2.0.RELEASE.jar

    2.新建一个Helloworld类,定义一个私有的变量并提供set方法,提供一个公共访问方法

package com.ants.spring.beans;

public class HelloWorld {
    private String name;

    public HelloWorld() {
        System.out.println("HelloWord类被实例化,对象被创建");
    }

    public void setName(String name) {
        this.name = name;
    }

    public void say() {
        System.out.println("hello," + name);
    }
}

 

    3.如果安装Spring IDE插件,在new->other->spring->spring bean configuration file 新建applicationContext.xml配置文件

        如果eclipse没有安装Spring插件,可点击Spring IDE安装链接安装插件;还可以从文档中把开头部分粘贴过来 xxx/spring-framework-4.2.0.RELEASE/docs/spring-framework-reference/html/beans.html

<?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.ants.spring.beans.HelloWorld">
        <property name="name" value="world"></property>
    </bean>
</beans>

    4.编写测试类Test

package com.ants.spring.beans;

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

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
        helloWorld.say();
        System.out.println(helloWorld);
    }
}

    5.运行程序,控制台打印出以下内容,HelloWorld创建成功

HelloWord类被实例化,对象被创建
hello,world com.ants.spring.beans.HelloWorld@b1c705b

 

posted @ 2017-11-05 00:16  i孤独行者  阅读(210)  评论(0)    收藏  举报