Spring Boot系列笔记--HelloWorld
其他
2021-01-30 21:33:44
阅读次数: 0
一、编写helloworld过程
- 创建maven工程(jar)
- 导入springboot相关依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- 编写主程序
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
- 编写相关controller、service
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}
}
- 运行
- 简化部署
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
二、注解细节
- @SpringBootApplication
进入该注解:@Target({
ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {
@Filter(
type = FilterType.CUSTOM,
classes = {
TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {
AutoConfigurationExcludeFilter.class}
)}
)
进入@EnableAutoConfiguration注解:@AutoConfigurationPackage
@Import({
AutoConfigurationImportSelector.class})
@Import注解将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所 有组件扫描到Spring容器
- @RestController
该注解是@ResponseBody和@Controller的结合体
转载自blog.csdn.net/weixin_44863537/article/details/108966908