咱們的Example類上使用的第一個註解是 @RestController 。這被稱爲一個構造型(stereotype)註解。它爲閱讀代碼的人們提供建議。對於Spring,該類扮演了一個特殊角色。在本示例中,咱們的類是一個web @Controller ,因此當處理進來的web請求時,Spring會詢問它。@RequestMapping 註解提供路由信息。它告訴Spring任何來自"/"路徑的HTTP請求都應該被映射到 home 方法。 @RestController 註解告訴Spring以字符串的形式渲染結果,並直接返回給調用者。@RestController 和 @RequestMapping 註解是Spring MVC註解(它們不是Spring Boot的特定部分)java
這個註解告訴Spring Boot根據添加的jar依賴猜想你想如何配置Spring。因爲 spring-boot-starter-web 添加了Tomcat和Spring MVC,因此auto-configuration將假定你正在開發一個web應用並相應地對Spring進行設置。Starter POMs和Auto-Configuration:設計auto-configuration的目的是更好的使用"Starter POMs",但這兩個概念沒有直接的聯繫。你能夠自由地挑選starter POMs之外的jar依賴,而且Spring Boot將仍舊盡最大努力去自動配置你的應用。web
你能夠經過將 @EnableAutoConfiguration 或 @SpringBootApplication 註解添加到一個 @Configuration 類上來選擇自動配置。
注:你只須要添加一個 @EnableAutoConfiguration 註解。咱們建議你將它添加到主 @Configuration 類上。spring
若是發現應用了你不想要的特定自動配置類,你可使用 @EnableAutoConfiguration 註解的排除屬性來禁用它們。sql
import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.jdbc.*; import org.springframework.context.annotation.*; @Configuration @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}) public class MyConfiguration { }
Spring Boot提倡基於Java的配置。儘管你可使用一個XML源來調用 SpringApplication.run() ,咱們一般建議你使用 @Configuration 類做爲主要源。通常定義 main 方法的類也是主要 @Configuration 的一個很好候選。你不須要將全部的 @Configuration 放進一個單獨的類。 @Import 註解能夠用來導入其餘配置類。另外,你也可使用 @ComponentScan 註解自動收集全部的Spring組件,包括 @Configuration 類。app
若是你絕對須要使用基於XML的配置,咱們建議你仍舊從一個 @Configuration 類開始。你可使用附加的 @ImportResource 註解加載XML配置文件。框架
@Configuration註解該類,等價 與XML中配置beans;用@Bean標註方法等價於XML中配置bean。spring-boot
你能夠自由地使用任何標準的Spring框架技術去定義beans和它們注入的依賴。簡單起見,咱們常常使用 @ComponentScan 註解搜索beans,並結合 @Autowired 構造器注入。
若是使用上面建議的結構組織代碼(將應用類放到根包下),你能夠添加 @ComponentScan 註解而不須要任何參數。你的全部應用程序組件( @Component , @Service , @Repository , @Controller 等)將被自動註冊爲Spring Beans。this
不少Spring Boot開發者老是使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 註解他們的main類。因爲這些註解被如此頻繁地一塊使用(特別是你遵循以上最佳實踐時),Spring Boot提供一個方便的 @SpringBootApplication 選擇。
該 @SpringBootApplication 註解等價於以默認屬性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 。spa
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
屬性注入.net
@Component @ConfigurationProperties(prefix="connection") public class ConnectionSettings { private String username; private InetAddress remoteAddress; // ... getters and setters }
爲了使用@ConfigurationProperties beans,你可使用與其餘任何bean相同的方式注入它們
@Service public class MyService { @Autowired private ConnectionSettings connection; //... @PostConstruct public void openConnection() { Server server = new Server(); this.connection.configure(server); } }
正如使用@ConfigurationProperties註解一個類,你也能夠在@Bean方法上使用它。當你須要綁定屬性到不受你控制的第三方組件時,這種方式很是有用。
爲了從Environment屬性配置一個bean,將@ConfigurationProperties添加到它的bean註冊過程:
@ConfigurationProperties(prefix = "foo") @Bean public FooComponent fooComponent() { ... }
和上面ConnectionSettings的示例方式相同,任何以foo爲前綴的屬性定義都會被映射到FooComponent上。
Spring Boot將嘗試校驗外部的配置,默認使用JSR-303(若是在classpath路徑中)。你能夠輕鬆的爲你的@ConfigurationProperties類添加JSR-303 javax.validation約束註解:
@Component @ConfigurationProperties(prefix="connection") public class ConnectionSettings { @NotNull private InetAddress remoteAddress; // ... getters and setters }
當@EnableConfigurationProperties註解應用到你的@Configuration時,任何被@ConfigurationProperties註解的beans將自動被Environment屬性配置
你能夠經過在@EnableConfigurationProperties註解中直接簡單的列出屬性類來快捷的註冊@ConfigurationProperties bean的定義。
@Configuration @EnableConfigurationProperties(ConnectionSettings.class) public class MyConfiguration { }
@Component被用在要被自動掃描和裝配的類上。@Component類中使用方法或字段時不會使用CGLIB加強(及不使用代理類:調用任何方法,使用任何變量,拿到的是原始對象)Spring 註解@Component等效於@Service,@Controller,@Repository
@Bean主要被用在方法上,來顯式聲明要用生成的類;用@Configuration註解該類,等價 與XML中配置beans;用@Bean標註方法等價於XML中配置bean。
如今項目上,本工程中的類,通常都使用@Component
來生成bean。在把經過web service取得的類,生成Bean時,使用@Bean
和getter方法來生成bean
Spring Profiles提供了一種隔離應用程序配置的方式,並讓這些配置只能在特定的環境下生效。任何@Component或@Configuration都能被@Profile標記,從而限制加載它的時機。
@Configuration @Profile("production") public class ProductionConfiguration { // ... }
以正常的Spring方式,你可使用一個spring.profiles.active的Environment屬性來指定哪一個配置生效。你可使用日常的任何方式來指定該屬性,例如,能夠將它包含到你的application.properties中:
spring.profiles.active=dev,hsqldb