spring boot 它的設計目的就是爲例簡化開發,開啓了各類自動裝配,你不想寫各類配置文件,引入相關的依賴就能迅速搭建起一個web工程。它採用的是創建生產就緒的應用程序觀點,優先於配置的慣例。java
可能你有不少理由不放棄SSM,SSH,可是當你一旦使用了springboot ,你會以爲一切變得簡單了,配置變的簡單了、編碼變的簡單了,部署變的簡單了,感受本身大步流星,開發速度大大提升了。就比如,當你用了IDEA,你會以爲再也回不到Eclipse時代同樣。另,本系列教程所有用的IDEA做爲開發工具。linux
你須要:git
15分鐘 jdk 1.8或以上 maven 3.0+ Idea
打開Idea-> new Project ->Spring Initializr ->填寫group、artifact ->鉤上web(開啓web功能)->點下一步就好了。github
建立完工程,工程的目錄結構以下:web
- src -main -java -package -SpringbootApplication -resouces - statics - templates - application.yml -test - pom
resouces 資源文件spring
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.forezp</groupId> <artifactId>springboot-first-application</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-first-application</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </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>
其中spring-boot-starter-web不只包含spring-boot-starter,還自動開啓了web功能。apache
說了這麼多,你可能還體會不到,舉個栗子,好比你引入了Thymeleaf的依賴,spring boot 就會自動幫你引入SpringTemplateEngine,當你引入了本身的SpringTemplateEngine,spring boot就不會幫你引入。它讓你專一於你的本身的業務開發,而不是各類配置。瀏覽器
再舉個栗子,建個controller:tomcat
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; @RestController public class HelloController { @RequestMapping("/") public String index() { return "Greetings from Spring Boot!"; } }
啓動SpringbootFirstApplication的main方法,打開瀏覽器localhost:8080,瀏覽器顯示:springboot
Greetings from Spring Boot!
神奇之處:
你沒有作任何的web.xml配置; springboot爲你作了。 你沒有作任何的sping mvc的配置; springboot爲你作了。 你沒有配置tomcat ;springboot內嵌tomcat.
cd到項目主目錄:
mvn clean mvn package 編譯項目的jar 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工程,而後再測試,測試經過 ^.^
源碼下載:https://github.com/forezp/Spr...
市面上有不少springboot的書,有不少springboot的博客,爲何我還要寫這樣一個系列?到目前爲止,我沒有看過一本springboot的書,由於還沒來得及看,看的都是官方指南,固然也參考了不少的博客,他們都寫的很是的棒!在看官方指南和博客的時候,發現他們有不少不一樣之處,因此我打算寫一個來源於官方,經過本身理解加整合寫一個系列,因此取名叫《springboot 非官方教程》。我相信我寫的可能跟其餘人的寫的會不太同樣。另外,最主要的緣由仍是提升本身,懷着一個樂於分享的心,將本身的理解分享給更多須要的人。