01构建第一个SpringBoot工程
第一篇:构建第一个SpringBoot工程
创建项目
- 创建工程:Idea-> new Project ->Spring Initializr ->填写group、artifact ->钩上web(开启web功能)->点下一步就行了
- maven项目依赖spring-boot-starter-web不仅包含spring-boot-starter,还自动开启了web功能
- 使用@RestController注解控制器
- 使用@RequestMapping("/")注解路由
启动springboot
- cd到项目主目录
- 清理target: mvn clean
- 打包命令: 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>
- 启动命令:mvn spring-boot:run
- jar包方式启动,cd 到target目录,java -jar 项目.jar
CommandLineRunner执行定时任务
- CommandLineRunner接口主要用于实现在应用初始化后,去执行一段代码块逻辑,这段初始化代码在整个应用生命周期内只会执行一次。
- 用法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 !!");
}
}
- 用法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 !!");
}
}
- 使用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 !!");
}
}
springboot注入的bean
- 代码如下
// 下面的代码,在项目启动时,会执行,
// 打印启动时加载的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项目
- 通过@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 第一个项目做好了"));
}
}
- 报错:Could not resolve placeholder 'local.server.port' in value "${local.server.port}
- 在配置文件中添加:local.server.port=8090
- 报错: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)
参考
- SpringBoot非官方教程 | 第一篇:构建第一个SpringBoot工程 https://blog.csdn.net/forezp/article/details/70341651
- Spring boot CommandLineRunner接口使用例子 https://www.cnblogs.com/chenpi/p/9696310.html
01构建第一个SpringBoot工程的更多相关文章
- (转) SpringBoot非官方教程 | 第一篇:构建第一个SpringBoot工程
简介 spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程.它采用的是建立生产就绪的应用程序观点,优先于配置的惯例 ...
- 1. 构建第一个SpringBoot工程
1.File - New - Module 2.选项的是Spring Initializr(官方的构建插件,需要联网) ,一定要选择jdk 3.填写项目基本信息 Group:组织ID,一般分为多个段 ...
- 企业级 SpringBoot 教程 (一)构建第一个SpringBoot工程
简介 spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程.它采用的是建立生产就绪的应用程序观点,优先于配置的惯例 ...
- SpringBoot非官方教程 | 第一篇:构建第一个SpringBoot工程
转载请标明出处: https://www.fangzhipeng.com/springboot/2017/07/11/springboot1 本文出自方志朋的博客 简介 spring boot 它的设 ...
- 第一篇:构建第一个SpringBoot工程
简介 spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程.它采用的是建立生产就绪的应用程序观点,优先于配置的惯例 ...
- 构建第一个SpringBoot工程
学习和使用 SpringBoot 有一段时间了,现在开始陆陆续续会总结归纳 SpringBoot 学习中遇到的相关知识点. SpringBoot 设计目的是用来简化新Spring应用的初始搭建以及开发 ...
- SpringBoot(一):构建第一个SpringBoot工程
1.项目格式如下: 1.启动类: package com.monkey01.springbootstart; import org.springframework.boot.SpringApplica ...
- Spring Boot2 系列教程 (二) | 第一个 SpringBoot 工程详解
微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 哎呦喂,按照以往的惯例今天周六我的安排应该是待在家学学猫叫啥的.但是今年这种日子就可能一去不复返了,没法办法啊.前 ...
- 使用开发IDE生成一个springboot工程。
说实话,没办法,大势所趋. 当今天下,大企业,还是小公司,只要有想要更高效率的提高开发效率,能频繁迭代,又影响最小,那么只有使用分布式工程开发. 使用它就因为他快,加载东西,插件快,jar包引入方便. ...
随机推荐
- mybatis-generator : 自动生成代码
[参考文章]:mybatis generator自动生成代码时 只生成了insert 而没有其他 [参考文章]:Mybatis Generator最完整配置详解 1. pom <plugin&g ...
- IntelliJ IDEA激活,永久有效
2017.3.4版本 正版的idea实在太贵了,有能力请支持正版. 下载jar包,放置在idea的bin目录下,传送门 https://files.cnblogs.com/files/dslx/Jet ...
- lua-nginx-module模块里ngx_lua的所有指令以及可用ngx所有方法
http://www.04007.cn/article/430.html
- 105 - kube-scheduler源码分析 - predicate算法注册
一.predicate注册过程 今天我们来聊聊predicate函数是怎么被注册进去的,也就是要执行的一堆predicate是怎么成为“选中的孩子”. 代码位置:pkg/scheduler/fact ...
- RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.3版本全新发布
1.RDIFramework.NET框架介绍 RDIFramework.NET,基于.NET的快速信息化系统开发.整合框架,为企业或个人快速开发系统提供了强大的支持,开发人员不需要开发系统的基础功能和 ...
- springcloud情操陶冶-springcloud config server(二)
承接前文springcloud情操陶冶-springcloud config server(一),本文将在前文的基础上讲解config server的涉外接口 前话 通过前文笔者得知,cloud co ...
- 【2】Asp.Net Core2.2第一个功能增加
[前言] 上一篇完成了Asp.Net Core 2.2项目的建立,解释了一番项目结构,这一篇开始动手写个小功能,从Controller-Action-Model-View,完成前后端最基础的交互过程, ...
- 你真的懂JavaScript基础类型吗
夯实Javascript基础. 基本类型有六种: null,undefined,boolean,number,string,symbol. 基本类型的值是保存在栈内存中的简单数据段 基础类型特性 基础 ...
- Android - 文字向上翻滚效果的实现
本文转载https://xwc2013.iteye.com/blog/1976051 今天看到了一种文字翻滚的效果,感觉非常实用.所以就自己试着做出了这种效果,现在把它分享给大家! 首先在res目录下 ...
- Android Material Design控件使用(一)——ConstraintLayout 约束布局
参考文章: 约束布局ConstraintLayout看这一篇就够了 ConstraintLayout - 属性篇 介绍 Android ConstraintLayout是谷歌推出替代PrecentLa ...