Spring Boot
Posted cnwuhao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot相关的知识,希望对你有一定的参考价值。
spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程。它采用的是建立生产就绪的应用程序观点,优先于配置的惯例。
建构工程需要15分钟、jdk 1.8+、maven 3.0+。
打开Idea-> new Project ->Spring Initializr ->填写group、artifact ->钩上web(开启web功能)->点下一步。工具自动创建了一个项目,SpringbootApplication类为程序的入口。
pom.xml依赖文件如下
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 5 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 6 7 <modelVersion>4.0.0</modelVersion> 8 9 10 11 <groupId>com.forezp</groupId> 12 13 <artifactId>springboot-first-application</artifactId> 14 15 <version>0.0.1-SNAPSHOT</version> 16 17 <packaging>jar</packaging> 18 19 20 21 <name>springboot-first-application</name> 22 23 <description>Demo project for Spring Boot</description> 24 25 26 27 <parent> 28 29 <groupId>org.springframework.boot</groupId> 30 31 <artifactId>spring-boot-starter-parent</artifactId> 32 33 <version>1.5.2.RELEASE</version> 34 35 <relativePath/> <!-- lookup parent from repository --> 36 37 </parent> 38 39 40 41 <properties> 42 43 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 44 45 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 46 47 <java.version>1.8</java.version> 48 49 </properties> 50 51 52 53 <dependencies> 54 55 <dependency> 56 57 <groupId>org.springframework.boot</groupId> 58 59 <artifactId>spring-boot-starter-web</artifactId> 60 61 </dependency> 62 63 64 65 <dependency> 66 67 <groupId>org.springframework.boot</groupId> 68 69 <artifactId>spring-boot-starter-test</artifactId> 70 71 <scope>test</scope> 72 73 </dependency> 74 75 </dependencies> 76 77 78 79 <build> 80 81 <plugins> 82 83 <plugin> 84 85 <groupId>org.springframework.boot</groupId> 86 87 <artifactId>spring-boot-maven-plugin</artifactId> 88 89 </plugin> 90 91 </plugins> 92 93 </build> 94 95 96 97 98 99 </project>
其中spring-boot-starter-web不仅包含spring-boot-starter,还自动开启了web功能。
建个controller
1 import org.springframework.web.bind.annotation.RestController; 2 3 import org.springframework.web.bind.annotation.RequestMapping; 4 5 6 7 @RestController 8 9 public class HelloController { 10 11 12 13 @RequestMapping("/") 14 15 public String index() { 16 17 return "Greetings from Spring Boot!"; 18 19 } 20 21 22 23 }
启动SpringbootFirstApplication的main方法,输入
curl -S localhost:8080/hi
启动springboot 方式
cd到项目主目录
mvn clean mvn package mvn spring-boot: run
或者 cd 到target目录,java -jar 项目.jar
来看看springboot在启动的时候为我们注入了哪些bean
在程序入口加入:
@SpringBootApplication
public class SpringbootFirstApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootFirstApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let‘s inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}
程序输出:
Let’s inspect the beans provided by Spring Boot:
basicErrorController
beanNameHandlerMapping
beanNameViewResolver
characterEncodingFilter
commandLineRunner
conventionErrorViewResolver
defaultServletHandlerMapping
defaultViewResolver
dispatcherServlet
dispatcherServletRegistration
duplicateServerPropertiesDetector
embeddedServletContainerCustomizerBeanPostProcessor
error
errorAttributes
errorPageCustomizer
errorPageRegistrarBeanPostProcessor
….
….
在程序启动的时候,springboot自动诸如注入了40-50个bean.
单元测试
通过@RunWith() @SpringBootTest开启注解:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerIT {
@LocalServerPort
private int port;
private URL base;
@Autowired
private TestRestTemplate template;
@Before
public void setUp() throws Exception {
this.base = new URL("http://localhost:" + port + "/");
}
@Test
public void getHello() throws Exception {
ResponseEntity<String> response = template.getForEntity(base.toString(),
String.class);
assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));
}
}
运行它会先开启sprigboot工程,然后再测试,测试通过 ^.^
以上是关于Spring Boot的主要内容,如果未能解决你的问题,请参考以下文章
一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式
一张图帮你记忆,Spring Boot 应用在启动阶段执行代码的几种方式
一张图,理顺 Spring Boot应用在启动阶段执行代码的几种方式