天天看點

Spring 各種注解備注

Spring 各種注解備注

Spring 各種注解備注

felix_feng 關注

2016.12.28 10:34* 字數 2092 閱讀 790評論 0喜歡 6

轉載 (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提供以上資源

Spring 各種注解備注