关闭

spring-boot开发:使用内嵌容器进行快速开发及测试

标签: spring-boot微框架spring-boot快速开发
2376人阅读 评论(0) 收藏 举报
分类:

一、简述一下spring-boot微框架

1、spring-boot微框架是什么?

大家都知道,在使用spring框架进行应用开发时需要很多*.xml的初始化配置文件,而springBoot就是用来简化新的Spring应用的初始搭建以及开发过程的微框架。该框架使用了自动设置的方式来进行配置,使开发人员不再需要定义样板化的配置,该框架可以方便前端开发任玥或者测试人员进行快速开发或者测试单元

2、spring官方介绍

官方springboot教程:http://projects.spring.io/spring-boot/

二、spring-boot快速开发

1、依赖包引用

在这里个人使用spring-boot1.3.5,采用maven环境搭建

我的maven依赖:

<!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
       
    </dependencies>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/libs-snapshot</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/libs-snapshot</url>
        </pluginRepository>
    </pluginRepositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  

2、快速开发

简单开发发布一个helloworld!应用
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Configuration
@EnableAutoConfiguration
@Controller
public class TestController {
@RequestMapping("/hello")
@ResponseBody
public String hello()
{
	return "helloworld!";
	
}
	public static void main(String[] args) {
		SpringApplication.run(TestController.class,args);
	}
}

3、发布应用
如上代码,使用main()方法默认采用内置tomcat发布该应用,当然也可以使用其他内置容器或者外置容器
发布之后效果(看到这里,一个基于springBoot微框架的应用就开发完了,可以在浏览器打开http://localhost:8080/hello打开发布的应用):
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::            (v1.0.0.RC1)

2016-05-31 09:37:16.157  INFO 4268 --- [           main] cn.eguid.TestController                  : Starting TestController on wang-pc with PID 4268 (D:\eguid\Workspaces\MyEclipse2015\springBootTest\target\classes started by wangliang)
2016-05-31 09:37:16.329  INFO 4268 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3d04562f: startup date [Tue May 31 09:37:16 CST 2016]; root of context hierarchy
2016-05-31 09:37:17.673  INFO 4268 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.actuate.autoconfigure.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/actuate/autoconfigure/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2016-05-31 09:37:19.533  INFO 4268 --- [           main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
2016-05-31 09:37:19.921  INFO 4268 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-05-31 09:37:19.921  INFO 4268 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.47
2016-05-31 09:37:20.155  INFO 4268 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-05-31 09:37:20.155  INFO 4268 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3826 ms
2016-05-31 09:37:21.718  INFO 4268 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-05-31 09:37:21.824  INFO 4268 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String cn.eguid.TestController.hello()
2016-05-31 09:37:21.824  INFO 4268 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.util.Map<java.lang.String, java.lang.Object> org.springframework.boot.actuate.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-05-31 09:37:21.839  INFO 4268 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.actuate.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)
2016-05-31 09:37:21.855  INFO 4268 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-05-31 09:37:21.855  INFO 4268 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-05-31 09:37:22.277  INFO 4268 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Registering beans for JMX exposure on startup
2016-05-31 09:37:22.371  INFO 4268 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/shutdown],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.ShutdownMvcEndpoint.invoke()
2016-05-31 09:37:22.371  INFO 4268 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/configprops],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-05-31 09:37:22.371  INFO 4268 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/info],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-05-31 09:37:22.371  INFO 4268 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/trace],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-05-31 09:37:22.371  INFO 4268 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/beans],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-05-31 09:37:22.371  INFO 4268 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env/{name:.*}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2016-05-31 09:37:22.371  INFO 4268 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-05-31 09:37:22.371  INFO 4268 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/dump],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-05-31 09:37:22.371  INFO 4268 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/metrics/{name:.*}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2016-05-31 09:37:22.371  INFO 4268 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/metrics],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-05-31 09:37:22.371  INFO 4268 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/autoconfig],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-05-31 09:37:22.371  INFO 4268 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/health],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2016-05-31 09:37:22.371  INFO 4268 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2016-05-31 09:37:22.371  INFO 4268 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'environmentEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=environmentEndpoint]
2016-05-31 09:37:22.418  INFO 4268 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'healthEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=healthEndpoint]
2016-05-31 09:37:22.418  INFO 4268 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'beansEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=beansEndpoint]
2016-05-31 09:37:22.433  INFO 4268 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'infoEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=infoEndpoint]
2016-05-31 09:37:22.433  INFO 4268 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'metricsEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=metricsEndpoint]
2016-05-31 09:37:22.449  INFO 4268 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'traceEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=traceEndpoint]
2016-05-31 09:37:22.449  INFO 4268 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'dumpEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=dumpEndpoint]
2016-05-31 09:37:22.464  INFO 4268 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'autoConfigurationAuditEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=autoConfigurationAuditEndpoint]
2016-05-31 09:37:22.464  INFO 4268 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'shutdownEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=shutdownEndpoint]
2016-05-31 09:37:22.480  INFO 4268 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'configurationPropertiesReportEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=configurationPropertiesReportEndpoint]
2016-05-31 09:37:22.558  INFO 4268 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port: 8080
2016-05-31 09:37:22.558  INFO 4268 --- [           main] cn.eguid.TestController                  : Started TestController in 7.833 seconds (JVM running for 9.301)





