1. 程式人生 > >spring系列常用註解

spring系列常用註解

http://blog.csdn.net/sudiluo_java/article/details/51858978來自 這位大牛的分享

宣告Bean的註解:
  • @Component : 元件,沒有明確的角色
  • @Service : 在業務邏輯層(service層)使用
  • @Repository : 在資料訪問層(dao層)使用.
  • @Controller : 在展現層(MVC--SpringMVC)使用

注入Bean的註解:
  • @Aautowired : Spring提供的註解.
  • @Inject : JSR-330提供的註解
  • @Resource : JSR-250提供的註解

配置檔案的註解:
  • @Configuration : 聲明當前類是個配置類,相當於一個Spring配置的xml檔案.
  • @ComponentScan (cn.test.demo): 自動掃描包名下所有使用 @Component @Service  @Repository @Controller 的類,並註冊為Bean
  • @WiselyConfiguration : 組合註解 可以替代 @Configuration和@ComponentScan
  • @Bean : 註解在方法上,聲明當前方法的返回值為一個Bean.
    • @Bean(initMethod="aa",destroyMethod="bb")--> 指定 aa和bb方法在構造之後.Bean銷燬之前執行.

AOP切面程式設計註解:
  • @Aspect : 宣告這是一個切面 
  • @After @Before. @Around 定義切面,可以直接將攔截規則(切入點 PointCut)作為引數
  • @PointCut : 專門定義攔截規則 然後在 @After @Before. @Around 中呼叫
  • @Transcational : 事務處理
  • @Cacheable : 資料快取
  • @EnableAaspectJAutoProxy : 開啟Spring 對 這個切面(Aspect )的支援
  • @Target (ElementType.TYPE):元註解,用來指定註解修飾類的那個成員 -->指定攔截規則
  • @Retention(RetentionPolicy.RUNTIME) 
    • --->當定義的註解的@Retention為RUNTIME時,才能夠通過執行時的反射機制來處理註解.-->指定攔截規則

Spring 常用配置:
  • @import :匯入配置類
  • @Scope : 新建Bean的例項 @Scope("prototype") 宣告Scope 為 Prototype
  • @Value : 屬性注入
    • @Value ("我愛你")  --> 普通字串注入
    • @Value ("#{systemProperties['os.name']}") -->注入作業系統屬性
    • @Value ("#{ T (java.lang.Math).random()  * 100.0 }") --> 注入表示式結果
    • @Value ("#{demoService.another}") --> 注入其他Bean屬性
    • @Value ( "classpath:com/wisely/highlight_spring4/ch2/el/test.txt" ) --> 注入檔案資源
    • @Value ("http://www.baidu.com")-->注入網址資源
    • @Value ("${book.name}" ) --> 注入配置檔案  注意: 使用的是$ 而不是 #
  • @PostConstruct : 在建構函式執行完之後執行
  • @PreDestroy  : 在 Bean 銷燬之前執行
  • @ActiveProfiles : 用來宣告活動的 profile
  • @profile: 為不同環境下使用不同的配置提供了支援
    •  @Profile("dev") .......對方法名為 dev-xxxx的方法提供例項化Bean
  • @EnableAsync : 開啟非同步任務的支援(多執行緒)
  • @Asyns : 宣告這是一個非同步任務,可以在類級別 和方法級別宣告.
  • @EnableScheduling : 開啟對計劃任務的支援(定時器)
  • @Scheduled : 宣告這是一個計劃任務 支援多種計劃任務,包含 cron. fixDelay fixRate
    • @Scheduled (dixedDelay = 5000) 通過註解 定時更新
  • @Conditional : 條件註解,根據滿足某一特定條件建立一個特定的Bean
  • @ContextConfiguration : 載入配置檔案
    • @ContextConfiguration(classes = {TestConfig.class})
    • @ContextConfiguration用來載入ApplicationContext 
    • classes屬性用來載入配置類
  • @WebAppCofiguration : 指定載入 ApplicationContext是一個WebApplicationContext
@Enable*註解:
  • @EnableAsync : 開啟非同步任務的支援(多執行緒)
  • @EnableScheduling : 開啟對計劃任務的支援(定時器)
  • @EnableWebMVC : 開啟對Web MVC 的配置支援
  • @EnableAaspectJAutoProxy : 開啟Spring 對 這個切面(Aspect )的支援
  • @EnableConfigurationProperties 開啟對@ConfigurationProperties註解配置Bean的支援
  • @EnableJpaRepositories : 開啟對Spring Data JAP Repository 的支援
  • @EnableTransactionManagement 開啟對註解式事物的支援
  • @EnableCaching開啟註解是快取的支援.
  • @EnableDiscoveryClient 讓服務發現伺服器,使用伺服器.Spring cloud 實現服務發現
  • @EnableEurekaServer 註冊伺服器 spring cloud 實現服務註冊@
  • @EnableScheduling 讓spring可以進行任務排程,功能類似於spring.xml檔案中的名稱空間<task:*>
  • @EnableCaching 開啟Cache快取支援;
