1. 程式人生 > >Spring常用註解介紹

Spring常用註解介紹

Spring的一個核心功能是IOC,就是將Bean初始化載入到容器中,Bean是如何載入到容器的,可以使用Spring註解方式或者Spring XML配置方式。
Spring註解方式減少了配置檔案內容,更加便於管理,並且使用註解可以大大提高了開發效率!
下面按照分類講解Spring中常用的一些註解。

一: 元件類註解

思考:Spring怎麼知道應該把哪些Java類當成bean註冊到容器中呢?
答案:使用配置檔案或者註解的方式進行標識需要處理的java!
  
  • 1
  • 2

1、註解類介紹


@Component :標準一個普通的spring Bean類。
@Repository:標註一個DAO元件類。
@Service:標註一個業務邏輯元件類。
@Controller:標註一個控制器元件類。


這些都是註解在平時的開發過程中出鏡率極高,@Component、@Repository、@Service、@Controller實質上屬於同一類註解,用法相同,功能相同,區別在於標識元件的型別。@Component可以代替@Repository、@Service、@Controller,因為這三個註解是被@Component標註的。如下程式碼

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
    String value() default
""; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

這裡寫圖片描述

2、舉例詳解

(1)當一個元件代表資料訪問層(DAO)的時候,我們使用@Repository進行註解,如下

@Repository
public class HappyDaoImpl implements HappyDao{
private final static Logger LOGGER = LoggerFactory.getLogger(HappyDaoImpl .class);
public void  club(){
        //do something ,like drinking and singing
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

(2)當一個元件代表業務層時,我們使用@Service進行註解,如下

@Service(value="goodClubService")
//使用@Service註解不加value ,預設名稱是clubService
public class ClubServiceImpl implements ClubService {
    @Autowired
    private ClubDao clubDao;

    public void doHappy(){
        //do some Happy
    }
 }
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

(3)當一個元件作為前端互動的控制層,使用@Controller進行註解,如下

@Controller
public class HappyController {
    @Autowired //下面進行講解
    private ClubService clubService;

    // Control the people entering the Club
    // do something
}
/*Controller相關的註解下面進行詳細講解,這裡簡單引入@Controller*/
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3、總結注意點


1、被註解的java類當做Bean例項,Bean例項的名稱預設是Bean類的首字母小寫,其他部分不變。@Service也可以自定義Bean名稱,但是必須是唯一的!


2、儘量使用對應元件註解的類替換@Component註解,在spring未來的版本中,@Controller,@Service,@Repository會攜帶更多語義。並且便於開發和維護!


3、指定了某些類可作為Spring Bean類使用後,最好還需要讓spring搜尋指定路徑,在Spring配置檔案加入如下配置:

<!-- 自動掃描指定包及其子包下的所有Bean類 -->
<context:component-scan base-package="org.springframework.*"/>
  
  • 1
  • 2

二:裝配bean時常用的註解

1、註解介紹


@Autowired:屬於Spring 的org.springframework.beans.factory.annotation包下,可用於為類的屬性、構造器、方法進行注值
@Resource:不屬於spring的註解,而是來自於JSR-250位於java.annotation包下,使用該annotation為目標bean指定協作者Bean。
@PostConstruct 和 @PreDestroy 方法 實現初始化和銷燬bean之前進行的操作

2、舉例說明

(1):@Autowired


@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
    boolean required() default true;
}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
@Controller
public class HappyController {
    @Autowired //預設依賴的ClubDao 物件(Bean)必須存在
    //@Autowired(required = false) 改變預設方式
    @Qualifier("goodClubService")
    private ClubService clubService;

    // Control the people entering the Club
    // do something
}

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

(2):@Resource

@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)
public @interface Resource {
 String name() default "";
 Class type() default java.lang.Object.class;
 ...
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
public class AnotationExp {

    @Resource(name = "HappyClient")
    private HappyClient happyClient;

    @Resource(type = HappyPlayAno .class)
    private HappyPlayAno happyPlayAno;
}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3、總結

(1):相同點

@Resource的作用相當於@Autowired,均可標註在欄位或屬性的setter方法上。

(2):不同點

a:提供方 @Autowired是Spring的註解,@Resource是javax.annotation註解,而是來自於JSR-250,J2EE提供,需要JDK1.6及以上。
b :注入方式 @Autowired只按照Type 注入;@Resource預設按Name自動注入,也提供按照Type 注入;
c:屬性
@Autowired註解可用於為類的屬性、構造器、方法進行注值。預設情況下,其依賴的物件必須存在(bean可用),如果需要改變這種預設方式,可以設定其required屬性為false。
還有一個比較重要的點就是,@Autowired註解預設按照型別裝配,如果容器中包含多個同一型別的Bean,那麼啟動容器時會報找不到指定型別bean的異常,解決辦法是結合@Qualified註解進行限定,指定注入的bean名稱。
@Resource有兩個中重要的屬性:name和type。name屬性指定byName,如果沒有指定name屬性,當註解標註在欄位上,即預設取欄位的名稱作為bean名稱尋找依賴物件,當註解標註在屬性的setter方法上,即預設取屬性名作為bean名稱尋找依賴物件。
需要注意的是,@Resource如果沒有指定name屬性,並且按照預設的名稱仍然找不到依賴物件時, @Resource註解會回退到按型別裝配。但一旦指定了name屬性,就只能按名稱裝配了。

d:@Resource註解的使用性更為靈活,可指定名稱,也可以指定型別 ;@Autowired註解進行裝配容易丟擲異常,特別是裝配的bean型別有多個的時候,而解決的辦法是需要在增加@Qualitied進行限定。

Spring中 @Autowired註解與@Resource註解的區別

注意點:使用@Resource也要注意新增配置檔案到Spring,如果沒有配置component-scan

<context:component-scan> 
<!--<context:component-scan>的使用,是預設啟用<context:annotation-config>功能-->
  
  • 1
  • 2

則一定要配置 annotation-config

<context:annotation-config/>
  
  • 1

三:@Component vs @Configuration and @Bean

1、簡單介紹


Spring的官方團隊說@Component可以替代 @Configuration註解,事實上我們看原始碼也可以發現看到,如下

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component  //看這裡!!!
public @interface Configuration {
    String value() default "";

  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

雖然說可以替代但是兩個註解之間還是有區別的!

Bean註解主要用於方法上,有點類似於工廠方法,當使用了@Bean註解,我們可以連續使用多種定義bean時用到的註解,譬如用@Qualifier註解定義工廠方法的名稱,用@Scope註解定義該bean的作用域範圍,譬如是singleton還是prototype等。

Spring 中新的 Java 配置支援的核心就是@Configuration 註解的類。這些類主要包括 @Bean 註解的方法來為 Spring 的 IoC 容器管理的物件定義例項,配置和初始化邏輯。

使用@Configuration 來註解類表示類可以被 Spring 的 IoC 容器所使用,作為 bean 定義的資源。

@Configuration
public class AppConfig {
    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

這和 Spring 的 XML 檔案中的非常類似

<beans>
    <bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
  
  • 1
  • 2
  • 3

@Bean 註解扮演了和元素相同的角色。

2、舉例說明@Component 和 @Configuration

@Configuration
public static class Config {

    @Bean
    public SimpleBean simpleBean() {
        return new SimpleBean();
    }

    @Bean
    public SimpleBeanConsumer simpleBeanConsumer() {
        return new SimpleBeanConsumer(simpleBean());
    }
}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
@Component
public static class Config {

    @Bean
    public SimpleBean simpleBean() {
        return new SimpleBean();
    }

