An annotation is a form of metadata, that can be added to Java source code. Classes, methods, variables, parameters and packages may be annotated. Annotations have no direct effect on the operation of the code they annotate.html
可以添加到 Java 源代碼的語法元數據。類、方法、變量、參數、包均可以被註解,可用來將信息元數據與程序元素進行關聯。Annotation 中文常譯爲「註解」java
Annotations have a number of uses, among them:maven
Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.工具
Compile-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.this
Runtime processing — Some annotations are available to be examined at runtime.code
a. 標記,用於告訴編譯器一些信息orm
b. 編譯時動態處理,如動態生成代碼xml
c. 運行時動態處理,如獲得註解信息htm
Java註解能夠用在構建期。當構建咱們的工程時,構建進程會編譯源碼、生成xml文件,打包編譯後的代碼和文件到jar包。構建過程通常由構建工具自動完成,經常使用的構建工具備ant、maven。構建工具在構建時會自動掃描咱們的代碼,當遇到構建期註解時,會根據註解的內容生成源碼或者其它文件進程
Annotations提供一些原本不屬於程序的數據,好比:一段代碼的做者或者告訴編譯器禁止一些特殊的錯誤。An annotation 對代碼的執行沒有什麼影響。Annotations使用@annotation的形式應用於代碼:類(class),屬性(attribute),方法(method)等等。一個Annotation出如今上面提到的開始位置,並且通常只有一行,也能夠包含有任意的參數
Annotation解析package javaeetutorial.hello1;
import javax.enterprise.context.RequestScoped; import javax.inject.Named; @Named @RequestScoped public class Hello { private String name; public Hello() { } public String getName() { return name; } public void setName(String user_name) { this.name = user_name; } }
在上面的代碼中,Hello類叫作管理bean類,它爲facelets頁面表達式所使用的name屬性提供了getter和setter方法,默認該facelets頁面表達式引用的是Hello類的名字,不過第一個字母是小寫字母(例如:hello.name)。
若是你使用的是默認的bean類的類名,你註解能夠用@Model來替代@Named和@RequestScoped。@Model註釋稱爲原型,是一個包含其餘註釋的註釋的術語。
在 Hello.java類中,註解javax.inject.Named和javax.enterprise.context.RequestScoped使用請求scope來標識Hello類爲管理bean類。scope定義應用程序數據是如何保存和共享的。
在JSF中最經常使用的scope以下:
Request(@RequestScoped):請求scope在Web應用程序中的單個HTTP請求期間仍然存在。像hello1應用,該應用由單個請求和響應組成,bean使用請求scope。
Session (@SessionScoped):會話scope持續存在於Web應用程序中的多個HTTP請求中。當應用程序包含須要維護數據的多個請求和響應時,bean使用會話scope。
Application (@ApplicationScoped):應用程序scope在全部用戶與Web應用程序的交互中持久存在。