IDEA中创建SpringBoot工程

tech2023-06-01  56

1、Create New Project

#配置tomcat的端口 server.port=8081 #配置项目的名称 server.servlet.context-path=/myspringboot package com.springboot.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TestController { @RequestMapping("/getHello") @ResponseBody public String getHello(){ return "hellospringboot"; } }

2、启动并测试

3、注意事项:Springboot知识点积累

3.1新建一个包,创建一个控制器,如图:

3.2、访问第一个 控制器TestController,并且请求  getHello的时候,返回了 hellospringboot,但是访问新建包的TestControllerdemo的时候,请求getHellodemo,却失败了,原因是没有扫描到新建的包的控制器,如图:

3.3、注释的介绍:

package com.springboot.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication //springbootApplication注解会自动扫描当前包下所有的spring注解,以及当前包下所有的子包下的spring注解 @ComponentScan(value = "com.springboot.demo2.controller") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } 将扫描包的注释加上之后,再次访问成功;如图:

 4、Springboot的热部署(插件:devTools),热部署实现修改了代码之后,实现自动打包发送到Tomcat

4.1 热部署的步骤

在pom文件加入devtools插件,在 spring-boot-maven-plugin下增加开启热部署的标签

<!--添加热部署的插件--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!--开启热部署支持--> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> <!--将源码目录中的xml文件一起打包发布--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </resource> </resources> </build>

如果做以上配置不生效: 1.开启idea自动make功能     ①:Settings -compile-> 查找make|bulid project automatically --> 选中     ②:ctrl+shift+A --->查找Registry --> 找到并勾选compiler.automake.allow.when.app.running    或者 ctrl+shift+alt+/

最新回复(0)