2 springboot的特点Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的 初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不 再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应 用开发领域(rapid application development)成为领导者。>
springboot(微框架) = springmvc(控制器) + spring(项目管理)
- 创建独立的Spring应用程序。
- 嵌入的Tomcat,无需部署WAR文件。
- 简化Maven配置。
- 自动配置Spring。
- 没有XML配置。
总结,如图所示:
springboot项目约定,如图所示:
1.打开IDEA如图所示的界面,点击Create New Project。
2.选择Empty Java,点击Next。如图所示:
3.填写项目名称,点击Finish。如图所示:
4.选择项目右键点击File–>New–Module,如图所示:
5.选择Maven,点击Next。如图所示:
6.填写项目名称,点击Next。如图所示:
7.点击Finish即可,如图所示:
8.引入相关依赖的代码如下:
4.0.0 com.txw springboot_01 1.0-SNAPSHOT war UTF-8 1.8 1.8 org.springframework.boot spring-boot-starter-parent 2.2.5.RELEASE org.springframework.boot spring-boot-starter-web springboot_01
如图所示:
9.在resources目录创建application.yml的代码如下:
server: port: 8080
如图所示:
10.编写SpringBoot01Application的代码如下:
package com.txw;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SuppressWarnings("all") // 注解警告信息
@SpringBootApplication // 标识这是一个springboot全局入口类
public class SpringBoot01Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot01Application.class,args);
}
}
如图所示:
运行如图所示:说明启动成功!
11.编写HelloController的代码如下:
package com.txw.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String hello(){
System.out.println("======hello world=======");
return "hello";
}
}
如图所示:
重新运行主启动类,如图所示:
通过浏览器访问:http://localhost:8080/hello/hello,如图所示:
控制台打印结果如图所示:
总结,如图所示:
springboot入口类注解&自定义banner&配置文件拆分,如图所示:
在springboot中可以管理自定义的简单组件对象的创建可以直接使用注解形式创建。
使用 @Repository @Service @Controller 以及@Component管理不同简单对象,比如: 比如要通过工厂创建自定义User对象。
1.编写User的代码如下:
package com.txw.entity;
import org.springframework.stereotype.Component;
@Component("user")
@SuppressWarnings("all") // 注解警告信息
public class User {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
如图所示:
通过工厂创建之后可以在使用处任意注入该对象,比如:在控制器中使用自定义简单对象创建。
2.编写HelloController的代码如下:
package com.txw.controller;
import com.txw.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@SuppressWarnings("all") // 注解警告信息
@Controller
@RequestMapping("/hello")
public class HelloController {
@Autowired
private User user;
@RequestMapping("/hello")
@ResponseBody
public String hello(){
System.out.println("======hello world=======");
System.out.println(user);
return "hello";
}
}
如图所示:
4.2 管理多个对象在springboot中如果要管理复杂对象必须使用@Configuration + @Bean注解进行管理。
1.管理复杂对象的创建的代码如下:
package com.txw.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Calendar;
@Configuration // (推荐)|@Component(不推荐)
public class Beans {
@Bean
public Calendar getCalendar(){
return Calendar.getInstance();
}
}
如图所示:
2.使用复杂对象的代码如下:
package com.txw.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Calendar;
@SuppressWarnings("all") // 注解警告信息
@Controller
@RequestMapping("/hello")
public class HelloController {
@Autowired
private Calendar calendar;
@RequestMapping("/hello")
@ResponseBody
public String hello(){
System.out.println("======hello world=======");
System.out.println(calendar);
return "hello";
}
}
如图所示:
总结,如图所示:
- 引入jsp的集成jar包的代码如下:
org.apache.tomcat.embed
tomcat-embed-jasper
如图所示:
3. 配置视图解析器的代码如下:
server:
port: 8080
spring:
mvc:
view:
prefix: / # 视图解析器的前缀
suffix: .jsp # 视图解析器的后置
如图所示:
4.编写HelloController的代码如下:
package com.txw.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("index")
@ResponseBody
public String index(){
System.out.println("======index controller=======");
return "index";
}
}
如图所示:
5. 引入jsp运行插件的代码如下:
org.springframework.boot
spring-boot-maven-plugin
-Dfile.encoding=UTF-8
如图所示:
运行流程,如图所示:
通过浏览器访问:http://localhost:8080/hello/index,如图所示:
控制台打印结果,如图所示:
如果报错误,解决办法,把如图所示的删除之后,重新加载即可!
总结,如图所示:



