快速搭建
springboot简单demo的构建就不多做赘述。
比较简单的方法,Fild --> new --> Project --> Spring Initializr,然后Next填写相应信息就好。
最简单的例子
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@ResponseBody
@RequestMapping
public String test() {
return "hello world";
}
}
关键注解
@SpringBootApplication
点进去可以看到
@SpringBootApplication注解主要组合了@Configuration、@EnableAutoConfiguration、@ComponentScan;若不是用@SpringBootApplication,则可以在入口类上直接使用@Configuration、@EnableAutoConfiguration、@ComponentScan。
@EnableAutoConfiguration让Spring Boot根据类路径中的jar包依赖为当前项目进行自动配置。例如,添加了spring-boot-starter-web依赖,会自动添加Tomcat和Spring MVC的以来,那么Spring Boot会对Tomcat和Spring MVC进行自动配置。又如,添加了spring-boot-starter-data-jpa依赖, Spring Boot会自动进行JPA相关的配置。
Spring Boot会自动扫描@SpringBootApplication所在类的同级包以及下级包里的bean(以及@Entity的实体类)。因此通常建议入口类放置在根包下。
@RestController
@EnableTransactionManagement
@EnableTransactionManagement注解开启注解式事务的支持。该注解通知Spring被@Transactional注解的方法被事务的切面包围。这样@Transactional就可以使用了。
配置文件
Spring Boot使用一个全局配置文件application.propertites或者application.yml,放置在src/main/resources目录或者类路径的/config下。Spring Boot的全局配置文件的作用是对一些默认配置的配置值进行修改。
https://blog.csdn.net/mrluzle/article/details/79164342
日志配置
Spring Boot集成mysql
Spring Boot dubbo zk