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

spring常用註解

http ase cto man proxy tar details aspect 綁定

原文鏈接:http://blog.csdn.net/sudiluo_java/article/details/51858978

Spring 註解學習

聲明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 項目的核心註解 主要目的是開啟自動配置
    • @SpringBootApplication註解是一個組合註解,主要組合了@Configuration .+@EnableAutoConfiguration.+@ComponentScan
  • @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 publicclassProduceInfoEntity { @Id @Column(name = "app_name", unique =true, length = 50) privateStringname; @Column(name = "status") @Enumerated(EnumType.STRING) privateProduceStatusstatus; @Column(name = "create_time", updatable =false) @Temporal(TemporalType.TIMESTAMP) @CreationTimestamp privateDatecreateTime; @Column(name = "update_time") @Temporal(TemporalType.TIMESTAMP) @UpdateTimestamp privateDateupdateTime;
@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提供以上資源

spring常用註解