码迷,mamicode.com
首页 > 编程语言 > 详细

第一个Spring Boot应用

时间:2017-07-31 23:53:22      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:title   package   UI   ica   dev   request   control   conf   targe   

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。----以上来自百度百科。

简单来说,它的一个特点是:习惯大于约定。Spring Boot提倡基于Java的配置,注解将会代替很多的配置文件。以下使用Maven为例,开发第一个Spring Boot应用。

1,环境需求

Apache Maven 3.2或更高版本

Java推荐使用较高版本(目前推荐使用Java 8,最低支持Java 6)

Servlet容器,本次使用Tomcat

IDE或其他工具

2,相关文件

pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.example</groupId>
 7     <artifactId>myproject</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9 
10     <!-- Inherit defaults from Spring Boot -->
11     <parent>
12         <groupId>org.springframework.boot</groupId>
13         <artifactId>spring-boot-starter-parent</artifactId>
14         <version>1.4.0.BUILD-SNAPSHOT</version>
15     </parent>
16 
17     <!-- Add typical dependencies for a web application -->
18     <dependencies>
19         <dependency>
20             <groupId>org.springframework.boot</groupId>
21             <artifactId>spring-boot-starter-web</artifactId>
22         </dependency>
23     </dependencies>
24 
25     <!-- Package as an executable jar -->
26     <build>
27         <plugins>
28             <plugin>
29                 <groupId>org.springframework.boot</groupId>
30                 <artifactId>spring-boot-maven-plugin</artifactId>
31             </plugin>
32         </plugins>
33     </build>
34 </project>

src/main/java/Example.java

 1 import org.springframework.boot.*;
 2 import org.springframework.boot.autoconfigure.*;
 3 import org.springframework.stereotype.*;
 4 import org.springframework.web.bind.annotation.*;
 5 
 6 @RestController
 7 @EnableAutoConfiguration
 8 public class Example {
 9 
10     @RequestMapping("/")
11     String home() {
12         return "Hello World!";
13     }
14 
15     public static void main(String[] args) throws Exception {
16         SpringApplication.run(Example.class, args);
17     }
18 
19 }

尽管代码不多,但这已经足够Spring Boot猜测你的想法并构建这个项目了。然后部署到Tomcat可以正常访问,Spring Boot的第一个应用完成!

3,代码笔记

(1)@SpringBootApplication注解等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan。

(2)@EnableAutoConfiguration,这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。

(3)@Configuration和@Bean同样可以用在普通的spring项目中,而不是Spring Boot特有的,在spring用的时候注意加上扫包配置

(4)通常将应用的main类放到其他类所在包的顶层(root package),并将@EnableAutoConfiguration注解到main类上,这样就隐式地定义了一个基础的包搜索路径(search package),以搜索某些特定的注解实体(比如@Service,@Component等) 。采用root package方式,就可以使用@ComponentScan注解而不需要指定basePackage属性。

 

第一个Spring Boot应用

标签:title   package   UI   ica   dev   request   control   conf   targe   

原文地址:http://www.cnblogs.com/bestzhanglei/p/7266264.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!