Spring 的环境配置以及HelloWorld

版权声明:博客知识产权来源命运的信徒,切勿侵权 https://blog.csdn.net/qq_37591637/article/details/85095665

API下载地址:http://spring.io/projects/spring-framework


安装 SPRING TOOL SUITE

SPRING TOOL SUITE 是一个 Eclipse 插件,利用该插件可以更方便的在 Eclipse 平台上开发基于 Spring 的应用。
安装方法说明(springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip):
Help --> Install New Software...
Click Add...
In dialog Add Site dialog, click Archive...
Navigate to springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip  and click  Open
Clicking OK in the Add Site dialog will bring you back to the dialog 'Install'
Select the xxx/Spring IDE that has appeared
Click Next  and then Finish
Approve the license
Restart eclipse when that is asked

然后关闭elipse,再打开,在preference里面有Spring就代表着安装成功

我的是myelipse,我在安装的过程中,提示,本身就已经安装了!


jar包


测试类

package cn.com.day01;

public class HelloWorld {
	private String name;

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

	public void gethello() {
		System.out.println("你的姓名:" + name);
	}
	public HelloWorld() {
		// TODO Auto-generated constructor stub
		System.out.println("helloworld's construstor....");
	}
}
package cn.com.day01;

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

public class TestHello {
	public static void main(String[] args) {
		/*
		 * //1.新建对象
		 *  HelloWorld hh=new HelloWorld(); 
		 * //2.
		 * 赋值 hh.setName("田江南");
		 */
		// 用Spring的话,只需要这样做就可以了
		// 1.创建spring的IOC容器
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		// 2.从容器中获取bean
		HelloWorld hh = (HelloWorld) ctx.getBean("helloWorld");
		// 3.调用方法
		hh.gethello();
	}
}

Spring的配置文件

src下面新建一个Spring bean configuration的xml文件 重命名为applicationContext.xml

对于myelipse的就不一样了,

右键项目-Myelispe-add Spring Capabilities

勾选和jar包一致的版本

 

成功之后,项目就变成这样了

 文件里面配置情况如下

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- class就是Spring要替换的对象的全路径,id就是类名(第一个字母小写) -->
<bean id="helloWorld" class="cn.com.day01.HelloWorld">
<!--name是setName的set+名称,这个名称的第一个字母小写  -->
<property name="name" value="Spring"></property>
</bean>

</beans>

如果把测试类的调用方法这一行代码去掉的话、

package cn.com.day01;

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

public class TestHello {
	public static void main(String[] args) {
		/*
		 * //1.新建对象
		 *  HelloWorld hh=new HelloWorld(); 
		 * //2.
		 * 赋值 hh.setName("田江南");
		 */
		// 用Spring的话,只需要这样做就可以了
		// 1.创建spring的IOC容器
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		// 2.从容器中获取bean
		HelloWorld hh = (HelloWorld) ctx.getBean("helloWorld");
		// 3.调用方法
		/*hh.gethello();*/
	}
}

程序首先会创建IOC容器,在配置文件里面,先指向类,然后调用setName方法赋值,

结果如下


总结:

1.搭配环境

1.1 是elipse就安装Spring插件,myelipse一般都是自带的

1.2 引入jar包4个Spring jar包,一个日志依赖包

1.3配置文件

2.一个demo例子,了解Spring的作用

猜你喜欢

转载自blog.csdn.net/qq_37591637/article/details/85095665