Spring框架学习笔记--HelloWorld(二)

1. Eclipse安装Spring插件


1.1 Spring插件下载(Spring tool suite)

        Spring tool suite 是一个 Eclipse 插件,利用该插件可以更方便的在 Eclipse 平台上开发基于 Spring 的应用。

        插件下载的官网地址:http://spring.io/tools/sts/all

        在官网中找到Eclipse对应的版本插件,安装有两种方式:在线安装和本地安装。本文通过下载插件到本地,再进行本地安装。

        下载位置如下图:



1.2 插件安装

        安装方法说明:

        1) 启动Eclipse

        2) Help --> Install New Software...


        3) 点击  “Add...”,在弹出的对话框中,点击 “Archive...”,选择下载的Spring插件的压缩文件,然后点击 “OK”。


        4) 然后在 “Name” 下方只选择带有 “IDE” 的选项,去掉下方联网更新的选项,以防拖慢安装速度。然后点击 “Next>”。


        5) 然后直接点击 “Next>”。


        6) 然后再直接点击 “Next>”。



        7) 同意条款,最后点击 “Finish” 完成安装。



        8) 完成后,重新启动 Eclipse,在 “Welcome”  欢迎页中出现 “Spring IDE”,并且属性页中也出现 “Spring” 选项,表示插件安装成功。



2. 搭建 Spring 开发环境


2.1 jar 包准备

        Spring 的 jar 包可从官网下载,下载方式参考 http://blog.csdn.net/llh0804/article/details/73991744

        在下载好的 jar 包文件中,需要用到的有以下4个:

  • spring-beans-4.xx.RELEASE.jar
  • spring-context-4.xx.RELEASE.jar
  • spring-core-4.xx.RELEASE.jar
  • spring-expression-4.xx.RELEASE.jar

        还有一个是 Spring 必须依赖的日志包:commons-logging-1.xx.jar;这个包需要另外下载。

        本文中使用到的 jar 包的版本是:Spring 提供的 jar 文件的版本为4.3.9,日志包的 jar 版本为1.1.3。


2.2. jar 包加入到工程

        首先新建一个 Java 工程,然后在根目录下新建一个 lib 文件夹,将上面准备好的 jar 包拷贝到 lib 文件夹下,工程目录如下:

        然后将 lib 文件夹下的 jar 文件加入到 Path 下,选择所有 jar 文件,然后右键选择 Build Path 下的选项 Add to Build Path,如下图完成添加:


        Spring 的环境就此完成,只需要5个必要的 jar 包就可以了。


3. 工程 Hello World


3.1 普通的 Java 工程

        首先新建一个类:HelloWorld.java,完成如下:

package com.xtw.spring;

public class HelloWorld {

	private String name;

	public void setName(String name) {
		this.name = name;
	}
	
	public void hello() {
		System.out.println("hello: " + name);
	}
}

        然后再新建一个类:Main.java,在这个类中对 HelloWorld 类进行操作,完成如下:
package com.xtw.spring;

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

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		//创建HelloWorld实例
		HelloWorld helloWorld = new HelloWorld();	
		//给name属性赋值
		helloWorld.setName("Spring");				
		//调用hello方法
		helloWorld.hello();							
	}
}

        运行 main 方法,打印如下:

hello: Spring

        以上与 Spring 毫无关系,只是一个普通的 Java 程序的运行过程。但我们要完成的 Spring 的工程执行结果与上保持一致。


3.2 Spring 的实现

        在上面的工程中,Main.java 类中对 HelloWorld 的操作:创建对象和赋值,这个部分可以交给 Spring 完成,hello 方法的调用不变,这样的改进就是 Spring 的实现。下面将详细描述其过程。

        1. 首先在 src 目录下创建一个 Spring 的配置文件:在 src 目录的新建菜单下找到 Spring Bean Configuration File 选项;如果没有该选项,则选择 Other 选项,在弹出的对话框中找到 Spring,其下内容中找到该配置文件。



        点击 “Next>” 按钮,命名为:applicationContext.xml,然后 “Next>” ,可选择需要的命名空间,这里是 Spring 插件所提供的,不安装则没有这些选项,然后点击 “Finish” 完成配置文件的创建。


        2. 完成创建,就可以对 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.xsd">

	<!-- 配置bean -->
	<bean id="helloWorld" class="com.xtw.spring.HelloWorld">
		<property name="name" value="Spring"></property>
	</bean>
