一,使用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进行管理的更多相关文章

  1. 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 ...

  2. Spring入门(四):使用Maven管理Spring项目

    让我们先回顾下本系列的前3篇博客: Spring入门(一):创建Spring项目 Spring入门(二):自动化装配bean Spring入门(三):通过JavaConfig装配bean 1.为什么要 ...

  3. IDEA一步步创建Maven管理的Spring入门程序

    目前,做Java开发的很多人都在使用IDEA了,而有些人也选择用Eclipse,我这里介绍一下IDEA一步步创建Maven项目的步骤,并创建一个Spring的入门程序(Java项目,非Web项目),讲 ...

  4. Spring+Mybatis+MySql+Maven 简单的事务管理案例

    利用Maven来管理项目中的JAR包,同时使用Spring在业务处理层进行事务管理.数据库使用MySq,数据处理层使用Spring和Mybatis结合. 本案例代码主要结构如图: 1.数据库脚本 -- ...

  5. Spring Security 3.2.x与Spring 4.0.x的Maven依赖管理

    原文链接: Spring Security with Maven原文日期: 2013年04月24日翻译日期: 2014年06月29日翻译人员: 铁锚 1. 概述 本文通过实例为您介绍怎样使用 Mave ...

  6. Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制

    Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制 201311.27 代码下载 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 前言 ...

  7. Spring入门5.事务管理机制

    Spring入门5.事务管理机制 20131126 代码下载 : 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 回顾之前的知识,Spring 最为核心的两个部分 ...

  8. 【第十五篇】- Maven 依赖管理之Spring Cloud直播商城 b2b2c电子商务技术总结

    Maven 依赖管理 Maven 一个核心的特性就是依赖管理.当我们处理多模块的项目(包含成百上千个模块或者子项目),模块间的依赖关系就变得非常复杂,管理也变得很困难.针对此种情形,Maven 提供了 ...

  9. 【Spring Framework】Spring入门教程(八)Spring的事务管理

    事务是什么? 事务:指单个逻辑操作单元的集合. 在操作数据库时(增删改),如果同时操作多次数据,我们从业务希望,要么全部成功,要么全部失败.这种情况称为事务处理. 例如:A转账给B. 第一步,扣除A君 ...

  10. Spring入门(十四):Spring MVC控制器的2种测试方法

    作为一名研发人员,不管你愿不愿意对自己的代码进行测试,都得承认测试对于研发质量保证的重要性,这也就是为什么每个公司的技术部都需要质量控制部的原因,因为越早的发现代码的bug,成本越低,比如说,Dev环 ...

随机推荐

  1. 关于函数lower_bound()如何使用的问题

    这个函数是c++ STL里自带的函数,应该需要引用头文件#include<iostream> 功能:在一个有序的序列中查找可以将value(一个变量)放在队列里面而不会引起序列长度变化,单 ...

  2. 用select实现多客户端连接

    server.c 把accept也看成是一个read类型的函数, 于是我们可以把sockfd也放入到select中 maxi标记当前客户端连接数组的最大下标 select返回值为当前已经准备就绪的fd ...

  3. Springboot01-web

    Springboot快速构建 访问http://start.spring.io 构建springboot项目,这里选择版本2.0.4 单击Generate Project按钮下载springboot ...

  4. hashlib加密模块,加密方式:(MD5,sha级别)

    三,hashlib模块 算法介绍 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一 ...

  5. vue证明题二,让vue跑起来

    使用vue有很多连带产品,大多数入门的并非看不懂官方文档,也并非不会语法,而是卡在这些连带产品上 笔者刚刚入手这台电脑,什么都没装,就以此开始,从头构建一个vue项目吧,哪怕没有任何基础,跟着来应该是 ...

  6. js转换成布尔类型boolean

    /** * js转换成布尔值 * a.转换方法:Boolean(var) * b.数字转换成布尔,除了0与NaN,其余都是true * c.字符串转换成布尔,除了空串"",其余都是 ...

  7. GeneXus笔记本—获取当月的最后一天

    首先获取当前日期 然后赋值为当前年月的第一天  然后加一个月 减去一天 就是当月最后一天 多用于筛选数据时的条件或者区间 我们先随便拉个页面  简单点就好 放入两个textblock 然后点击Even ...

  8. mongoose 与 mylab 的使用 (1)

    1.引入mongoose  模块 const mongoose = require('mongoose'); 2.连接数据库 //连接数据库 mongoose.connect( db, {useNew ...

  9. oracle死锁查询

    select sess.sid ||','|| sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo.locked ...

  10. 利用PHP和百度ai实现文本以及图片的审核

    步骤: 首先打开百度ai 开发平台 注册一个账号: 注册账号,进入控制台 创建自己的应用,获取apikey 和秘钥 进入文档页 文本审核: 图像审核: 代码实例: class Sentive { pr ...