SpringMVC 常用註解:
  • @Controller : 註解在類上 宣告這個類是springmvc裡的Controller,將其宣告為一個spring的Bean.
  • @RequestMapping :可以註解在類上和方法上 對映WEB請求(訪問路徑和引數)
    • @RequestMapping(value= "/convert",produces+{"application/x-wisely"}) 設定訪問URL 返回值型別
  • @ResponseBody : 支援將返回值放入response體內 而不是返回一個頁面(返回的是一個組資料)
  • @RequestBody : 允許request的引數在request體中,而不是直接連線在地址後面 次註解放置在引數前
  • @Path Variable : 用來接收路徑引數 如/test/001,001為引數,次註解放置在引數前
  • @RestController : @Controller + @ResponseBody 組合註解
  • @ControllerAdvice : 通過@ControllerAdvice可以將對已控制器的全域性配置放置在同一個位置
  • @ExceptionHandler : 用於全域性處理控制器的異常
    • @ExceptionHandier(value=Exception.class) -->通過value屬性可過濾攔截器條件,攔截所有的異常
  • @InitBinder : 用來設定WebDataBinder , WebDataBinder用來自動繫結前臺請求引數到Model中.
  • @ModelAttrbuute : 繫結鍵值對到Model中,
  • @RunWith : 執行器 
    • @RunWith(JUnit4.class)就是指用JUnit4來執行
    • @RunWith(SpringJUnit4ClassRunner.class),讓測試運行於Spring測試環境
    • @RunWith(Suite.class)的話就是一套測試集合,
  • @WebAppConfiguration("src/main/resources") : 註解在類上,用來宣告載入的ApplicationContex 是一個WebApplicationContext ,它的屬性指定的是Web資源的位置,預設為 src/main/webapp ,自定義修改為 resource
  • @Before : 在 xxx 前初始化

Spring Boot 註解:
  •  @SpringBootApplication : 是Spring Boot 專案的核心註解 主要目的是開啟自動配置
  • @Value : 屬性注入,讀取properties或者 Yml 檔案中的屬性
  • @ConfigurationProperties : 將properties屬性和一個Bean及其屬性關聯,從而實現型別安全的配置
    • @ConfigurationProperties(prefix = "author",locations = {"classpath:config/author.properties"})
    • 通過@ConfigurationProperties載入配置,通過prefix屬性指定配置字首,通過location指定配置檔案位置
  • @EnableAutoConfiguration 註解:作用在於讓 Spring Boot   根據應用所宣告的依賴來對 Spring 框架進行自動配置
            這個註解告訴Spring Boot根據新增的jar依賴猜測你想如何配置Spring。由於spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration將假定你正在開發一個web應用並相應地對Spring進行設定。
  • @ Configuration 註解,以明確指出該類是 Bean 配置的資訊源
  • @ComponentScan 註解會告知Spring掃描指定的包來初始化Spring Bean這能夠確保我們宣告的Bean能夠被發現。
  • @ImportResource 註解載入XML配置檔案
  • @EnableAutoConfiguration(exclude={xxxx.class}) 禁用特定的自動配置
  • @SpringBootApplication   註解等價於以預設屬性使用@Configuration,@EnableAutoConfiguration和     @ComponentScan。


@SuppressWarnings註解
  • @SuppressWarnings("unchecked")
    • 告訴編譯器忽略 unchecked 警告資訊,如使用 list ArrayList等未進行引數化產生的警告資訊
  • @SuppressWarnings("serial")
    • 如果編譯器出現這樣的警告資訊: The serializable class WmailCalendar does not declare a static final serialVersionUID field of type long     使用這個註釋將警告資訊去掉。
  • @SuppressWarnings("deprecation")
    • 如果使用了使用@Deprecated註釋的方法,編譯器將出現警告資訊。使用這個註釋將警告資訊去掉。
  • @SuppressWarnings("unchecked", "deprecation")
    • 告訴編譯器同時忽略unchecked和deprecation的警告資訊。
  • @SuppressWarnings(value={"unchecked", "deprecation"})
    • 等同於@SuppressWarnings("unchecked", "deprecation")
案例

@Entity @Table (name = "S_PRODUCEINFO" ) @Data @NoArgsConstructor @AllArgsConstructor public class ProduceInfoEntity {
     @Id      @Column (name =  "app_name" , unique = true , length = 50)      private String name ;
     @Column (name =  "status" )      @Enumerated (EnumType. STRING )      private ProduceStatus status ;
     @Column (name =  "create_time" , updatable = false )      @Temporal (TemporalType. TIMESTAMP )      @CreationTimestamp      private Date createTime ;
     @Column (name =  "update_time" )      @Temporal (TemporalType. TIMESTAMP )      @UpdateTimestamp      private Date updateTime ;

@Entity : 對映資料庫實體類 @Table (name = "S_PRODUCEINFO" ) : 表名為  "S_PRODUCEINFO" @Id : 宣告主鍵ID @Column (name = "app_name" , unique = true , length = 50) :對應資料庫欄位,屬性 @Enumerated (EnumType. STRING ) : 採用列舉值型別和資料庫欄位進行互動   @Temporal : 時間格式 對映資料庫會得到規定時間格式的日期        @Enumerted(EnumType.STRING)  HH:MM:SS 格式的日期        @Enumerted(EnumType.DATE) 獲取年月日  yyyy-MM-dd 

       @Enumerted(EnumType.TIME) 獲取時分秒  HH:MM:SS

                                               -------Mr.Ge提供以上資源