關於java專案中用到的註解
阿新 • • 發佈:2019-01-10
[email protected]
使用在一個方法上面,一般在Controller裡面的方法中,都會使用@RequestMapping來定位這個方法的,然後我們一般返回都是一個url,比如return'index',表示返回index.jsp/html頁面,而如果我們在方法上面加上@responsebody,那麼返回的資料就不會被解析成為跳轉的url路徑,而是直接寫入HTTP responsebody中。比如非同步獲取json資料,加上@responsebody後,會直接返回json資料。
[email protected]
該註解用於讀取request請求的body部分資料,使用系統預設配置的HttpMessageConverter進行解析,然後把相應的資料繫結到要返回的物件上面。註解用於將Controller的方法引數,根據HTTP Request Header的content-Type的內容,通過適當的HttpMessageConverter轉換為JAVA類,使用在POST或者PUT的資料是JSON格式或者XML格式,而不是普通的鍵值對形式.
[email protected]
用來在Controller作定位用的:eg:@RequestMapping("/admin/log/")
[email protected]/@resource
1)@Autowired與@Resource都可以用來裝配bean.都可以寫在欄位上,或寫在setter方法上。
2)@Autowired預設按型別裝配(這個註解是屬業spring的),預設情況下必須要求依賴物件必須存在,如果要允許null 值,可以設定它的required屬性為false,如:@Autowired(required=false) ,如果我們想使用名稱裝配可以結合@Qualifier註解進行使用,如下:
@Autowired() @Qualifier("baseDao")
private BaseDao baseDao;
3)@Resource(這個註解屬於J2EE的),預設安照名稱進行裝配,名稱可以通過name屬性進行指定,
如果沒有指定name屬性,當註解寫在欄位上時,預設取欄位名進行按照名稱查詢,如果註解寫在setter方法上預設取屬性名進行裝配。 當找不到與名稱匹配的bean時才按照型別進行裝配。但是需要注意的是,如果name屬性一旦指定,就只會按照名稱進行裝配。
@Resource(name="baseDao")
private BaseDao baseDao;
5.java自定義註解
元註解
元註解是指註解的註解:@Retention @Target @Document @Inherited四種。
1)
@Retention: 定義註解的保留策略
@Retention(RetentionPolicy.SOURCE) //註解僅存在於原始碼中,在class位元組碼檔案中不包含
@Retention(RetentionPolicy.CLASS) //預設的保留策略,註解會在class位元組碼檔案中存在,但執行時無法獲得,
@Retention(RetentionPolicy.RUNTIME) //註解會在class位元組碼檔案中存在,在執行時可以通過反射獲取到
2)
@Target:定義註解的作用目標
原始碼:
@Target(ElementType.TYPE) //介面、類、列舉、註解
@Target(ElementType.FIELD) //欄位、列舉的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法引數
@Target(ElementType.CONSTRUCTOR) //建構函式
@Target(ElementType.LOCAL_VARIABLE)//區域性變數
@Target(ElementType.ANNOTATION_TYPE)//註解
@Target(ElementType.PACKAGE) ///包
由以上的原始碼可以知道,他的elementType 可以有多個,一個註解可以為類的,方法的,欄位的等等
3) @Document:說明該註解將被包含在javadoc中
4) @Inherited:說明子類可以繼承父類中的該註解
6.spring-AOP
@aspect申明一個切面類
列印資訊:
方法before前執行.....
方法環繞start.....
我看.....................
方法after執行.....
方法環繞end.....
方法afterReurning執行.....
7.spring註解
@Component 是一個泛化的概念,僅僅表示一個元件 (Bean) ,可以作用在任何層次。
@Service 通常作用在業務層,但是目前該功能與 @Component 相同。
@Constroller 通常作用在控制層,但是目前該功能與 @Component 相同。
@Repository註解便屬於最先引入的一批,它用於將資料訪問層 (DAO 層 ) 的類標識為 Spring Bean。具體只需將該註解標註在 DAO類上即可。同時,為了讓 Spring 能夠掃描類路徑中的類並識別出 @Repository 註解,需要在 XML 配置檔案中啟用Bean 的自動掃描功能,這可以通過<context:component-scan/>實現。如下所示:
// 首先使用 @Repository 將 DAO 類宣告為 Bean
package bookstore.dao;
@Repository
public class UserDaoImpl implements UserDao{ …… }
// 其次,在 XML 配置檔案中啟動 Spring 的自動掃描功能
<beans … >
……
<context:component-scan base-package=”bookstore.dao” />
……
</beans>
[email protected]去除警告
[email protected]
@Import註解就是之前xml配置中的import標籤,可以用於依賴第三方包中bean的配置和載入
在4.2之前只支援匯入配置類
在4.2,@Import註解支援匯入普通的java類,並將其宣告成一個bean
eg:
@Configuration
@Import(DemoService.class)//在spring 4.2之前是不不支援的
public class DemoConfig {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com..example");
DemoService ds = context.getBean(DemoService.class);
ds.doSomething();
}
}
[email protected]
Configuration 是 Spring 3.X 後提供的註解,用於取代 XML 來配置 Spring
@Configuration一般被用來初始化配置,有兩種方法可以使帶有@Configuration的類被初始化,一為讓把類所在包的路徑納入scanBasePackages,這樣就進入了Spring的掃描範圍;還有一種方法就是在spring.factories中用org.springframework.boot.autoconfigure.EnableAutoConfiguration=類的全路徑名,這樣在專案啟動的時候SpringFactoriesLoader會初始化spring.factories(包括引入的jar包中的)中配置的類。在spring.factories配置的好處就是,如果我們想開發一個jar把供其他人使用,那麼我們就在自己工程的spring.factories中配置@@Configuration類,這樣只要其他人在他們專案的POM中加入了我們開發的jar包作為依賴,在他們專案啟動的時候就會初始化我們開發的工程中的類。
使用在一個方法上面,一般在Controller裡面的方法中,都會使用@RequestMapping來定位這個方法的,然後我們一般返回都是一個url,比如return'index',表示返回index.jsp/html頁面,而如果我們在方法上面加上@responsebody,那麼返回的資料就不會被解析成為跳轉的url路徑,而是直接寫入HTTP responsebody中。比如非同步獲取json資料,加上@responsebody後,會直接返回json資料。
[email protected]
該註解用於讀取request請求的body部分資料,使用系統預設配置的HttpMessageConverter進行解析,然後把相應的資料繫結到要返回的物件上面。註解用於將Controller的方法引數,根據HTTP Request Header的content-Type的內容,通過適當的HttpMessageConverter轉換為JAVA類,使用在POST或者PUT的資料是JSON格式或者XML格式,而不是普通的鍵值對形式.
用來在Controller作定位用的:eg:@RequestMapping("/admin/log/")
[email protected]/@resource
1)@Autowired與@Resource都可以用來裝配bean.都可以寫在欄位上,或寫在setter方法上。
2)@Autowired預設按型別裝配(這個註解是屬業spring的),預設情況下必須要求依賴物件必須存在,如果要允許null 值,可以設定它的required屬性為false,如:@Autowired(required=false) ,如果我們想使用名稱裝配可以結合@Qualifier註解進行使用,如下:
@Autowired() @Qualifier("baseDao")
private BaseDao baseDao;
3)@Resource(這個註解屬於J2EE的),預設安照名稱進行裝配,名稱可以通過name屬性進行指定,
如果沒有指定name屬性,當註解寫在欄位上時,預設取欄位名進行按照名稱查詢,如果註解寫在setter方法上預設取屬性名進行裝配。 當找不到與名稱匹配的bean時才按照型別進行裝配。但是需要注意的是,如果name屬性一旦指定,就只會按照名稱進行裝配。
@Resource(name="baseDao")
private BaseDao baseDao;
5.java自定義註解
元註解是指註解的註解:@Retention @Target @Document @Inherited四種。
1)
@Retention: 定義註解的保留策略
@Retention(RetentionPolicy.SOURCE) //註解僅存在於原始碼中,在class位元組碼檔案中不包含
@Retention(RetentionPolicy.CLASS) //預設的保留策略,註解會在class位元組碼檔案中存在,但執行時無法獲得,
@Retention(RetentionPolicy.RUNTIME) //註解會在class位元組碼檔案中存在,在執行時可以通過反射獲取到
2)
@Target:定義註解的作用目標
原始碼:
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Target { ElementType[] value(); }
@Target(ElementType.TYPE) //介面、類、列舉、註解
@Target(ElementType.FIELD) //欄位、列舉的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法引數
@Target(ElementType.CONSTRUCTOR) //建構函式
@Target(ElementType.LOCAL_VARIABLE)//區域性變數
@Target(ElementType.ANNOTATION_TYPE)//註解
@Target(ElementType.PACKAGE) ///包
由以上的原始碼可以知道,他的elementType 可以有多個,一個註解可以為類的,方法的,欄位的等等
3) @Document:說明該註解將被包含在javadoc中
4) @Inherited:說明子類可以繼承父類中的該註解
6.spring-AOP
@aspect申明一個切面類
@Aspect
public class AspectStyle {
配置切面點
@Pointcut("execution(* com.sxit..*.*(..))")
public void init(){
}
@Before(value="init()")
public void before(){
System.out.println("方法執行前執行.....");
}
@AfterReturning(value="init()")
public void afterReturning(){
System.out.println("方法執行完執行.....");
}
@AfterThrowing(value="init()")
public void throwss(){
System.out.println("方法異常時執行.....");
}
@After(value="init()")
public void after(){
System.out.println("方法最後執行.....");
}
@Around(value="init()")
public Object around(ProceedingJoinPoint pjp){
System.out.println("方法環繞start.....");
Object o = null;
try {
o = pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("方法環繞end.....");
return o;
}
}
列印資訊:
方法before前執行.....
方法環繞start.....
我看.....................
方法after執行.....
方法環繞end.....
方法afterReurning執行.....
7.spring註解
@Component 是一個泛化的概念,僅僅表示一個元件 (Bean) ,可以作用在任何層次。
@Service 通常作用在業務層,但是目前該功能與 @Component 相同。
@Constroller 通常作用在控制層,但是目前該功能與 @Component 相同。
@Repository註解便屬於最先引入的一批,它用於將資料訪問層 (DAO 層 ) 的類標識為 Spring Bean。具體只需將該註解標註在 DAO類上即可。同時,為了讓 Spring 能夠掃描類路徑中的類並識別出 @Repository 註解,需要在 XML 配置檔案中啟用Bean 的自動掃描功能,這可以通過<context:component-scan/>實現。如下所示:
// 首先使用 @Repository 將 DAO 類宣告為 Bean
package bookstore.dao;
@Repository
public class UserDaoImpl implements UserDao{ …… }
// 其次,在 XML 配置檔案中啟動 Spring 的自動掃描功能
<beans … >
……
<context:component-scan base-package=”bookstore.dao” />
……
</beans>
[email protected]去除警告
[email protected]
@Import註解就是之前xml配置中的import標籤,可以用於依賴第三方包中bean的配置和載入
在4.2之前只支援匯入配置類
在4.2,@Import註解支援匯入普通的java類,並將其宣告成一個bean
eg:
@Configuration
@Import(DemoService.class)//在spring 4.2之前是不不支援的
public class DemoConfig {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com..example");
DemoService ds = context.getBean(DemoService.class);
ds.doSomething();
}
}
[email protected]
Configuration 是 Spring 3.X 後提供的註解,用於取代 XML 來配置 Spring
@Configuration一般被用來初始化配置,有兩種方法可以使帶有@Configuration的類被初始化,一為讓把類所在包的路徑納入scanBasePackages,這樣就進入了Spring的掃描範圍;還有一種方法就是在spring.factories中用org.springframework.boot.autoconfigure.EnableAutoConfiguration=類的全路徑名,這樣在專案啟動的時候SpringFactoriesLoader會初始化spring.factories(包括引入的jar包中的)中配置的類。在spring.factories配置的好處就是,如果我們想開發一個jar把供其他人使用,那麼我們就在自己工程的spring.factories中配置@@Configuration類,這樣只要其他人在他們專案的POM中加入了我們開發的jar包作為依賴,在他們專案啟動的時候就會初始化我們開發的工程中的類。