spring--hello~!(如何搭建一个spring项目)

1.准备

Java–jdk1.8
详细版本:8u111
百度 spring jar 找到:
spring--hello~!(如何搭建一个spring项目)
点击如下:
spring--hello~!(如何搭建一个spring项目)
传送门 ->点我
spring--hello~!(如何搭建一个spring项目)
这个是我自己用的。
解压如下:
spring--hello~!(如何搭建一个spring项目)
这些jar包是必须的,在spring项目中都需要用到:
spring--hello~!(如何搭建一个spring项目)
spring-core是spring框架的核心工具类,spring其他组件都要用到这个包里的类。
spring-beans:所有应用都要用到的jar包,包含访问配置文件、创建和管理Bean以及进行Ioc或者DI操作相关的类。
spring-context:提供了在基础Ioc功能上的扩展服务,提供企业级服务,如邮件服务、任务调度、JNDI定位、EJB集成、远程访问、缓存。
spring-expression定义了spring表达式语言。

spring项目还需要一个第三方的jar----commons.loggingspring--hello~!(如何搭建一个spring项目)

2.创建工程

spring--hello~!(如何搭建一个spring项目)

3.jar包导入

将上述jar包拷贝到工程下:
spring--hello~!(如何搭建一个spring项目)
选中jar包->右键->bulid path ->add bulid path:
spring--hello~!(如何搭建一个spring项目)

4.增加自己的操作

选中src右键->new ->package:
spring--hello~!(如何搭建一个spring项目)
spring--hello~!(如何搭建一个spring项目)
说明一下:
resource放xml文件
client放测试类(main)
beans放javaBean
spring--hello~!(如何搭建一个spring项目)
spring--hello~!(如何搭建一个spring项目)
spring--hello~!(如何搭建一个spring项目)
spring--hello~!(如何搭建一个spring项目)
spring--hello~!(如何搭建一个spring项目)
会自动生成get,set方法
spring--hello~!(如何搭建一个spring项目)
注意JavaBean的属性最好不要以set或者get开头,小布尔不要以is开头
spring--hello~!(如何搭建一个spring项目)
spring--hello~!(如何搭建一个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.xsd">
	<bean id="people" class="beans.People">
	</bean>
</beans>

spring--hello~!(如何搭建一个spring项目)
注意,这里可以不用写版本:
因为:
spring--hello~!(如何搭建一个spring项目)
spring--hello~!(如何搭建一个spring项目)
在这个文件中指定了默认的配置,所以刚开始可以不用写版本号,直接使用默认的就行:
spring--hello~!(如何搭建一个spring项目)
class的路径对不对,一个小的验证方法,按住ctrl左键点击引号内容,如果可以跳转,说明路径正确。
spring--hello~!(如何搭建一个spring项目)

package client;

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

import beans.People;

public class Main {

	public static void main(String[] args) {

		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"resource/beans.xml");
		People people = (People) applicationContext.getBean("people");
		people.setAge(33);
		people.setName("spring");
		people.setMessage("hello");
		System.out.println("name:"+people.getName());
		System.out.println("age:"+people.getAge());
		System.out.println("message:"+people.getMessage());
	}

}



运行:
spring--hello~!(如何搭建一个spring项目)
注意:运行之前需要关闭.xml文件,否则因为文件占用,会重新创建一个xml文件。
spring--hello~!(如何搭建一个spring项目)
spring--hello~!(如何搭建一个spring项目)
spring--hello~!(如何搭建一个spring项目)
选中main方法:
spring--hello~!(如何搭建一个spring项目)

这个spring hello就完成了。
1.运行文件时,xml文件不能被占用,不能用eclipse打开xml文件
2.spring和jdk有版本对应关系