第一篇:构建第一个SpringBoot工程

创建项目

  1. 创建工程:Idea-> new Project ->Spring Initializr ->填写group、artifact ->钩上web(开启web功能)->点下一步就行了
  2. maven项目依赖spring-boot-starter-web不仅包含spring-boot-starter,还自动开启了web功能
  3. 使用@RestController注解控制器
  4. 使用@RequestMapping("/")注解路由

启动springboot

  1. cd到项目主目录
  2. 清理target: mvn clean
  3. 打包命令: mvn package
  • 编译报错:No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
  • 原因是maven使用jdk编译,ide使用jre编译
  • 解决办法:
<!-- 在pom.xml中添加配置,让Maven使用jdk编译 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
  1. 启动命令:mvn spring-boot:run
  2. jar包方式启动,cd 到target目录,java -jar 项目.jar

CommandLineRunner执行定时任务

  1. CommandLineRunner接口主要用于实现在应用初始化后,去执行一段代码块逻辑,这段初始化代码在整个应用生命周期内只会执行一次。
  2. 用法1:和@Component注解一起使用
@Component
public class ApplicationStartupRunner implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass()); @Override
public void run(String... args) throws Exception {
logger.info("ApplicationStartupRunner run method Started !!");
}
}
  1. 用法2:和@SpringBootApplication注解一起使用
@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer implements CommandLineRunner { @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWebApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootWebApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
logger.info("Application Started !!");
}
}
  1. 使用3:声明一个实现了CommandLineRunner接口的Bean
public class ApplicationStartupRunner implements CommandLineRunner {
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void run(String... args) throws Exception {
logger.info("Application Started !!");
}
}
  1. 参考:https://www.cnblogs.com/chenpi/p/9696310.html

springboot注入的bean

  1. 代码如下
// 下面的代码,在项目启动时,会执行,
// 打印启动时加载的bean
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
// 下面使用的是lambda表达式,编译器根据方法体推测,返回一个CommandLineRunner对象
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}

单元测试,可以直接测试web项目

  1. 通过@RunWith() @SpringBootTest开启注解
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class FirstProgramApplicationTests {
@Test
public void contextLoads() {
}
// 获取服务端口
@LocalServerPort
private int port;
private URL base;
// 自动注入一个TestRestTemplate对象
@Autowired
private TestRestTemplate template;
// 执行测试之前执行
@Before
public void setUp() throws Exception {
this.base = new URL("http://localhost:" + port + "/");
}
// 执行测试
@Test
public void getHello() throws Exception {
ResponseEntity<String> response = template.getForEntity(base.toString(),
String.class);
assertThat(response.getBody(), equalTo("hello 第一个项目做好了"));
}
}
  1. 报错:Could not resolve placeholder 'local.server.port' in value "${local.server.port}
  • 在配置文件中添加:local.server.port=8090
  1. 报错:No qualifying bean of type 'org.springframework.boot.test.web.client.TestRestTemplate' available
  • 解决:
将头部的注解修改
@RunWith(SpringRunner.class)
@SpringBootTest
改为:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

参考