</beans>

        在上面配置中,bean 的标签就是配置的内容。

  • class:就是 bean 的全类名,该例中即为 HelloWorld;是用反射的方式由 Spring 创建对象
  • id:对象标识,建议为类名小写
  • name:bean 的属性标签 prooperty 下的属性,对应HelloWorld类中的 set 方法
  • value:bean 的属性标签 prooperty 下的属性,给 name 属性赋值


        3. 在 Main 类中,注释掉 HelloWorld 对象的创建及其属性的赋值两条语句,加入 Spring 的实现过程,先通过配置文件创建 IOC 容器对象,然后在 IOC 容器中根据 Bean 的 id 获取实例。完成如下:

package com.xtw.spring;

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

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

//		//创建HelloWorld实例
//		HelloWorld helloWorld = new HelloWorld();	
//		//给name属性赋值
//		helloWorld.setName("Spring");				
		
		//创建 Spring 的 IOC 容器对象
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//从 IOC 容器中获取 Bean 实例
		HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("helloWorld");
		
		//调用hello方法
		helloWorld.hello();							
	}
}

        以上是通过 Spring 创建对象的简单实现

  • ApplicationContext:代表 Spring 中的 IOC 容器,是一个接口
  • ClassPathXmlApplicationContext:表示配置文件在类路径下,是 ApplicationContext 的一个实现类,传入的参数即为配置文件的名称
  • getBean:该方法有众多的重载,此处使用的是传入配置文件中 bean 的 id 值


        再次运行 main 方法,打印如下:

七月 10, 2017 3:59:59 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1f17ae12: startup date [Mon Jul 10 15:59:59 CST 2017]; root of context hierarchy
七月 10, 2017 3:59:59 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
hello: Spring

        最后一行即为打印结果,与普通创建对象的方法所打印的结果一致。其余打印内容是 Spring 的日志信息。

        这就是 Spring 的一个简单实现,通过对比可以看出,一般的方法中,对象的创建是在需要的地方主动 new 出该对象,而 Spring 的对象创建过程是通过 IOC 容器创建,需要用到对象的时候,只需要从 IOC 容器中获取就可以了,后面篇章会对 IOC 容器做详细描述。


3.3  Spring 的 IOC 容器对象的生成

        上面已经简单的实现了 Spring 的应用,那么在获取 Bean 对象前,Spring 都做了些什么?

        首先把 Main 类中 geiBean 方法和 hello 方法的调用两句注释掉,如下所示:

		//创建 Spring 的 IOC 容器对象
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//从 IOC 容器中获取 Bean 实例
		//HelloWorld helloWorld = (HelloWorld)applicationContext.getBean("helloWorld");
		
		//调用hello方法
		//helloWorld.hello();		

        然后在 HelloWorld 类中创建无参的构造函数并打印语句,在 setName 方法中打印语句,完成如下:

package com.xtw.spring;

public class HelloWorld {

	private String name;

	public void setName(String name) {
		System.out.println("setName: " + name);
		this.name = name;
	}
	
	public void hello() {
		System.out.println("hello: " + name);
	}
	
	public HelloWorld() {
		// TODO Auto-generated constructor stub
		System.out.println("HelloWorld's constructor");
	}
}

        运行程序,效果如下:

七月 10, 2017 4:44:29 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1f17ae12: startup date [Mon Jul 10 16:44:29 CST 2017]; root of context hierarchy
七月 10, 2017 4:44:29 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
HelloWorld's constructor
setName: Spring

        在只创建 IOC 容器的情况下,分别执行了 HelloWorld 中的 无参构造器和 set 方法;说明 Spring 会对配置文件中的 bean 进行初始化,同时会调用 set 方法,对属性进行赋值。所以再通过 getBean 方法在 IOC 容器中就可以直接获取创建好的对象了。


4. 小结

        该篇内容主要通过一个简单的工程,介绍了 Spring 工程的环境、配置及实现。

        Spring 的环境搭建很简单,只需要导入必须的5个 jar 包,就可以了。

        Spring 的配置,主要是配置 bean,该篇中只介绍了一种 bean 的配置,是通过 property 属性标签完成的,还有很多配置方式,后面会涉及。

        Spring的实现过程中,主要说明了 ApplicationContext 接口及其实现类 ClassPathXmlApplicationContext ,用于创建 Spring 的 IOC 容器对象,然后再通过 getBean 从 IOC 容器中获取对象,拿到对象后就可以为所欲为了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值