    @Bean
    public SimpleBeanConsumer simpleBeanConsumer() {
        return new SimpleBeanConsumer(simpleBean());
    }
}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

第一個程式碼正常工作,正如預期的那樣,SimpleBeanConsumer將會得到一個單例SimpleBean的連結。第二個配置是完全錯誤的,因為Spring會建立一個SimpleBean的單例bean,但是SimpleBeanConsumer將獲得另一個SimpleBean例項(也就是相當於直接呼叫new SimpleBean() ,這個bean是不歸Spring管理的),既new SimpleBean() 例項是Spring上下文控制元件之外的。

3、原因總結


使用@ configuration,所有標記為@ bean的方法將被包裝成一個CGLIB包裝器,它的工作方式就好像是這個方法的第一個呼叫,那麼原始方法的主體將被執行,最終的物件將在spring上下文中註冊。所有進一步的呼叫只返回從上下文檢索的bean。

在上面的第二個程式碼塊中,新的SimpleBeanConsumer(simpleBean())只調用一個純java方法。為了糾正第二個程式碼塊,我們可以這樣做

@Component
public static class Config {
    @Autowired
    SimpleBean simpleBean;

    @Bean
    public SimpleBean simpleBean() {
        return new SimpleBean();
    }

    @Bean
    public SimpleBeanConsumer simpleBeanConsumer() {
        return new SimpleBeanConsumer(simpleBean);
    }
}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

Spring @Configuration vs @Component
基本概念:@Configuration 和@Bean

四:spring MVC模組註解

1、web模組常用到的註解

  • @Controller :表明該類會作為與前端作互動的控制層元件,通過服務介面定義的提供訪問應用程式的一種行為,解釋使用者的輸入,將其轉換成一個模型然後將試圖呈獻給使用者。
@Controller
public class HappyController {
    //do something
...
}
  
  • 1
  • 2
  • 3
  • 4
  • 5

Spring MVC 使用 @Controller 定義控制器,它還允許自動檢測定義在類路徑下的元件(配置檔案中配置掃描路徑)並自動註冊。

  • @RequestMapping : 這個註解用於將url對映到整個處理類或者特定的處理請求的方法。可以只用萬用字元!
@Controller
@RequestMapping("/happy")
public class HappyController  {

  @Autowired
  private HappyService happyService;

