Spring 各種注解備注


Spring 各種注解備注

96 
felix_feng 
2016.12.28 10:34* 字數 2092 閱讀 790評論 0

轉載 (http://blog.csdn.net/sudiluo_java/article/details/51858978

@ConditionalOnBean 配置了某個特定Bean
@ConditionalOnMissingBean 沒有配置特定的Bean
@ConditionalOnClass Classpath里有指定的類
@ConditionalOnMissingClass Classpath里缺少指定的類
@ConditionalOnExpression 給定的Spring Expression Language(SpEL)表達式計算結果為true
@ConditionalOnJava Java的版本匹配特定值或者一個范圍值
@ConditionalOnJndi 參數中給定的JNDI位置必須存在一個,如果沒有給參數,則要有JNDI
InitialContext
@ConditionalOnProperty 指定的配置屬性要有一個明確的值
@ConditionalOnResource Classpath里有指定的資源
@ConditionalOnWebApplication 這是一個Web應用程序
@ConditionalOnNotWebApplication 這不是一個Web應用程序

聲明Bean的注解:
@Component : 組件,沒有明確的角色
@Service : 在業務邏輯層(service層)使用
@Repository : 在數據訪問層(dao層)使用.
@Controller : 在展現層(MVC--SpringMVC)使用

注入Bean的注解:
@Aautowired : Spring提供的注解.
@Inject : JSR-330提供的注解
@Resource : JSR-250提供的注解
@Primary 注入時優先使用此注解下的實現類,對應org.springframework.beans.factory.NoUniqueBeanDefinitionException異常

配置文件的注解:
@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
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提供以上資源


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM