一、Spring Jar包下载:

  • 如果没使用maven进行管理的话,可以直接到spring官网的下载地址中去下载你所需要的jar包:http://repo.spring.io;在页面上点击左侧的Artifacts->右侧Quick->输入“spring-framework”->点击Search;在查询出结果后,可以点击右上角的Page页数或者点击Modified(通过时间来筛选),找到你索要下载的对应版本进行下载。
  • 使用Maven管理jar包,spring提供了Spring IO platform来解决spring不同模块或者外部进行集成时的依赖版本号问题。


有网友给的说明是这样的:Spring起初只专注ioc和aop,现在已发展成一个庞大体系。比如security、mvc等。如此一来,不同模块或者与外部进行集成时,依赖处理就需要各自对应版本号。比如,较新spring与较老的quartz,它们集成就会遇到问题,给搭建和升级带来不便。因此Spring IO Platform应运而生,只要项目中引入了它,外部集成时依赖关系无需版本号。官网的原文如下:“when you do declare a dependency on something that’s part of the Platform, you will now be able to omit the version number.”


Spring IO platform的官方指导给出的使用是非常简单的,只要引入<dependencyManagement>中引入platform-bom包,目前最新版本是2.0.5;引入BOM后,在之后的使用中,spring的相关模块会自动选择最优版本,不用你去关心使用什么版本了。配置完后,我们引入spring-core包,在Maven Dependencies中我们发现,下载的是spring-core-4.2.6版本,自动带了版本号。


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.spring.platform</groupId>
            <artifactId>platform-bom</artifactId>
            <version>2.0.5.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
</dependency>



Java springboot和springclould 依赖包的版本 springboot依赖包下载_eclipse



二、添加必要的spring依赖:

  • 上面我们已经添加了核心依赖jat包:spring-core;spring最多的就是作为容器,所以我们就添加了容器包:spring-context;添加完后发现不仅spring-context包有了,还下了另外四个包aop、aopalliance、beans、expression,看来Spring IO platform还是很强大的,把依赖都自动添加了;不仅如此,我们发现下面的junit包的version下面有黄色警告,查看发现原来Spring IO platform(platform-bom)推荐我们使用的最好junit版本是4.12,我们直接把<version>3.8.1</version>删除后发现,Maven Dependencies中的junit-3.8.1变成了,junit-4.12+hamcrest-core-1.3(junit依赖包)只想说一句,spring还是很强大的~~
  • PS:到了这里可能有的人项目上有个小红叉,我就发生了,这是因为添加了jar包,maven项目的依赖包需要重新更新构建一下。选中项目,右击Maven->Update Project…->点击OK。错误解决。



  • 因为我们是想跟web项目集成,所以还需要添加spring-web包。
<!-- spring-web -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
		</dependency>
  • 我们再添加一个commons-logging包,用来输出日志信息。
<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
		</dependency>

Application.java

package com.spring.demo.hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration//等价 与XML中配置beans
@ComponentScan//配置spring扫描
public class Application {
	@Bean//等价于XML中配置bean
	MessageService mockMessageService() {
		return new MessageService() {
			public String getMessage() {
				return "Hello World!";
			}
		};
	}

	public static void main(String[] args) {
		ApplicationContext context = new AnnotationConfigApplicationContext(Application.class);
		MessagePrinter printer = context.getBean(MessagePrinter.class);
		printer.printMessage();
	}
}




MessagePrinter.java

package com.spring.demo.hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component//标注为控制层组件
public class MessagePrinter {
	final private MessageService service;

    @Autowired//对成员变量、方法和构造函数进行标注,来完成自动装配的工作
    public MessagePrinter(MessageService service) {
        this.service = service;
    }

    public void printMessage() {
        System.out.println(this.service.getMessage());
    }
}




MessageService.java

package com.spring.demo.hello;

public interface MessageService {
	String getMessage();
}

三、web项目配置:web.xml中

  • web项目是放到类似于tomcat这样的容器中启动的,通过URL地址来访问的,就不能使用main来启动了。
  • web项目启动时,会首先加载web.xml,所有我们需要在web.xml中对spring进行配置。
  • 加载spring配置文件,contextConfigLocation在 ContextLoaderListener类中的默认值是 /WEB-INF/applicationContext.xml,我这边写在spring-common.xml(路径就是我们之前添加的源文件src/main/resources下);所以加载路径就要写成classpath*:/spring-*.xml;“*”代表加载classpath下所有spring-开头的xml文件,我比较倾向于配置分开,这样在后面的维护中便于查找。


<!-- 加载配置文件 -->
	<!--contextConfigLocation在 ContextLoaderListener类中的默认值是 /WEB-INF/applicationContext.xml-->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:/spring-*.xml
		</param-value>
	</context-param>



先弄一个空的spring配置文件,根标签以<beans >开始,spring-common.xml:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans></beans>





  • 配置ContextLoaderListener来启动spring(推荐使用)。


<!-- 配置listener启动spring -->
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>



  • 配置好之后,用Servers下配置好的tomcat启动(以后web项目中说的启动默认就是部署到tomcat中启动),通过Console打印出的信息可以看出,加载了spring-common.xml,并且初始化了spring:
  • 启动成功,项目已经可以启动加载spring了,后面就是在spring-common.xml中配置一些具体信息或者继承其它需要的组件,如链接配置链接数据库等。我们在后门的配置中逐步完善。




======================================================================================================================