  @RequestMapping(/hello/*)
  public void sayHello(){
        //請求為 /happy/hello/* 都會進入這個方法!
        //例如:/happy/hello/123   /happy/hello/adb
        //可以通過get/post 請求
  }
  @RequestMapping(value="/haha",method=RequestMethod.GET)
  public void sayHaHa(){
  //只能通過get請求
  }
...
}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

@RequestMapping 既可以作用在類級別,也可以作用在方法級別。當它定義在類級別時,標明該控制器處理所有的請求都被對映到 /favsoft 路徑下。@RequestMapping中可以使用 method 屬性標記其所接受的方法型別,如果不指定方法型別的話,可以使用 HTTP GET/POST 方法請求資料,但是一旦指定方法型別,就只能使用該型別獲取資料。

  • @RequestParam :將請求的引數繫結到方法中的引數上,有required引數,預設情況下,required=true,也就是改引數必須要傳。如果改引數可以傳可不傳,可以配置required=false。
 @RequestMapping("/happy")
  public String sayHappy(
  @RequestParam(value = "name", required = false) String name,
  @RequestParam(value = "age", required = true) String age) {
  //age引數必須傳 ,name可傳可不傳
  ...
  }
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • @PathVariable : 該註解用於方法修飾方法引數,會將修飾的方法引數變為可供使用的uri變數(可用於動態繫結)。
@RequestMapping(value="/happy/{dayid}",method=RequestMethod.GET)
public String findPet(@PathVariable String dayid, Model mode) {
//使用@PathVariable註解繫結 {dayid} 到String dayid
}
  
  • 1
  • 2
  • 3
  • 4

@PathVariable中的引數可以是任意的簡單型別,如int, long, Date等等。Spring會自動將其轉換成合適的型別或者丟擲 TypeMismatchException異常。當然,我們也可以註冊支援額外的資料型別。
@PathVariable支援使用正則表示式,這就決定了它的超強大屬性,它能在路徑模板中使用佔位符,可以設定特定的字首匹配,字尾匹配等自定義格式。

  • @RequestBody : @RequestBody是指方法引數應該被繫結到HTTP請求Body上。
@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body@RequestBody User user){
   //可以繫結自定義的物件型別
}
  
  • 1
  • 2
  • 3
  • 4
  • @ResponseBody : @ResponseBody與@RequestBody類似,它的作用是將返回型別直接輸入到HTTP response body中。
    @ResponseBody在輸出JSON格式的資料時,會經常用到。
@RequestMapping(value = "/happy", method =RequestMethod.POST)
@ResponseBody
public String helloWorld() {    
return "Hello World";//返回String型別
}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • @RestController :控制器實現了REST的API,只為服務於JSON,XML或其它自定義的型別內容,@RestController用來建立REST型別的控制器,與@Controller型別。@RestController就是這樣一種型別,它避免了你重複的寫@RequestMapping與@ResponseBody。

  • @ModelAttribute :@ModelAttribute可以作用在方法或方法引數上,當它作用在方法上時,標明該方法的目的是新增一個或多個模型屬性(model attributes)。
    該方法支援與@RequestMapping一樣的引數型別,但並不能直接對映成請求。控制器中的@ModelAttribute方法會在@RequestMapping方法呼叫之前而呼叫。

@ModelAttribute方法有兩種風格:一種是新增隱形屬性並返回它。另一種是該方法接受一個模型並新增任意數量的模型屬性。使用者可以根據自己的需要選擇對應的風格。

五:Spring事務模組註解

1、常用到的註解


在處理dao層或service層的事務操作時,譬如刪除失敗時的回滾操作。使用 @Transactional 作為註解,但是需要在配置檔案啟用

<!-- 開啟註解方式宣告事務 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
  
  • 1
  • 2

2、舉例

@Service
public class CompanyServiceImpl implements CompanyService {
  @Autowired
  private CompanyDAO companyDAO;

  @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
  public int deleteByName(String name) {

    int result = companyDAO.deleteByName(name);
    return result;
  }
  ...
}
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3、總結

事務的傳播機制和隔離機制比較重要!

事務傳播行為型別 說明
PROPAGATION_REQUIRED 如果當前沒有事務,就新建一個事務,如果已經存在一個事務中,加入到這個事務中。這是最常見的選擇。
PROPAGATION_SUPPORTS 支援當前事務,如果當前沒有事務,就以非事務方式執行。
PROPAGATION_MANDATORY 使用當前的事務,如果當前沒有事務,就丟擲異常。
PROPAGATION_REQUIRES_NEW 新建事務,如果當前存在事務,把當前事務掛起。
PROPAGATION_NOT_SUPPORTED 以非事務方式執行操作,如果當前存在事務,就把當前事務掛起。
PROPAGATION_NEVER 以非事務方式執行,如果當前存在事務,則丟擲異常
PROPAGATION_NESTED 如果當前存在事務,則在巢狀事務內執行。如果當前沒有事務,則執行與PROPAGATION_REQUIRED類 似的操作

一圖學習 Spring事務傳播性


readOnly : 事務的讀寫屬性,取true或者false,true為只讀、預設為false
rollbackFor : 回滾策略,當遇到指定異常時回滾。譬如上例遇到異常就回滾
timeout (補充的) : 設定超時時間,單位為秒
isolation : 設定事務隔離級別,列舉型別,一共五種

型別 說明
DEFAULT 採用資料庫預設隔離級別
READ_UNCOMMITTED 讀未提交的資料(會出現髒讀取)
READ_COMMITTED 讀已提交的資料(會出現幻讀,即前後兩次讀的不一樣)
REPEATABLE_READ 可重複讀,會出現幻讀
SERIALIZABLE 序列化 (對資源消耗較大,一般不使用)


透徹的掌握 Spring 中@transactional 的使用


Spring事務配置及事務的傳播性與隔離級別詳解

參考博文

spring 常用註解
詳解Spring MVC 4常用的那些註解

史上最全最強SpringMVC詳細示例實戰教程