2
1
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

两个springboot项目如何部署在同一个服务上,two spring boot applications deploy in same server

现在有两个spring boot的maven项目,需要部署到同一个外部的服务上(tomcat),他们都包含spring-boot-starter-actuator(健康检查)的依赖,但是出现下面的异常...
  • a862218
  • a862218
  • 2016-11-15 10:17
  • 3280

spring-boot入门(三)

spring boot 的监控管理功能
  • suncoder
  • suncoder
  • 2017-09-02 21:43
  • 161

监控和管理生产环境spring boot actuator

spring-boot-actuator模块提供了一个监控和管理生产环境的模块,可以使用http、jmx、ssh、telnet等拉管理和监控应用。审计(Auditing)、 健康(health)、数...
  • wh_ouyangshuang
  • wh_ouyangshuang
  • 2015-08-28 11:27
  • 16144

spring-boot开发:使用内嵌容器进行快速开发及测试

一、简述一下springBoot微框架 1、springBoot微框架是什么? 大家都知道,在使用spring框架进行应用开发时需要很多*.xml的初始化配置文件,而springBoot就是用来简化新...
  • eguid_1
  • eguid_1
  • 2016-05-31 09:11
  • 2376

spring boot 两个war包同时在tomcat中部署后启动报错

2017-05-25 17:14:38.858  INFO 11976 --- [ost-startStop-1] o.s.j.e.a.AnnotationMB eanExporter       ...
  • gisam
  • gisam
  • 2017-05-25 17:17
  • 2782

Spring基础:快速入门spring cloud(2):服务发现之eureka

Spring Cloud是Spring总多的Project中的一个,它提供了一整套的工具帮助系统架构师们在进行分布式设计的时候可以拿来即用, 在创建和发布微服务时极为便捷和有效。本系列文章将会使用最简...
  • liumiaocn
  • liumiaocn
  • 2016-12-28 08:10
  • 10466

Spring Boot应用服务部署——使用内嵌的tomcat容器

Spring Boot 应用服务部署有两种, 一种是传统的部署方式(没试验,暂时不清楚), 一种是使用Spring Boot内嵌的容器(tomcat 或者 jetty),本文介绍本文介绍的是基于内嵌的...
  • wang_peng_
  • wang_peng_
  • 2016-04-26 11:16
  • 5435

spring boot实战(第七篇)内嵌容器tomcat配置

spring boot实战(第七篇)内嵌容器tomcat配置默认容器spring boot默认web程序启用tomcat内嵌容器tomcat,监听8080端口,servletPath默认为 / 通过需...
  • liaokailin
  • liaokailin
  • 2015-10-07 12:30
  • 39995

spring boot实战(第七篇)内嵌容器tomcat配置

spring boot实战(第七篇)内嵌容器tomcat配置 默认容器 spring boot默认web程序启用tomcat内嵌容器tomcat,监听8080端口,servletPath默...
  • tongtong_use
  • tongtong_use
  • 2017-11-28 09:14
  • 67

Spring Boot 内嵌容器Undertow参数设置

配置项:# 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程,不要设置过大,如果过大,启动项目会报错:打开文件数过多server.undertow.io-...
  • rickiyeat
  • rickiyeat
  • 2017-12-26 20:50
  • 288
    个人资料
    • 访问:314814次
    • 积分:3588
    • 等级:
    • 排名:第10620名
    • 原创:75篇
    • 转载:0篇
    • 译文:0篇
    • 评论:277条
    提供一个交流平台,方便大家交流讨论
    Licenses
    博客专栏
    评论排行
    最新评论