J2ee项目从0搭建(五):最新Spring下载(BOM解决jar包依赖)、集成
来源:互联网 发布:php 获取头部信息 编辑:程序博客网 时间:2024/05/18 17:01
一、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>
二、添加必要的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>
- spring示例:我们使用spring官方给出的示例来展示spring的集成和作用http://projects.spring.io/spring-framework/;在github上的项目路径为com.spring.demo.hello下;在Application.java中,右击Run As->Java Application,能成功在控制台输出“Hello World!”
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中配置beanMessageService 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中配置一些具体信息或者继承其它需要的组件,如链接配置链接数据库等。我们在后门的配置中逐步完善。
为了便于大家学习,项目将被开源在我的github上:
项目地址:https://github.com/gubaijin/buildmavenweb
如何将项目开源道github,请看我的另一篇博文:GitHub上创建项目 并初始化本地工程提交到GitHub上
0 0
- J2ee项目从0搭建(五):最新Spring下载(BOM解决jar包依赖)、集成
- 解决jar包依赖:Spring IO platform推出bom
- 解决jar包依赖:Spring IO platform推出bom
- 解决jar包依赖:Spring IO platform推出bom
- J2ee项目从0搭建(六):Spring MVC集成
- J2EE项目Struts2、Hibernate、Spring框架相关jar包下载
- spring maven项目解决依赖jar包版本冲突方案
- spring-dependencies依赖项目jar下载地址
- spring依赖jar包
- spring jar包依赖
- Spring所依赖的AspectJ的jar包下载/Spring-aop注解jar包下载
- 从Maven项目中导出项目依赖的jar包
- Spring框架jar包的最新下载方法
- 在最新官网下载spring 的jar包
- J2ee项目从0搭建(十):在项目中集成log日志收集(slf4j+log4j)
- IDEA maven 下载依赖jar包失败的解决思路
- 怎样下载Spring所依赖的AspectJ的jar包
- Maven下载项目依赖jar包和使用方法
- iOS多线程学习笔记
- smartUpload组件的简单总结
- 237. Delete Node in a Linked List
- Kafka学习整理七(producer和consumer编程实践)
- linux系统c线程池的实现
- J2ee项目从0搭建(五):最新Spring下载(BOM解决jar包依赖)、集成
- matplotlib核心剖析
- 深度学习——Xavier初始化方法
- 强悍的 ubuntu —— ubuntu 下的文件搜索
- 根据经纬坐标计算距离
- Android安全攻防战,反编译与混淆技术完全解析
- "Could not open '/sys/class/power_supply/genymotion_fake_path/present'
- Struts2 深入理解
- iOS Cocoapods APP 使用教程