关于SpringBoot的初识

it2023-02-01  51

关于SpringBoot的初识 SpringBoot简化了原先使用ssm框架开发代码的繁琐的文件配置操作,它使用起来更加方便,也就是极简的配置来省略以前的配置文件的操作

首先需要一个启动引导类,并且添加@SpringBootApplication注解

package com.itheima; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Springboot03ConfigApplication { public static void main(String[] args) { SpringApplication.run(Springboot03ConfigApplication.class, args); } }

在pom.xml文件中的配置,需要导入SpringBoot的坐标,以及web的坐标省去了手动导jar包的烦恼

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.itheima</groupId> <artifactId>springboot03-config</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot03-config</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- 处理@ConfigurationProperties注解导入配置的处理器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

测试类

package com.itheima.controller; import com.itheima.pojo.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /* * 向浏览器输出一个字符串:Hi,my name is SpringBoot * */ @RestController public class HelloController { @Value("${message1}") private String message1; @Value("${message2}") private String message2; @Autowired private Person person; @Autowired private Environment environment; @RequestMapping("sayHello") public String sayHello() { // System.out.println("message1:"+message1); // System.out.println("message2:"+message2); System.out.println("Person:"+person); System.out.println("message1:"+environment.getProperty("message1")); System.out.println("message2:"+environment.getProperty("message2")); // return "Hi.my name is SpringBoot!!! message1:"+message1+",message2:"+message2; return "Hi.my name is SpringBoot!!! Person:"+person; } }
最新回复(0)