热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

springboot初级体验

Spring boot的介绍我就不多说了,网上可以自己看一下。

 

它的优点就是:快!适合小白,没有复杂的配置文件。

缺点也很明显:坑有些多, 文档略少,报错有时不知道该如何处理。

 

下面做个最简单的入门:

首先使用maven来创建一个项目,jar即可。

假设工程名字为spring-boot

然后在pom.xml中添加如下配置:

  
<parent>  
    <groupId>org.springframework.bootgroupId>  
    <artifactId>spring-boot-starter-parentartifactId>  
    <version>0.5.0.BUILD-SNAPSHOTversion>  
parent>  
  
  
<dependencies>  
    <dependency>  
        <groupId>org.springframework.bootgroupId>  
        <artifactId>spring-boot-starter-webartifactId>  
    dependency>  
dependencies>  
  
  
<build>  
    <plugins>  
        <plugin>  
            <groupId>org.springframework.bootgroupId>  
            <artifactId>spring-boot-maven-pluginartifactId>  
        plugin>  
    plugins>  
build>  
  
  
  
<repositories>  
    <repository>  
        <id>spring-snapshotsid>  
        <url>http://repo.spring.io/snapshoturl>  
        <snapshots><enabled>trueenabled>snapshots>  
    repository>  
    <repository>  
        <id>spring-milestonesid>  
        <url>http://repo.spring.io/milestoneurl>  
        <snapshots><enabled>trueenabled>snapshots>  
    repository>  
repositories>  
<pluginRepositories>  
    <pluginRepository>  
        <id>spring-snapshotsid>  
        <url>http://repo.spring.io/snapshoturl>  
    pluginRepository>  
    <pluginRepository>  
        <id>spring-milestonesid>  
        <url>http://repo.spring.io/milestoneurl>  
    pluginRepository>  
pluginRepositories>  

之后我们创建一个controller:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
 
@Controller
@EnableAutoConfiguration
public class SimpleController {
 
    @RequestMapping(value ="/hello", method = RequestMethod.GET)
    @ResponseBody
    public String hello(){
        return "hello world";
    }
 
    public static void main(String[] args) {
        SpringApplication.run(SimpleController.class, args);
    }
}

这个controller里有个main方法,看到没,我们直接运行即可,他会自动部署到tomcat或者jetty(8080端口不要被占用)。

然后在控制台你会看到:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.0.2.RELEASE)
 
2014-04-26 22:54:40.985  INFO 7236 --- [           main] c.r.spring.boot.SimpleController         : Starting SimpleController on rollen with PID 7236 (D:\workspace\GitHub\SpringDemo\spring-boot\target\classes started by wenchao.ren in D:\workspace\GitHub\SpringDemo\spring-boot)
2014-04-26 22:54:41.008  INFO 7236 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@50de0926: startup date [Sat Apr 26 22:54:41 CST 2014]; root of context hierarchy
2014-04-26 22:54:41.583  INFO 7236 --- [           main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
2014-04-26 22:54:41.706  INFO 7236 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2014-04-26 22:54:41.706  INFO 7236 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-04-26 22:54:41.785  INFO 7236 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2014-04-26 22:54:41.785  INFO 7236 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 779 ms
2014-04-26 22:54:42.055  INFO 7236 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2014-04-26 22:54:42.057  INFO 7236 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2014-04-26 22:54:42.289  INFO 7236 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-04-26 22:54:42.368  INFO 7236 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET],params=[],headers=[],cOnsumes=[],produces=[],custom=[]}" onto public java.lang.String com.rollenholt.spring.boot.SimpleController.hello()
2014-04-26 22:54:42.376  INFO 7236 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-04-26 22:54:42.377  INFO 7236 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2014-04-26 22:54:42.447  INFO 7236 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2014-04-26 22:54:42.459  INFO 7236 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http
2014-04-26 22:54:42.460  INFO 7236 --- [           main] c.r.spring.boot.SimpleController         : Started SimpleController in 1.675 seconds (JVM running for 1.944)
2014-04-26 22:54:54.963  INFO 7236 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2014-04-26 22:54:54.963  INFO 7236 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2014-04-26 22:54:54.971  INFO 7236 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 8 ms

当然还有一种启动运行方式,就是通过@Configuration+@ComponentScan开启注解扫描并自动注册相应的注解Bean

import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
import org.springframework.context.annotation.ComponentScan;  
import org.springframework.context.annotation.Configuration;  
  
@Configuration  
@ComponentScan  
@EnableAutoConfiguration  
public class Application {  
    public static void main(String[] args) {  
        SpringApplication.run(Application.class);  
    }  
}  

启动完之后浏览器访问一下:http://localhost:8080/hello

就是这么快。

对于想学习/体验Spring的新手,快速建立项目模型等可以考虑用这种方式。


推荐阅读
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 开发笔记:spring boot项目打成war包部署到服务器的步骤与注意事项
    本文介绍了将spring boot项目打成war包并部署到服务器的步骤与注意事项。通过本文的学习,读者可以了解到如何将spring boot项目打包成war包,并成功地部署到服务器上。 ... [详细]
  • Spring框架《一》简介
    Spring框架《一》1.Spring概述1.1简介1.2Spring模板二、IOC容器和Bean1.IOC和DI简介2.三种通过类型获取bean3.给bean的属性赋值3.1依赖 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • eclipse学习(第三章:ssh中的Hibernate)——11.Hibernate的缓存(2级缓存,get和load)
    本文介绍了eclipse学习中的第三章内容,主要讲解了ssh中的Hibernate的缓存,包括2级缓存和get方法、load方法的区别。文章还涉及了项目实践和相关知识点的讲解。 ... [详细]
  • MyBatis错题分析解析及注意事项
    本文对MyBatis的错题进行了分析和解析,同时介绍了使用MyBatis时需要注意的一些事项,如resultMap的使用、SqlSession和SqlSessionFactory的获取方式、动态SQL中的else元素和when元素的使用、resource属性和url属性的配置方式、typeAliases的使用方法等。同时还指出了在属性名与查询字段名不一致时需要使用resultMap进行结果映射,而不能使用resultType。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 如何搭建Java开发环境并开发WinCE项目
    本文介绍了如何搭建Java开发环境并开发WinCE项目,包括搭建开发环境的步骤和获取SDK的几种方式。同时还解答了一些关于WinCE开发的常见问题。通过阅读本文,您将了解如何使用Java进行嵌入式开发,并能够顺利开发WinCE应用程序。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 本文讨论了如何使用Web.Config进行自定义配置节的配置转换。作者提到,他将msbuild设置为详细模式,但转换却忽略了带有替换转换的自定义部分的存在。 ... [详细]
  • 本文介绍了使用哈夫曼树实现文件压缩和解压的方法。首先对数据结构课程设计中的代码进行了分析,包括使用时间调用、常量定义和统计文件中各个字符时相关的结构体。然后讨论了哈夫曼树的实现原理和算法。最后介绍了文件压缩和解压的具体步骤,包括字符统计、构建哈夫曼树、生成编码表、编码和解码过程。通过实例演示了文件压缩和解压的效果。本文的内容对于理解哈夫曼树的实现原理和应用具有一定的参考价值。 ... [详细]
author-avatar
猪的快乐旅途_278
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有