现在需要搭建spring boot框架,并实现一个HelloWorld的项目,让程序真正运行起来。

一、在pom.xml中引入spring-boot-start-parent,spring官方的叫stater poms,它可以提供dependency management,也就是依赖管理,引入以后在声明其它dependency的时候就不需要version了。

  1.  
    <parent>
  2.  
    <groupId>org.springframework.boot</groupId>
  3.  
    <artifactId>spring-boot-starter-parent</artifactId>
  4.  
    <version>1.5.3.RELEASE</version>
  5.  
    </parent>

二、需要在pom.xml中引入spring-boot-starter-web,spring官方解释spring-boot-start-web包含了spring webmvc和tomcat等web开发的特性。

  1.  
    <dependencies>
  2.  
    <dependency>
  3.  
    <groupId>org.springframework.boot</groupId>
  4.  
    <artifactId>spring-boot-starter-web</artifactId>
  5.  
    </dependency>
  6.  
    </dependencies>

三、如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。如果使用maven的spring-boot:run的话就不需要此配置。

  1.  
    <build>
  2.  
    <plugins>
  3.  
    <plugin>
  4.  
    <groupId>org.springframework.boot</groupId>
  5.  
    <artifactId>spring-boot-maven-plugin </artifactId>
  6.  
    </plugin>
  7.  
    </plugins>
  8.  
    </build>

四、下面开始写程序,我们需要一个启动类,然后在启动类声明让spring boot自动给我们配置spring需要的配置,比如:@SpringBootApplication,为了可以尽快让程序跑起来,我们简单写一个通过浏览器访问hello world字样的例子:

  1.  
    @RestController
  2.  
    @SpringBootApplication
  3.  
    public class ExampleSpringBoot {
  4.  
    @RequestMapping("/home")
  5.  
    String home() {
  6.  
    return "Hello World!";
  7.  
    }
  8.  
     
  9.  
    public static void main(String[] args) {
  10.  
    SpringApplication.run(ExampleSpringBoot.class, args);
  11.  
    }
  12.  
    }

其中@SpringBootApplication声明让spring boot自动给程序进行必要的配置,等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan。

@RestController返回json字符串的数据,直接可以编写RESTFul的接口。

五、运行我们的Application,我们先介绍第一种运行方式。第一种方式特别简单:右键Run As -> Java Application。之后打开浏览器输入地址:http://127.0.0.1:8080/home 就可以看到Hello world!了。
第二种方式右键project – Run as – Maven build – 在Goals里输入spring-boot:run ,然后Apply,最后点击Run。

六、运行报错
启动时报错NoClassDefFoundError: org/apache/juli/logging/LogFactory
参见下面的解决方法
http://blog.csdn.net/a78270528/article/details/77548779

七、完整的pom.xml文件

  1.  
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3.  
    <modelVersion>4.0.0</modelVersion>
  4.  
    <groupId>com.bocom</groupId>
  5.  
    <artifactId>maventest</artifactId>
  6.  
    <packaging>war</packaging>
  7.  
    <version>0.0.1-SNAPSHOT</version>
  8.  
    <!-- 继承父包 -->
  9.  
    <parent>
  10.  
    <groupId>org.springframework.boot</groupId>
  11.  
    <artifactId>spring-boot-starter-parent</artifactId>
  12.  
    <version>1.5.3.RELEASE</version>
  13.  
    <relativePath></relativePath>
  14.  
    </parent>
  15.  
    <name>maventest</name>
  16.  
    <url>http://maven.apache.org</url>
  17.  
    <dependencies>
  18.  
    <dependency>
  19.  
    <groupId>org.springframework.boot</groupId>
  20.  
    <artifactId>spring-boot-starter-web</artifactId>
  21.  
    </dependency>
  22.  
     
  23.  
    <dependency>
  24.  
    <groupId>junit</groupId>
  25.  
    <artifactId>junit</artifactId>
  26.  
    <version>3.8.1</version><!--$NO-MVN-MAN-VER$-->
  27.  
    <scope>test</scope>
  28.  
    </dependency>
  29.  
     
  30.  
    <dependency>
  31.  
    <groupId>org.apache.tomcat.embed</groupId>
  32.  
    <artifactId>tomcat-embed-logging-juli</artifactId>
  33.  
    <version>8.0.23</version>
  34.  
    </dependency>
  35.  
    </dependencies>
  36.  
    <build>
  37.  
    <finalName>maventest</finalName>
  38.  
    <plugins>
  39.  
    <plugin>
  40.  
    <groupId>org.springframework.boot</groupId>
  41.  
    <artifactId>spring-boot-maven-plugin </artifactId>
  42.  
    </plugin>
  43.  
    </plugins>
  44.  
    </build>
  45.  
    </project>

至些,我们的Spring boot项目已经搭建完成,并成功的运行了HelloWorld的项目。

