標籤:pos rem 組件 ... efi 排除 變數 not targe
@RestController和@RequestMapping註解
我們的Example類上使用的第一個註解是 @RestController 。這被稱為一個構造型(stereotype)註解。它為閱讀代碼的人們提供建議。對於Spring,該類扮演了一個特殊角色。在本樣本中,我們的類是一個web @Controller ,所以當處理進來的web請求時,Spring會詢問它。@RequestMapping 註解提供路由資訊。它告訴Spring任何來自"/"路徑的HTTP請求都應該被映射到 home 方法。 @RestController 註解告訴Spring以字串的形式渲染結果,並直接返回給調用者。
註: @RestController 和 @RequestMapping 註解是Spring MVC註解(它們不是Spring Boot的特定部分)
@EnableAutoConfiguration註解
第二個類層級的註解是 @EnableAutoConfiguration 。這個註解告訴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將仍舊盡最大努力去自動設定你的應用。
你可以通過將 @EnableAutoConfiguration 或 @SpringBootApplication 註解添加到一個 @Configuration 類上來選擇自動設定。
註:你只需要添加一個 @EnableAutoConfiguration 註解。我們建議你將它添加到主 @Configuration 類上。
如果發現應用了你不想要的特定自動設定類,你可以使用 @EnableAutoConfiguration 註解的排除屬性來禁用它們。
import org.springframework.boot.autoconfigure.*;import org.springframework.boot.autoconfigure.jdbc.*;import org.springframework.context.annotation.*;@Configuration@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})public class MyConfiguration {}
@Configuration
Spring Boot提倡基於Java的配置。儘管你可以使用一個XML源來調用 SpringApplication.run() ,我們通常建議你使用 @Configuration 類作為主要源。一般定義 main 方法的類也是主要 @Configuration 的一個很好候選。你不需要將所有的 @Configuration 放進一個單獨的類。 @Import 註解可以用來匯入其他配置類。另外,你也可以使用 @ComponentScan 註解自動收集所有的Spring組件,包括 @Configuration 類。
如果你絕對需要使用基於XML的配置,我們建議你仍舊從一個 @Configuration 類開始。你可以使用附加的 @ImportResource 註解載入XML設定檔。
@Configuration註解該類,等價 與XML中配置beans;用@Bean標註方法等價於XML中配置bean
@ComponentScan
你可以自由地使用任何標準的Spring架構技術去定義beans和它們注入的依賴。簡單起見,我們經常使用 @ComponentScan 註解搜尋beans,並結合 @Autowired 構造器注入。
如果使用上面建議的結構組織代碼(將應用類放到根包下),你可以添加 @ComponentScan 註解而不需要任何參數。你的所有應用程式組件( @Component , @Service , @Repository , @Controller 等)將被自動註冊為Spring Beans。
@SpringBootApplication
很多Spring Boot開發人員總是使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 註解他們的main類。由於這些註解被如此頻繁地一塊使用(特別是你遵循以上最佳實務時),Spring Boot提供一個方便的 @SpringBootApplication 選擇。
該 @SpringBootApplication 註解等價於以預設屬性使用 @Configuration , @EnableAutoConfiguration 和 @ComponentScan 。
package com.example.myproject;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScanpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
@ConfigurationProperties
屬性注入
@Component@ConfigurationProperties(prefix="connection")public class ConnectionSettings { private String username; private InetAddress remoteAddress; // ... getters and setters}
為了使用@ConfigurationProperties beans,你可以使用與其他任何bean相同的方式注入它們
@Servicepublic 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")@Beanpublic FooComponent fooComponent() {...}
和上面ConnectionSettings的樣本方式相同,任何以foo為首碼的屬性定義都會被映射到FooComponent上。
Spring Boot將嘗試校正外部的配置,預設使用JSR-303(如果在classpath路徑中)。你可以輕鬆的為你的@ConfigurationProperties類添加JSR-303 javax.validation約束註解:
@Component@ConfigurationProperties(prefix="connection")public class ConnectionSettings {@NotNullprivate InetAddress remoteAddress;// ... getters and setters}
@EnableConfigurationProperties
當@EnableConfigurationProperties註解應用到你的@Configuration時,任何被@ConfigurationProperties註解的beans將自動被Environment屬性配置
你可以通過在@EnableConfigurationProperties註解中直接簡單的列出屬性類來快捷的註冊@ConfigurationProperties bean的定義。
@Configuration@EnableConfigurationProperties(ConnectionSettings.class)public class MyConfiguration {}
@Component和@Bean
@Component被用在要被自動掃描和裝配的類上。@Component類中使用方法或欄位時不會使用CGLIB增強(及不使用代理類:調用任何方法,使用任何變數,拿到的是原始對象)Spring 註解@Component等效於@Service,@Controller,@Repository
@Bean主要被用在方法上,來顯式聲明要用產生的類;用@Configuration註解該類,等價 與XML中配置beans;用@Bean標註方法等價於XML中配置bean。
現在項目上,本工程中的類,一般都使用@Component來產生bean。在把通過web service取得的類,產生Bean時,使用@Bean和getter方法來產生bean
@Profiles
Spring Profiles提供了一種隔離應用程式配置的方式,並讓這些配置只能在特定的環境下生效。任何@Component或@Configuration都能被@Profile標記,從而限制載入它的時機。
@Configuration@Profile("production")public class ProductionConfiguration {// ...}
以正常的Spring方式,你可以使用一個spring.profiles.active的Environment屬性來指定哪個配置生效。你可以使用平常的任何方式來指定該屬性,例如,可以將它包含到你的application.properties中:
spring.profiles.active=dev,hsqldb
springboot註解使用說明