了解Spring的朋友都知道,创建一个Spring Framework项目都需要依赖什么样的Jar包。如果不使用Maven,那么在项目中就需要手动下载相关的依赖。由于Spring Framework又会依赖与其他开源类库,因此实际中往往会下载Spring Framework的jar包,还的下载所有它依赖的其他jar包。这么做往往就引入了很多不必要的依赖。另一种做法是只下载Spring Framework的jar包,不包含其他的相关依赖,到实际使用的时候,再根据报错信息,或者查询相关文档,加入需要的其他依赖。

1. 创建Maven项目
如果对Maven不太了解的可以参考:[Maven实战](4)eclipse创建Maven项目
使用IDE创建一个Maven项目非常简单,选择菜单项File->New->Other,在弹出的对话框中选择Maven下的Maven Project,然后点击Next按钮:

在弹出的New Maven Project对话框中,使用默认的选项,然后点击Next按钮:
此时m2eclipse会提示我们选择一个Archetype。这里选择maven-archetype-quickstart,再点击Next按钮:
此时输入groupId,artifactId,version,package:
点击Finish即可完成。
2. 添加依赖配置pom.xml
2.1 Spring2.xxx
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
 
<groupId>com.sjf.springdemo</groupId>
<artifactId>springdemo-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
 
<name>springdemo-helloworld</name>
<url>http://maven.apache.org</url>
 
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
 
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
</dependencies>
</project>

2.2 Spring3.xxx
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
 
<groupId>com.sjf.springdemo</groupId>
<artifactId>springdemo-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
 
<name>springdemo-helloworld</name>
<url>http://maven.apache.org</url>
 
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
 
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
</project>

3. 编译查看Maven是否创建成功
运行mvn clean compile命令:
Run:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building springdemo-helloworld 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ springdemo-helloworld ---
[INFO] Deleting D:\WorkSpace\EclipseCode\springdemo-helloworld\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ springdemo-helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\WorkSpace\EclipseCode\springdemo-helloworld\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ springdemo-helloworld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\WorkSpace\EclipseCode\springdemo-helloworld\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.292 s
[INFO] Finished at: 2016-01-30T15:24:11+08:00
[INFO] Final Memory: 9M/23M
[INFO] ------------------------------------------------------------------------
BUILD SUCCESS则表示创建Maven成功。
4. Spring bean主代码
在src/main/java/com/sjf/bean/下创建HellloWorld类,Spring的bean仅仅是一个普通的Java类,稍后在Spring bean配置文件中声明。
package com.sjf.bean;
 
/**
* hello world 类
* @author sjf0115
*
*/
public class HelloWorld {
private String name;
 
public void setName(String name) {
this.name = name;
}
public void sayHello(){
System.out.println("welcome "+ name +" to spring world...");
}
}
5. Spring bean配置文件
在src/main/resources创建applicationContext.xml文件,这是的Spring bean的配置文件,该文件告知所有可用的Spring bean。
applicationContext.xml:
<?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.sjf.bean.HelloWorld">
<property name="name" value="sjf0115"></property>
</bean>
 
</beans>
6.项目结构
Spring2.xxx:
Spring3.xxx:
7.运行
src/main/java下创建Test.java:
package com.sjf.bean;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 
/**
* 测试类
* @author sjf0115
*
*/
public class Test {
 
private static ApplicationContext context;
private static HelloWorld helloWorld;
public static void main(String[] args) {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 从IOC容器中获取Bean实例
helloWorld = (HelloWorld)context.getBean("helloworld");
// 3.调用sayHello方法
helloWorld.sayHello();
}
}
运行结果:
一月 30, 2016 3:53:19 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@164b9b6: display name [org.springframework.context.support.ClassPathXmlApplicationContext@164b9b6]; startup date [Sat Jan 30 15:53:19 CST 2016]; root of context hierarchy
一月 30, 2016 3:53:19 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
一月 30, 2016 3:53:19 下午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@164b9b6]: org.springframework.beans.factory.support.DefaultListableBeanFactory@18b6281
一月 30, 2016 3:53:19 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18b6281: defining beans [helloworld]; root of factory hierarchy
welcome sjf0115 to spring world...

