Spring 各種注解備注

轉(zhuǎn)載 (http://blog.csdn.net/sudiluo_java/article/details/51858978

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

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

注入Bean的注解:
@Aautowired : Spring提供的注解.
@Inject : JSR-330提供的注解
@Resource : JSR-250提供的注解
@Primary 注入時(shí)優(yōu)先使用此注解下的實(shí)現(xiàn)類,對應(yīng)org.springframework.beans.factory.NoUniqueBeanDefinitionException異常

配置文件的注解:
@Configuration : 聲明當(dāng)前類是個(gè)配置類,相當(dāng)于一個(gè)Spring配置的xml文件.
@ComponentScan (cn.test.demo): 自動掃描包名下所有使用 @Component @Service @Repository @Controller 的類,并注冊為Bean
@WiselyConfiguration : 組合注解 可以替代 @Configuration和@ComponentScan
@Bean : 注解在方法上,聲明當(dāng)前方法的返回值為一個(gè)Bean.
@Bean(initMethod="aa",destroyMethod="bb")--> 指定 aa和bb方法在構(gòu)造之后.Bean銷毀之前執(zhí)行.

AOP切面編程注解:
@Aspect : 聲明這是一個(gè)切面
@After @Before. @Around 定義切面,可以直接將攔截規(guī)則(切入點(diǎn) PointCut)作為參數(shù)
@PointCut : 專門定義攔截規(guī)則 然后在 @After @Before. @Around 中調(diào)用
@Transcational : 事務(wù)處理
@Cacheable : 數(shù)據(jù)緩存
@EnableAaspectJAutoProxy : 開啟Spring 對 這個(gè)切面(Aspect )的支持
@Target (ElementType.TYPE):元注解,用來指定注解修飾類的那個(gè)成員 -->指定攔截規(guī)則
@Retention(RetentionPolicy.RUNTIME)
--->當(dāng)定義的注解的@Retention為RUNTIME時(shí),才能夠通過運(yùn)行時(shí)的反射機(jī)制來處理注解.-->指定攔截規(guī)則

Spring 常用配置:
@import :導(dǎo)入配置類
@Scope : 新建Bean的實(shí)例 @Scope("prototype") 聲明Scope 為 Prototype
@Value : 屬性注入
@Value ("我愛你") --> 普通字符串注入
@Value ("#{systemProperties['os.name']}") -->注入操作系統(tǒng)屬性
@Value ("#{ T (java.lang.Math).random() * 100.0 }") --> 注入表達(dá)式結(jié)果
@Value ("#{demoService.another}") --> 注入其他Bean屬性
@Value ( "classpath:com/wisely/highlight_spring4/ch2/el/test.txt" ) --> 注入文件資源
@Value ("http://www.baidu.com")-->注入網(wǎng)址資源
@Value ("${book.name}" ) --> 注入配置文件 注意: 使用的是$ 而不是 #

@PostConstruct : 在構(gòu)造函數(shù)執(zhí)行完之后執(zhí)行
@PreDestroy : 在 Bean 銷毀之前執(zhí)行
@ActiveProfiles : 用來聲明活動的 profile
@profile: 為不同環(huán)境下使用不同的配置提供了支持
@Profile("dev") .......對方法名為 dev-xxxx的方法提供實(shí)例化Bean

@EnableAsync : 開啟異步任務(wù)的支持(多線程)
@Asyns : 聲明這是一個(gè)異步任務(wù),可以在類級別 和方法級別聲明.
@EnableScheduling : 開啟對計(jì)劃任務(wù)的支持(定時(shí)器)
@Scheduled : 聲明這是一個(gè)計(jì)劃任務(wù) 支持多種計(jì)劃任務(wù),包含 cron. fixDelay fixRate
@Scheduled (dixedDelay = 5000) 通過注解 定時(shí)更新

@Conditional : 條件注解,根據(jù)滿足某一特定條件創(chuàng)建一個(gè)特定的Bean
@ContextConfiguration : 加載配置文件
@ContextConfiguration(classes = {TestConfig.class})
@ContextConfiguration用來加載ApplicationContext
classes屬性用來加載配置類

@WebAppCofiguration : 指定加載 ApplicationContext是一個(gè)WebApplicationContext

@Enable注解:*
@EnableAsync : 開啟異步任務(wù)的支持(多線程)
@EnableScheduling : 開啟對計(jì)劃任務(wù)的支持(定時(shí)器)
@EnableWebMVC : 開啟對Web MVC 的配置支持
@EnableAaspectJAutoProxy : 開啟Spring 對 這個(gè)切面(Aspect )的支持
@EnableConfigurationProperties 開啟對@ConfigurationProperties注解配置Bean的支持
@EnableJpaRepositories : 開啟對Spring Data JAP Repository 的支持
@EnableTransactionManagement 開啟對注解式事物的支持
@EnableCaching開啟注解是緩存的支持.
@EnableDiscoveryClient 讓服務(wù)發(fā)現(xiàn)服務(wù)器,使用服務(wù)器.Spring cloud 實(shí)現(xiàn)服務(wù)發(fā)現(xiàn)
@EnableEurekaServer 注冊服務(wù)器 spring cloud 實(shí)現(xiàn)服務(wù)注冊@
@EnableScheduling 讓spring可以進(jìn)行任務(wù)調(diào)度,功能類似于spring.xml文件中的命名空間<task:*>
@EnableCaching 開啟Cache緩存支持;

SpringMVC 常用注解:
@Controller : 注解在類上 聲明這個(gè)類是springmvc里的Controller,將其聲明為一個(gè)spring的Bean.
@RequestMapping :可以注解在類上和方法上 映射WEB請求(訪問路徑和參數(shù))
@RequestMapping(value= "/convert",produces+{"application/x-wisely"}) 設(shè)置訪問URL 返回值類型

@ResponseBody : 支持將返回值放入response體內(nèi) 而不是返回一個(gè)頁面(返回的是一個(gè)組數(shù)據(jù))
@RequestBody : 允許request的參數(shù)在request體中,而不是直接連接在地址后面 次注解放置在參數(shù)前
@Path Variable : 用來接收路徑參數(shù) 如/test/001,001為參數(shù),次注解放置在參數(shù)前
@RestController : @Controller + @ResponseBody 組合注解
@ControllerAdvice : 通過@ControllerAdvice可以將對已控制器的全局配置放置在同一個(gè)位置
@ExceptionHandler : 用于全局處理控制器的異常
@ExceptionHandier(value=Exception.class) -->通過value屬性可過濾攔截器條件,攔截所有的異常

@InitBinder : 用來設(shè)置WebDataBinder , WebDataBinder用來自動綁定前臺請求參數(shù)到Model中.
@ModelAttrbuute : 綁定鍵值對到Model中,
@RunWith : 運(yùn)行器
@RunWith(JUnit4.class)就是指用JUnit4來運(yùn)行
@RunWith(SpringJUnit4ClassRunner.class),讓測試運(yùn)行于Spring測試環(huán)境
@RunWith(Suite.class)的話就是一套測試集合,

@WebAppConfiguration("src/main/resources") : 注解在類上,用來聲明加載的ApplicationContex 是一個(gè)WebApplicationContext ,它的屬性指定的是Web資源的位置,默認(rèn)為 src/main/webapp ,自定義修改為 resource

@Before : 在 xxx 前初始化

Spring Boot 注解:
@SpringBootApplication : 是Spring Boot 項(xiàng)目的核心注解 主要目的是開啟自動配置
@SpringBootApplication注解是一個(gè)組合注解,主要組合了@Configuration .+@EnableAutoConfiguration.+@ComponentScan

@Value : 屬性注入,讀取properties或者 Yml 文件中的屬性
@ConfigurationProperties : 將properties屬性和一個(gè)Bean及其屬性關(guān)聯(lián),從而實(shí)現(xiàn)類型安全的配置
@ConfigurationProperties(prefix = "author",locations = {"classpath:config/author.properties"})
通過@ConfigurationProperties加載配置,通過prefix屬性指定配置前綴,通過location指定配置文件位置

@EnableAutoConfiguration 注解:作用在于讓 Spring Boot 根據(jù)應(yīng)用所聲明的依賴來對 Spring 框架進(jìn)行自動配置 這個(gè)注解告訴Spring Boot根據(jù)添加的jar依賴猜測你想如何配置Spring。由于spring-boot-starter-web
添加了Tomcat和Spring MVC,所以auto-configuration將假定你正在開發(fā)一個(gè)web應(yīng)用并相應(yīng)地對Spring進(jìn)行設(shè)置。
@ Configuration 注解,以明確指出該類是 Bean 配置的信息源
@ComponentScan 注解會告知Spring掃描指定的包來初始化Spring Bean這能夠確保我們聲明的Bean能夠被發(fā)現(xiàn)。
@ImportResource 注解加載XML配置文件
@EnableAutoConfiguration (exclude={xxxx.class}) 禁用特定的自動配置
@SpringBootApplication 注解等價(jià)于以默認(rèn)屬性使用 @Configuration,@EnableAutoConfiguration和 @ComponentScan。

@SuppressWarnings注解
@SuppressWarnings("unchecked")
告訴編譯器忽略 unchecked 警告信息,如使用 list ArrayList等未進(jìn)行參數(shù)化產(chǎn)生的警告信息

@SuppressWarnings("serial")
如果編譯器出現(xiàn)這樣的警告信息: The serializable class WmailCalendar does not declare a static final serialVersionUID field of type long 使用這個(gè)注釋將警告信息去掉。

@SuppressWarnings("deprecation")
如果使用了使用@Deprecated注釋的方法,編譯器將出現(xiàn)警告信息。使用這個(gè)注釋將警告信息去掉。

@SuppressWarnings("unchecked", "deprecation")
告訴編譯器同時(shí)忽略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 : 映射數(shù)據(jù)庫實(shí)體類
@Table(name = "S_PRODUCEINFO" ) : 表名為 "S_PRODUCEINFO"
@Id : 聲明主鍵ID
@Column(name = "app_name", unique = true, length = 50) :對應(yīng)數(shù)據(jù)庫字段,屬性
@Enumerated(EnumType. STRING) : 采用枚舉值類型和數(shù)據(jù)庫字段進(jìn)行交互
@Temporal : 時(shí)間格式 映射數(shù)據(jù)庫會得到規(guī)定時(shí)間格式的日期
@Enumerted(EnumType.STRING) HH:MM:SS 格式的日期
@Enumerted(EnumType.DATE) 獲取年月日 yyyy-MM-dd
@Enumerted(EnumType.TIME) 獲取時(shí)分秒 HH:MM:SS
-------Mr.Ge提供以上資源

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,724評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,290評論 6 342
  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan閱讀 4,518評論 2 7
  • 什么是Spring Spring是一個(gè)開源的Java EE開發(fā)框架。Spring框架的核心功能可以應(yīng)用在任何Jav...
    jemmm閱讀 16,786評論 1 133
  • 大家好,我是9組的統(tǒng)計(jì)員,時(shí)間過的飛快,我們的孵化營差不多還有一個(gè)星期就完美的結(jié)束了,本周我們的小伙伴們都表現(xiàn)的不...
    你若未央閱讀 400評論 1 6

友情鏈接更多精彩內(nèi)容