---------------------

本文来自 二一点 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/a78270528/article/details/77573818?utm_source=copy

Myeclipse下使用Maven搭建spring boot项目(第二篇)的更多相关文章

  1. Myeclipse下使用Maven搭建spring boot项目

    开发环境:Myeclipse2017.JDK1.6.Tomcat 8.0.Myeclipse下使用Maven搭建spring boot项目,详细过程如下: 1. New -> Project.. ...

  2. Myeclipse下使用Maven搭建spring boot2.0项目

    现在需要搭建spring boot框架,并实现一个HelloWorld的项目,让程序真正运行起来. 一.在pom.xml中引入spring-boot-start-parent,spring官方的叫st ...

  3. Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创

    原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...

  4. Maven 搭建spring boot多模块项目

    Maven 搭建spring boot多模块项目 备注:所有项目都在idea中创建 1.idea创建maven项目 1-1: 删除src,target目录,只保留pom.xml 1-2: 根目录pom ...

  5. Spring Boot入门(一):搭建Spring Boot项目

    从本篇博客开始,我们开始进入Spring Boot的世界,它的出现使Spring的开发变得更加简洁,因此一经推出受到众多程序员的喜爱. 作为Spring Boot系列的第一篇博客,我们先来讲解下如何搭 ...

  6. 基于 intellij IDEA 快速搭建Spring Boot项目

           在<一步步搭建 Spring Boot maven 框架的工程>一文中,已经介绍了如何使用Eclipse快速搭建Spring Boot项目.由于最近将开发工具由Eclipse ...

  7. 使用IDEA,Eclispe搭建Spring Boot项目

    如何创建一个Spring Boot项目?这里使用maven来进行依赖管理,根据常用的IDE,可以使用IDEA.Eclipse.或者访问官方网站搭建. 项目搭建环境准备 JDK:1.8 MAVEN:3. ...

  8. Spring Boot系列学习文章(一) -- Intellij IDEA 搭建Spring Boot项目

    前言: 最近做的一个项目是用Spring Boot来做的,所以把工作中遇到的一些知识点.问题点整理一下,做成一系列学习文章,供后续学习Spring Boot的同仁们参考,我也是第一次接触Spring ...

  9. Spring boot入门(一):快速搭建Spring boot项目

    (一)Spring boot介绍 本部分摘自:https://www.zhihu.com/question/64671972/answer/223383505 Spring Boot是由Pivotal ...

随机推荐

  1. python基础之Event对象、队列和多进程基础

    Event对象 用于线程间通信,即程序中的其一个线程需要通过判断某个线程的状态来确定自己下一步的操作,就用到了event对象 event对象默认为假(Flase),即遇到event对象在等待就阻塞线程 ...

  2. Mysql Communications link failure 问题的解决

    问题现象 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last p ...

  3. c#(winform)中自定义ListItem类方便ComboBox添加Item项

    1.定义ListItem类 public class ListItem { private string _key = string.Empty; private string _value = st ...

  4. 如何实现php异步处理

    在实际生成环境下,php作为后台的接口服务器已经很常见,php当然具有它能作为后台服务器的优势之处,但是,在处理一些客户端并不关心的结果时,就显出它的弊端了---没有异步执行的机制.就比如我们想做一些 ...

  5. Atitit. 解释器模式框架选型 and应用场景attilax总结 oao

    Atitit. 解释器模式框架选型 and应用场景attilax总结 oao 1. 解释器模式结构描述 1 2. 如何实现(简单的解释器模式,仅仅通过词法分析即可实现,而无需token流进行处理. 2 ...

  6. redis 做为缓存服务器 注项!

    作为缓存服务器,如果不加以限制内存的话,就很有可能出现将整台服务器内存都耗光的情况,可以在redis的配置文件里面设置: # maxmemory <bytes> #限定最多使用1.5GB内 ...

  7. 集合运算 蓝桥杯 set容器

    题目描述 给出两个整数集合A.B,求出他们的交集.并集以及B在A中的余集. 输入格式 第一行为一个整数n,表示集合A中的元素个数. 第二行有n个互不相同的用空格隔开的整数,表示集合A中的元素. 第三行 ...

  8. C++重载IO操作符

    操作符的重载有一定的规则,而IO操作符必须重载为普通函数,且应该声明为类的友元函数.我试了,非友元也可以,但是必须提供访问成员变量的函数,所以,出于效率的考虑还是应该定义为友元. 规则如下: 1.  ...

  9. Shell脚本中调用另外一个脚本的方法

    (转载): 在Linux平台上开发,经常会在console(控制台)上执行另外一个脚本文件,经常用的方法有:./my.sh 或 source my.sh 或 . my.sh:这三种方法有什么不同呢?我 ...

  10. 服务器端cs文件

    服务器端向mysql数据库写数据 using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Colle ...