第一篇:构建第一个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. docker初体验,搭建自用的gitlab服务

    一.前言 git在如日中天的版本管理系统,现在如果不是工作在git版本管理系统下,几乎都不好意思给人打招呼.现在就有现成的互联网的git服务器提供给大家使用,例如号称程序的社交网络github. 正好 ...

  2. vue初始化项目,构建vuex的后台管理项目架子

    构架vuex的后台管理项目源码:https://github.com/saucxs/structure-admin-web 一.node安装 可以参考这篇文章http://www.mwcxs.top/ ...

  3. QPainterPath 不规则提示框

    currentPosition()是最后一次绘制后的“结束点”(或初始点),使用moveTo()移动currentPosition()而不会添加任何元素. QPainterPath ​合并: 1.方法 ...

  4. 通用查询设计思想(2)- 基于ADO.Net的设计

    不少公司用的是ADO.NET的访问方式,估计不少朋友对于sql的拼写真是深恶痛绝,在没有一个封装足够好的底层的项目,特别是经过许多人接手之后,代码那叫一个惨不忍睹,本文借助[通用查询设计思想]这篇文章 ...

  5. es6涉及的那点东西

    前言 ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准.因为当前版本的ES6是在2015年发布的,所以又称ECMAScript 2015. 也就是说,ES6就是ES20 ...

  6. 图片与Base64的转换

    图片转为Base64 // 图片转化成base64字符串 public static String GetImageStr() {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 ...

  7. SpringMVC与Struts2的主要区别

    区别1: Struts2 的核心是基于一个Filter即StrutsPreparedAndExcuteFilterSpringMvc的核心是基于一个Servlet即DispatcherServlet( ...

  8. ASP.NET WebApi系列

    ASP.NET Web API 是一种框架,用于轻松构建可以访问多种客户端(包括浏览器和移动设备)的 HTTP 服务. ASP.NET Web API 是一种用于在 .NET Framework 上构 ...

  9. 学习笔记—JDBC

    JDBC的概念 JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言 ...

  10. ArcEngine ILabelEngineLayerProperties Expression 添加常量

    ArcEngine实现复杂标注的时候,需要结合几个字段并将常量加载表达式中. 开始的时候发现VBScript需要采用“”来括起来常量才能在VB中正常使用,但是 ILabelEngineLayerPro ...