01构建第一个SpringBoot工程的更多相关文章

  1. (转) SpringBoot非官方教程 | 第一篇:构建第一个SpringBoot工程

    简介 spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程.它采用的是建立生产就绪的应用程序观点,优先于配置的惯例 ...

  2. 1. 构建第一个SpringBoot工程

    1.File -  New - Module 2.选项的是Spring Initializr(官方的构建插件,需要联网) ,一定要选择jdk 3.填写项目基本信息 Group:组织ID,一般分为多个段 ...

  3. 企业级 SpringBoot 教程 (一)构建第一个SpringBoot工程

    简介 spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程.它采用的是建立生产就绪的应用程序观点,优先于配置的惯例 ...

  4. SpringBoot非官方教程 | 第一篇:构建第一个SpringBoot工程

    转载请标明出处: https://www.fangzhipeng.com/springboot/2017/07/11/springboot1 本文出自方志朋的博客 简介 spring boot 它的设 ...

  5. 第一篇:构建第一个SpringBoot工程

    简介 spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程.它采用的是建立生产就绪的应用程序观点,优先于配置的惯例 ...

  6. 构建第一个SpringBoot工程

    学习和使用 SpringBoot 有一段时间了,现在开始陆陆续续会总结归纳 SpringBoot 学习中遇到的相关知识点. SpringBoot 设计目的是用来简化新Spring应用的初始搭建以及开发 ...

  7. SpringBoot(一):构建第一个SpringBoot工程

    1.项目格式如下: 1.启动类: package com.monkey01.springbootstart; import org.springframework.boot.SpringApplica ...

  8. Spring Boot2 系列教程 (二) | 第一个 SpringBoot 工程详解

    微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 哎呦喂,按照以往的惯例今天周六我的安排应该是待在家学学猫叫啥的.但是今年这种日子就可能一去不复返了,没法办法啊.前 ...

  9. 使用开发IDE生成一个springboot工程。

    说实话,没办法,大势所趋. 当今天下,大企业,还是小公司,只要有想要更高效率的提高开发效率,能频繁迭代,又影响最小,那么只有使用分布式工程开发. 使用它就因为他快,加载东西,插件快,jar包引入方便. ...

随机推荐

  1. LitepalNewDemo【开源数据库ORM框架-LitePal2.0.0版本的使用】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 本Demo使用的是LitePal2.0.0版本,对于旧项目如何升级到2.0.0版本,请阅读<赶快使用LitePal 2.0版本 ...

  2. TabLayoutViewPagerDemo【TabLayout+ViewPager可滑动】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用TabLayout搭配ViewPager实现可滑动的顶部选项卡效果. 效果图 代码分析 1.演示常规的设置. 2.通过自定义Vi ...

  3. Docker中完成Jenkins的安装

    去年就开始学习采用Docker+Jenkins+.Net Core搭建生成式流水线,一直拖到现在,也没有彻底的好好静下来去总结总结.趁着现在对自己的严格要求下,逐渐开始重视自我总结,以此来巩固逐渐失去 ...

  4. docker~不使用yml批量部署服务

    回到目录 有时,我们在进行持续集成环境有时,有时yml环境是没有的,它可能只提供了docker工具,而docker-compose这个大家伙可能不被提供,而这样我们如果希望自动化构建解决方案下所有的项 ...

  5. Jenkins|简单Job配置|启动脚本|测试报告

    目录 1.Jenkins安装 2.Jenkins启动脚本 3.节点配置 4.任务配置 5.集成HTML测试报告 1.Jenkins安装 操作环境:Ubuntu jenkins针对windows,ubu ...

  6. 补习系列(15)-springboot 分布式会话原理

    目录 一.背景 二.SpringBoot 分布式会话 三.样例程序 四.原理进阶 A. 序列化 B. 会话代理 C. 数据老化 小结 一.背景 在 补习系列(3)-springboot 几种scope ...

  7. springcloud情操陶冶-初识springcloud

    许久之前便听到了springcloud如雷贯耳的大名,但是不曾谋面,其主要应用于微服务的相关架构.笔者对微服务并不是很了解,但其既然比较出众,遂也稍微接触研究下 springcloud特性 sprin ...

  8. 使用Atlas进行元数据管理之Glossary(术语)

    背景:笔者和团队的小伙伴近期在进行数据治理/元数据管理方向的探索, 在接下来的系列文章中, 会陆续与读者们进行分享在此过程中踩过的坑和收获. 元数据管理系列文章: [0] - 使用Atlas进行元数据 ...

  9. myeclipse配置tomcat服务器

    在进行j2EE开发时,需要进行服务器配置, 这里因为要进行servlet开发,也要配置服务器.这里以在myeclipse上配置tomcat服务器为例 这里只是做下记录,方便自己以后查看 1.打开mye ...

  10. [转]Blue Prism VBO Cheat Sheet

    本文转自:https://www.cheatography.com/ethanium/cheat-sheets/blue-prism-vbo/ Blue Prism MAPIEx Configure ...