Spring入门,使用Maven进行管理
一,使用maven创建项目原型
mvn archetype:generate

进入交互模式创建项目原型,根据网速不同,跳出设置选项的时间不定
第一个选项

直接Enter即可,表示使用默认值502,后面的如法炮制,直到设置我们需要的groupId,artifactId,version为止。
我的设置为:
groupId:com.yxl.springdemo
artifactId:springdemo
version:1.0-SNAPSHOT(也就是默认值)
使用tree展示原型目录结构:

二,编辑源代码
首先创建一个bean类,在com/yxl/springdemo目录下新建HelloBean.java。
package com.yxl.springdemo; public class HelloBean
{
private String helloword; public void setHelloword(String helloword)
{
this.helloword = helloword;
}
public String getHelloword()
{
return helloword;
}
}
在com/yxl/springdemo目录下新建SpringDemo.java。
package com.yxl.springdemo; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext; public class SpringDemo
{
public static void main(String[] args)
{
//ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-config.xml");
ApplicationContext ctx = new FileSystemXmlApplicationContext("beans-config.xml");
HelloBean hello = (HelloBean)ctx.getBean("helloBean");
System.out.println(hello.getHelloword());
}
}
重新编辑pom.xml,解决依赖关系,并生成可执行jar包。
<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/4.1.2.RELEASE1/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.yxl.springdemo</groupId>
<artifactId>springdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>springdemo</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.yxl.springdemo.SpringDemo</Main-Class>
<Build-Number>123</Build-Number>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build> </project>
在项目主目录springdemo(不是包目录)下新建beans-config.xml,配置bean属性。
<?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 = "helloBean"
class = "com.yxl.springdemo.HelloBean">
<property name = "helloword">
<value>Hello, World</value>
</property>
</bean>
</beans>
在这里我犯过的一个错误就是xml文件第一行的?与xml之间插入了一个空格,然后运行程序就会抛出异常。
三,编译并运行结果
mvn clean compile
mvn clean package
java -jar target/sprintdemo-1.0-SNAPSHOT.jar
结果:

注意,执行java -jar target/springdemo-1.0-SNAPSHOT.jar时,配置文件必须在当前目录下。比如我是在项目根目录下执行这条语句,所以beans-config.xml也被放置在项目根目录springdemo中,如果在target中执行该包文件,执行时会抛出异常。
Spring入门,使用Maven进行管理的更多相关文章
- http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/ 非常棒的spring入门,maven,以及eclipse
http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-t ...
- Spring入门(四):使用Maven管理Spring项目
让我们先回顾下本系列的前3篇博客: Spring入门(一):创建Spring项目 Spring入门(二):自动化装配bean Spring入门(三):通过JavaConfig装配bean 1.为什么要 ...
- IDEA一步步创建Maven管理的Spring入门程序
目前,做Java开发的很多人都在使用IDEA了,而有些人也选择用Eclipse,我这里介绍一下IDEA一步步创建Maven项目的步骤,并创建一个Spring的入门程序(Java项目,非Web项目),讲 ...
- Spring+Mybatis+MySql+Maven 简单的事务管理案例
利用Maven来管理项目中的JAR包,同时使用Spring在业务处理层进行事务管理.数据库使用MySq,数据处理层使用Spring和Mybatis结合. 本案例代码主要结构如图: 1.数据库脚本 -- ...
- Spring Security 3.2.x与Spring 4.0.x的Maven依赖管理
原文链接: Spring Security with Maven原文日期: 2013年04月24日翻译日期: 2014年06月29日翻译人员: 铁锚 1. 概述 本文通过实例为您介绍怎样使用 Mave ...
- Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制
Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制 201311.27 代码下载 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 前言 ...
- Spring入门5.事务管理机制
Spring入门5.事务管理机制 20131126 代码下载 : 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 回顾之前的知识,Spring 最为核心的两个部分 ...
- 【第十五篇】- Maven 依赖管理之Spring Cloud直播商城 b2b2c电子商务技术总结
Maven 依赖管理 Maven 一个核心的特性就是依赖管理.当我们处理多模块的项目(包含成百上千个模块或者子项目),模块间的依赖关系就变得非常复杂,管理也变得很困难.针对此种情形,Maven 提供了 ...
- 【Spring Framework】Spring入门教程(八)Spring的事务管理
事务是什么? 事务:指单个逻辑操作单元的集合. 在操作数据库时(增删改),如果同时操作多次数据,我们从业务希望,要么全部成功,要么全部失败.这种情况称为事务处理. 例如:A转账给B. 第一步,扣除A君 ...
- Spring入门(十四):Spring MVC控制器的2种测试方法
作为一名研发人员,不管你愿不愿意对自己的代码进行测试,都得承认测试对于研发质量保证的重要性,这也就是为什么每个公司的技术部都需要质量控制部的原因,因为越早的发现代码的bug,成本越低,比如说,Dev环 ...
随机推荐
- python 中for与else搭配使用
先看一段程序: for i in range(10): if i == 5: print( 'found it! i = %s' % i) break else: print('not found i ...
- MySQL 同一字段匹配多个值
delete from api_log WHERE curl LIKE '%.css' or curl LIKE '%.js' or curl LIKE '%.JPG'; 删除字段curl以 js,c ...
- python3使除法结果为整数
学习python3遇到问题: 今天在学习python时,想利用(121/100)得到的结果为整数 1, 121/100 outout:1.21 但是实际结果是浮点数 1.21 原因:python3后, ...
- python基础--冒泡排序
1.冒泡排序 1.首先用一张图来形象描述一下冒泡排序: 2.废话不多说,直接上代码 # 1.导入随机模块 import random # 2.定义一个列表,列表内的元素为20个100以内的随机整数 l ...
- springCloud的使用03-----服务消费者(feign)
1 创建springboot项目,引入jar依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=& ...
- docker--数据持久化之Data Volume
使用mysql为例 查看docker hub官方的mysql image 的dockerfile,有这一行:VOLUME /var/lib/mysql -v给volume创建别名 [root@loca ...
- 38.Subsets(子集和)
Level: Medium 题目描述: Given a set of distinct integers, nums, return all possible subsets (the power ...
- Nginx 实现 Rewrite 跳转
文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. 上一篇文章对Nginx的Location配置进行了讲解,本篇主要对于Nginx中的Rewrite跳转进行讲解. ...
- 笔记72 高级SSM整合
遇到的问题: 1.进行spring mvc测试的时候报错 测试代码: package com.li.test; import com.github.pagehelper.PageInfo; impor ...
- WPF ComboBox 默认选中无效
在WPF开发当中,我发现ComboBox的默认选中逻辑失效了,仔细查找后发现后台逻辑并没有出现问题. 测试后发现在XAML中,ComBoBox控件的SelectedValue属性需要写在ItemSou ...