首页   >   代码编程   >   JAVA开发

使用idea搭建springboot项目图文教程

前面讲了很多框架的搭建教程,今天来写一篇关于springboot的搭建教程,现在idea用的比较多了,就直接使用idea来搭建项目,不过这个跟工具关系不大,不管是idea还是eclipse,项目骨架都是基于官网(http://start.spring.io)来生成的。

打开idea,选择new project菜单

使用idea搭建springboot项目图文教程

如果没有自己配置的骨架模板,就直接使用官网的,这里就可以直接进入下一步

使用idea搭建springboot项目图文教程

偶尔会有网络波动,会出现上面的界面,不过不用担心,可以稍微等待一下,也可以直接返回上一步再次进来

使用idea搭建springboot项目图文教程

上面都是maven工程的一些基本配置,填写完毕之后,直接进入下一步

使用idea搭建springboot项目图文教程

这一步就比较重要了,选择项目中所用到的依赖,springboot跟springmvc很大的区别就在于它的自动配置机制,所以这里选择的依赖全部都是starter,而且只要你选择了,它就会自动配置,如果你没有提供相应的配置文件,那么就会导致项目起不来。

依赖选择就绪之后,进入最后一步:

使用idea搭建springboot项目图文教程

这里需要选择workspace,这个选择完了之后,项目就创建成功了。

springboot不再需要外部web容器了,因为它默认使用了内嵌的tomcat,而且是以jar包的形式来运行,所以它的入口就是一个启动类的main方法,如下所示:

package com.wolffy.springbootdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

/**
 * springboot程序入口启动类,必须放在根目录
 */
// 启动类注解
@SpringBootApplication
// 因为demo中暂时没有数据库,但是starter-web中依赖了jdbc,所以关闭它的自动配置
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class SpringbootDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootDemoApplication.class, args);
    }
}

如果依旧想用外部容器,可以排除掉它自身starter中的tomcat依赖,具体方式请移步:springboot打war包,部署到外部tomcat,既然是tomcat,那就也必须可以对它进行一系列的配置,如:端口号、contextPath等

server.port=8081
server.tomcat.uri-encoding=UTF-8
server.error.path=/myerror
server.servlet.context-path=/
server.servlet.session.timeout=60m
server.servlet.session.cookie.http-only=true

将tomcat配置好了之后,项目基本上就可以跑起来了(处理好pom依赖),如果是web项目,我们还可以对其进行视图层view的配置(我刚才选择是freemarker),以及静态资源映射配置:

spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.ftl

spring.mvc.static-path-pattern=/static/**

springboot还提供了一个非常犀利的功能,它能支持多环境下配置文件的切换,而且是很方便的切换,我们可以将不同环境的配置文件放在一起,然后通过spring.profiles.active属性来指定使用哪一个配置。

spring.profiles.active=dev

直接启动,日志打印的还挺骚:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.4.RELEASE)

2019-05-17 14:18:59.896  INFO 9586 --- [           main] c.w.s.SpringbootDemoApplication          : Starting SpringbootDemoApplication on felixdeMacBook-Pro.local with PID 9586 (/Users/felix/workspace2/springboot-demo/target/classes started by felix in /Users/felix/workspace2/springboot-demo)
2019-05-17 14:18:59.930  INFO 9586 --- [           main] c.w.s.SpringbootDemoApplication          : The following profiles are active: dev
2019-05-17 14:19:00.126  INFO 9586 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@34129c78: startup date [Fri May 17 14:19:00 CST 2019]; root of context hierarchy
2019-05-17 14:19:03.025  INFO 9586 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2019-05-17 14:19:03.060  INFO 9586 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-05-17 14:19:03.060  INFO 9586 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.32
2019-05-17 14:19:03.065  INFO 9586 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/felix/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2019-05-17 14:19:03.177  INFO 9586 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-05-17 14:19:03.177  INFO 9586 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3051 ms
2019-05-17 14:19:03.286  INFO 9586 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2019-05-17 14:19:03.311  INFO 9586 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-05-17 14:19:03.312  INFO 9586 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-05-17 14:19:03.313  INFO 9586 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-05-17 14:19:03.313  INFO 9586 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2019-05-17 14:19:03.479  INFO 9586 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-05-17 14:19:03.790  INFO 9586 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@34129c78: startup date [Fri May 17 14:19:00 CST 2019]; root of context hierarchy
2019-05-17 14:19:03.855  INFO 9586 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/myerror]}" onto public java.lang.String com.wolffy.springbootdemo.controller.MyErrorController.getErrorPath()
2019-05-17 14:19:03.862  INFO 9586 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test/res],methods=[GET]}" onto public com.wolffy.springbootdemo.entity.User com.wolffy.springbootdemo.controller.TestController.res()
2019-05-17 14:19:03.862  INFO 9586 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test/error2],methods=[GET]}" onto public org.springframework.web.servlet.ModelAndView com.wolffy.springbootdemo.controller.TestController.error2()
2019-05-17 14:19:03.863  INFO 9586 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test/error1],methods=[GET]}" onto public java.lang.String com.wolffy.springbootdemo.controller.TestController.error1()
2019-05-17 14:19:03.893  INFO 9586 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-05-17 14:19:03.893  INFO 9586 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/static/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2019-05-17 14:19:04.126  INFO 9586 --- [           main] o.s.w.s.v.f.FreeMarkerConfigurer         : ClassTemplateLoader for Spring macros added to FreeMarker configuration
2019-05-17 14:19:04.227  INFO 9586 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2019-05-17 14:19:04.291  INFO 9586 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2019-05-17 14:19:04.297  INFO 9586 --- [           main] c.w.s.SpringbootDemoApplication          : Started SpringbootDemoApplication in 5.695 seconds (JVM running for 8.048)

访问一下试试:

使用idea搭建springboot项目图文教程

springboot-demo.zip

下载次数: 1

QQ群: 686430774

站长Q: 1347384268

分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

欢迎分享本文,转载请注明出处!

作者:不忘初心

发布时间:2019-05-22

永久地址:https://www.jiweichengzhu.com/article/ad6eb4bfa7354fbea865fdf673399e56

评论