[Spring实战系列](2)Maven创建Spring-HelloWorld项目相关推荐

  1. [Spring实战系列](5)Spring应用上下文

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SunnyYoona/article/details/50618337 下面是Spring-Hello ...

  2. [Spring实战系列](8)Spring注入方式之setter注入

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SunnyYoona/article/details/50631178 通常,JavaBean 的属性 ...

  3. 用maven创建Spring MVC项目

    用maven创建Spring MVC项目 mvn archetype:generate -DgroupId=fry-arthur -DartifactId=spring-mvc-study -Darc ...

  4. Java工作笔记-使用Maven创建Spring Boot并生成war包外部tocamt运行

    目录 基本概念 代码与实例 基本概念 目前发现很多Java项目都是直接使用Maven作项目管理,在Maven中配置依赖,比如Spring boot完成操作,在经历了这么多操作后,还发现大家喜欢把Mav ...

  5. [Spring实战系列](6)配置Spring IOC容器的Bean

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SunnyYoona/article/details/50619900 1. 简介 Spring提供了 ...

  6. Spring实战笔记——(1)Spring之旅(上)

    Spring实战笔记--(1)Spring之旅 文章目录 Spring实战笔记--(1)Spring之旅 1.1简化Java开发 1.1.1 激发POJO的潜能 1.1.2依赖注入 依赖注入的实现 构 ...

  7. Skype For Business 2015实战系列14:创建Office Web App服务器场

    Skype For Business 2015实战系列14:创建Office Web App服务器场 前面的操作中我们已经成功的安装了Office Web App Server,今天我们将创建Offi ...

  8. 【Youtobe trydjango】Django2.2教程和React实战系列四【创建Django应用】

    [Youtobe trydjango]Django2.2教程和React实战系列四[创建Django应用] 1. 创建应用 2. 修改应用 1. 创建应用 打开cmd黑框,也可以用下列方法打开项目根目 ...

  9. Maven3路程(六)用Maven创建Spring3 MVC项目

    Maven3路程(六)用Maven创建Spring3 MVC项目 一.      环境 spring-framework-3.2.4.RELEASE jdk1.7.0_11 Maven3.0.5 ec ...

  10. idea maven创建java项目_新版本IntelliJ IDEA 构建maven,并用Maven创建一个web项目(图文教程)...

    之前都没试过用maven来管理过项目,但是手动找包导包确实不方便,于是今天用2016版的IDEA进行了maven的初尝试. 打开IDEA,创建新项目: 然后选择Maven,以及选择自己电脑的jdk: ...

最新文章

  1. SQL时间相关 - SQL日期,时间比较
  2. arm-buildroot-linux-,buildroot构建交叉编译工具链,根文件系统
  3. Java基础学习总结(14)——Java对象的序列化和反序列化
  4. css3 :nth-child()选择器的使用
  5. Keil5 编译生成bin二进制文件的设置方法
  6. C++设计模式-Builder建造者模式
  7. mysql 存储过程 sql变量_SQL基础-变量 存储过程和函数
  8. 深入理解定时器系列第二篇——被誉为神器的requestAnimationFrame
  9. OnScrollListener
  10. 签入代码(新建分支,新建推拉请求)关联工作项,却找不到自己需要的工作项...
  11. IDEA设置Maven组合命令一键编译打包发布
  12. sqlmap使用教程大全命令大全(图文)
  13. java进度条代码,java进度条代码该怎么编写?
  14. RPA机器人如何调用USB SERVER
  15. 机器学习 | 交叉验证
  16. C#实现百度翻译API调用
  17. 【Axure教程】中继器手风琴
  18. 迁移学习,DomainAdaption 笔记
  19. JavaWeb核心技术系列教程(23)——JSP标签
  20. 【数据结构与算法学习笔记005】多传感器信息融合

热门文章

  1. HTML常见标签及个人简历制作
  2. iOS捷径(Workflow 2.0)拓展
  3. AirPlay、AirTunes 移植开发
  4. python通信工程定额_版通信工程费用及定额套用解读
  5. IE8卸载再回到IE6
  6. 如何深入Delphi编程
  7. 奔图3305_奔图Pantum P3305DN打印机驱动官方版
  8. PHP第一季视频教程.李炎恢.学习笔记(三)(第2章 基本语法(2))
  9. php ip 库,php IP获取城市API(纯真IP数据库)
  10. linux num_fds文件描述符数量限制 too many openfiles 错误