Spring Boot是Spring框架的開發(fā)人員開發(fā)的子項(xiàng)目,以最小的配置創(chuàng)建獨(dú)立的生產(chǎn)級應(yīng)用程序。Spring Boot應(yīng)用程序通常捆綁為fat / uber jar文件,并且可以作為簡單的jar文件部署在任何平臺上。這就是為什么Spring Boot應(yīng)用程序是在Java中構(gòu)建微服務(wù)的理想選擇。
讓我們從eclipse中的spring boot hello world示例開始逐步學(xué)習(xí)。
目錄1.創(chuàng)建spring boot hello world項(xiàng)目模板2.將spring boot項(xiàng)目導(dǎo)入eclipse 3. Spring boot自動(dòng)配置4. Spring boot注解5.如何通過spring boot驗(yàn)證自動(dòng)配置的bean 6. Spring boot REST API示例7.演示
1.創(chuàng)建spring boot hello world項(xiàng)目模板
要為spring boot應(yīng)用程序創(chuàng)建模板,我建議使用http://start.spring.io/。在這里,您可以選擇當(dāng)前考慮的所有依賴關(guān)系,并生成項(xiàng)目。
Spring Boot選項(xiàng)
我選擇了Jersey,Spring Web,Spring HATEOAS,Spring JPA和Spring Security等依賴項(xiàng)。在下載并導(dǎo)入項(xiàng)目后或?qū)沓霈F(xiàn)需求時(shí),可以添加更多依賴項(xiàng)。
Generate Project按鈕將生成一個(gè).zip文件。下載文件并將其解壓縮到您的工作區(qū)中。
2.導(dǎo)入spring boot項(xiàng)目到eclipse
下一步是將生成的項(xiàng)目導(dǎo)入到IDE中。我為此使用了eclipse。
1)將spring boot項(xiàng)目導(dǎo)入為現(xiàn)有的maven項(xiàng)目。
將現(xiàn)有的Maven項(xiàng)目導(dǎo)入Eclipse
2)選擇pom.xml要導(dǎo)入的文件。
選擇pom.xml文件導(dǎo)入Maven項(xiàng)目
3)將導(dǎo)入項(xiàng)目,并且在生成zip文件時(shí)添加的依賴項(xiàng)將自動(dòng)下載并添加到classpath中。
導(dǎo)入的Spring Boot項(xiàng)目結(jié)構(gòu)
您現(xiàn)在已經(jīng)成功導(dǎo)入了spring boot應(yīng)用程序?,F(xiàn)在,讓我們看看它已經(jīng)為您配置了什么。
3. Spring Boot自動(dòng)配置
使用spring boot時(shí),好事是當(dāng)您添加一個(gè)依賴項(xiàng)(例如Spring security)時(shí),它會做出合理的假設(shè)并自動(dòng)為您配置一些默認(rèn)值。因此,您可以立即開始。
Spring Boot通過掃描類路徑中可用的依賴庫來使用約定而非配置。對于spring-boot-starter-*POM文件中的每個(gè)依賴關(guān)系,Spring Boot執(zhí)行一個(gè)默認(rèn)AutoConfiguration類。AutoConfiguration類使用*AutoConfiguration詞法模式,其中*表示庫。例如,spring安全性的自動(dòng)配置是通過完成的SecurityAutoConfiguration。
同時(shí),如果您不想對任何項(xiàng)目使用自動(dòng)配置,則此操作非常簡單。只需使用exclude = SecurityAutoConfiguration.class如下。
@SpringBootApplication?(exclude = SecurityAutoConfiguration.class)
public?class?SpringBootDemoApplication {
???public?static?void?main(String[] args)
???{
??????SpringApplication.run(SpringBootDemoApplication.class, args);
???}
}
也可以使用application.properties文件src/main/resources夾中的文件覆蓋默認(rèn)配置值。
4. Spring Boot注解
現(xiàn)在看一下@SpringBootApplication注解的實(shí)際作用。
4.1。@SpringBootApplication注解
SpringBootApplication?定義如下:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters =?@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class))
public?@interface?SpringBootApplication
{
???//more code
}
它為應(yīng)用程序配置目的添加了3個(gè)重要的注解。
@SpringBootConfiguration
@Configuration
public?@interface?SpringBootConfiguration
{
???//more code
}
該注解@Configuration為類添加注解,該注解將類標(biāo)記為應(yīng)用程序上下文的Bean定義的源。
@EnableAutoConfiguration
這告訴Spring Boot?pom.xml通過基于類路徑設(shè)置,其他bean和各種屬性設(shè)置添加bean來基于添加的依賴關(guān)系自動(dòng)配置重要的bean定義。
@ComponentScan
該注解告訴Spring Boot掃描基本軟件包,查找其他bean /組件并對其進(jìn)行配置。
5.如何通過Spring Boot驗(yàn)證自動(dòng)配置的Bean
如果您想知道在Spring Boot Hello World應(yīng)用程序中已自動(dòng)配置了所有bean?,請使用此代碼并運(yùn)行它。
SpringBootDemoApplication.java
import?java.util.Arrays;
import?org.springframework.boot.SpringApplication;
import?org.springframework.boot.autoconfigure.SpringBootApplication;
import?org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import?org.springframework.context.ApplicationContext;
@SpringBootApplication?(exclude = SecurityAutoConfiguration.class)
public?class?SpringBootDemoApplication {
???public?static?void?main(String[] args)
???{
??????ApplicationContext ctx = SpringApplication.run(SpringBootDemoApplication.class, args);
????????String[] beanNames = ctx.getBeanDefinitionNames();
????????Arrays.sort(beanNames);
????????for?(String beanName : beanNames)
????????{
????????????System.out.println(beanName);
????????}
???}
}
對于我的pom.xml文件,它會生成以下bean名稱以及許多其他springframework.boot.autoconfigure依賴項(xiàng)。
安慰
simpleControllerHandlerAdapter
sortResolver
spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties
spring.hateoas-org.springframework.boot.autoconfigure.hateoas.HateoasProperties
spring.http.encoding-org.springframework.boot.autoconfigure.web.HttpEncodingProperties
spring.http.multipart-org.springframework.boot.autoconfigure.web.MultipartProperties
spring.info-org.springframework.boot.autoconfigure.info.ProjectInfoProperties
spring.jackson-org.springframework.boot.autoconfigure.jackson.JacksonProperties
spring.jpa-org.springframework.boot.autoconfigure.orm.jpa.JpaProperties
spring.jta-org.springframework.boot.autoconfigure.transaction.jta.JtaProperties
spring.mvc-org.springframework.boot.autoconfigure.web.WebMvcProperties
spring.resources-org.springframework.boot.autoconfigure.web.ResourceProperties
springBootDemoApplication
standardJacksonObjectMapperBuilderCustomizer
stringHttpMessageConverter
tomcatEmbeddedServletContainerFactory
tomcatPoolDataSourceMetadataProvider
transactionAttributeSource
transactionInterceptor
transactionManager
transactionTemplate
viewControllerHandlerMapping
viewResolver
websocketContainerCustomizer
6. Spring Boot REST API示例
現(xiàn)在是時(shí)候?qū)⑷魏喂δ軜?gòu)建到hello world應(yīng)用程序中了。您可以根據(jù)需要添加功能,我正在添加REST API。
6.1。創(chuàng)建REST控制器
創(chuàng)建一個(gè)包c(diǎn)om.how2codex.demo.controller并在其中創(chuàng)建rest控制器。
EmployeeController.java
import?java.util.ArrayList;
import?java.util.List;
import?org.springframework.web.bind.annotation.RequestMapping;
import?org.springframework.web.bind.annotation.RestController;
import?com.how2codex.demo.model.Employee;
@RestController
public?class?EmployeeController
{
???@RequestMapping("/")
????public?List<Employee> getEmployees()
????{
??????List employeesList =?new?ArrayList<Employee>();
??????employeesList.add(new?Employee(1,"lokesh","gupta","howtodoinjava@gmail.com"));
??????return?employeesList;
????}
}
6.2。建立模型
創(chuàng)建模型類Employee。
Employee.java
public?class?Employee {
???public?Employee() {
???}
???public?Employee(Integer id, String firstName, String lastName, String email) {
??????super();
??????this.id = id;
??????this.firstName = firstName;
??????this.lastName = lastName;
??????this.email = email;
???}
???private?Integer id;
???private?String firstName;
???private?String lastName;
???private?String email;
???//getters and setters
???@Override
???public?String toString() {
??????return?"Employee [id="?+ id +?", firstName="?+ firstName
????????????+?", lastName="?+ lastName +?", email="?+ email +?"]";
???}
}
7. Spring Boot Hello World示例演示
現(xiàn)在,通過運(yùn)行中的main()方法啟動(dòng)應(yīng)用程序SpringBootDemoApplication。它將在port上啟動(dòng)嵌入式tomcat服務(wù)器8080。
由于我們已將演示REST API URL配置為根URL,因此您可以http;//localhost:8080/自行訪問它。
驗(yàn)證Spring Boot REST API
您將在測試工具或?yàn)g覽器中收到以下響應(yīng)。
[{“ id”:1,“ firstName”:“ lokesh”,“ lastName”:“ gupta”,“ email”:“ howtodoinjava@gmail.com”}]
這就是這個(gè)Spring Boot Rest Hello世界示例以及簡單的Rest API示例的全部內(nèi)容。
讓我問您有關(guān)如何使用Maven在Eclipse中創(chuàng)建Spring Boot項(xiàng)目的問題。
感謝原博主的序列, 轉(zhuǎn)載 --?http://how2codex.com/spring/spring-boot/3596.html