Spring学习笔记 1. 尚硅谷_佟刚_Spring_HelloWorld

1,准备工作

(1)安装spring插件

        搜索https://spring.io/tools/sts/all就可以下载最新的版本

image

      下载之后不用解压,使用Eclipse进行安装。在菜单栏最右面的Help菜单:

      image

     点击Install New Software之后,有如下界面,按次序点击找到刚才下载的安装包之后确认。

image

       确认之后到下面这个界面之后,注意只需要选择其中的某些选项就可以了。

image

    下面一路安装,之后重启就可以,可以看看是否安装成功,如下表示成功。

    image      

(2)下载jar包。

       spring-framework-4.2.4.RELEASE-dist  下载地址:http://repo.spring.io/release/org/springframework/spring/

       commons-logging-1.1.1  网上自行下载。

2,开始HelloWorld的小程序

(1)新建项目,添加一个lib文件,把刚才下载的包中的下面这些包拷贝进去,全选jar包,右键Build Path ---〉Add to Build Path

      image

(2)新建下面3个文件:HelloWorld.java; Main.java ;applicationContext.xml

      image

HelloWorld.java 代码:

package com.yfy;
public class HelloWorld {
    private String name;    
    public void setName(String name) {
        this.name = name;
    }
    public void hello(){
        System.out.println("hello: " + name);
    }
}

Main.java 代码:

package com.yfy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloWorld =(HelloWorld) applicationContext.getBean("helloWorld");
        helloWorld.hello();
    }
}

applicationContext.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.xsd">
    <!-- 配置bean -->
    <bean id="helloWorld" class="com.yfy.HelloWorld">
        <property name="name" value="spring"></property>
    </bean>
</beans>

运行Main.java

image

下面就详细分析一下:

image

注:以上学习笔记参考: 尚硅谷_佟刚_Spring_HelloWorld第一课,佟刚老师讲的不错,慢慢学习。

posted @ 2016-01-02 12:19  yefengyu  阅读(2397)  评论(0编辑  收藏  举报