天天看點

Spring注解@Component、@Repository、@Service、@Controller

很長時間沒做web項目都把以前學的那點架構知識忘光了,今天把以前做的一個項目翻出來看一下發現用·@Component标記一個元件,而網上有的用@Service标記元件,我暈就查了一下資料:

Spring 2.5 中除了提供 @Component 注釋外,還定義了幾個擁有特殊語義的注釋,它們分别是:@Repository、@Service 和 @Controller。

在目前的 Spring 版本中,這 3 個注釋和 @Component 是等效的,但是從注釋類的命名上,很容易看出這 3 個注釋分别和持久層、業務層和控制層(Web 層)相對應。

雖然目前這3 個注釋和 @Component 相比沒有什麼新意,但 Spring 将在以後的版本中為它們添加特殊的功能。

是以,如果 Web 應用程式采用了經典的三層分層結構的話,最好在持久層、業務層和控制層分别采用上述注解對分層中的類進行注釋。

@Service用于标注業務層元件

@Controller用于标注控制層元件(如struts中的action)

@Repository用于标注資料通路元件,即DAO元件

@Component泛指元件,當元件不好歸類的時候,我們可以使用這個注解進行标注。

[java] view plain copy

  1. @Service  
  2. public class VentorServiceImpl implements iVentorService {     
  3. }  
  4. @Repository  
  5. public class VentorDaoImpl implements iVentorDao {   
  6. }  

在一個稍大的項目中,如果元件采用xml的bean定義來配置,顯然會增加配置檔案的體積,查找以及維護起來也不太友善。

Spring2.5為我們引入了元件自動掃描機制,他在類路徑下尋找标注了上述注解的類,并把這些類納入進spring容器中管理。

它的作用和在xml檔案中使用bean節點配置元件時一樣的。要使用自動掃描機制,我們需要打開以下配置資訊:

代碼

[html] view plain copy

  1. <?xml version="1.0" encoding="UTF-8" ?>   
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.                 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.         http://www.springframework.org/schema/context  
  8.         http://www.springframework.org/schema/context/spring-context-2.5.xsd">   
  9.     <context:component-scan base-package=”com.eric.spring”>     
  10. </beans>   

1.component-scan标簽預設情況下自動掃描指定路徑下的包(含所有子包),将帶有@Component、@Repository、 @Service、@Controller标簽的類自動注冊到spring容器。對标記了 Spring's @Required、@Autowired、JSR250's @PostConstruct、@PreDestroy、@Resource、JAX-WS's @WebServiceRef、EJB3's @EJB、JPA's @PersistenceContext、@PersistenceUnit等注解的類進行對應的操作使注解生效(包含了annotation- config标簽的作用)。

getBean的預設名稱是類名(頭字母小寫),如果想自定義,可以@Service(“aaaaa”)這樣來指定。

這種bean預設是“singleton”的,如果想改變,可以使用@Scope(“prototype”)來改變。

可以使用以下方式指定初始化方法和銷毀方法:

[java] view plain copy

  1. @PostConstruct  
  2. public void init() {   
  3. }   
  4. @PreDestroy  
  5. public void destory() {   
  6. }   

注入方式:

把DAO實作類注入到action的service接口(注意不要是service的實作類)中,注入時不要new 這個注入的類,因為spring會自動注入,如果手動再new的話會出現錯誤,

然後屬性加上@Autowired後不需要getter()和setter()方法,Spring也會自動注入。  

在接口前面标上@Autowired注釋使得接口可以被容器注入,如:

[java] view plain copy

  1. @Autowired  
  2. @Qualifier("chinese")  
  3. private Man man;   

當接口存在兩個實作類的時候必須使用@Qualifier指定注入哪個實作類,否則可以省略,隻寫@Autowired。

附加:

@RequestMapping(value = "/produces", produces = "application/json"):表示将功能處理方法将生産json格式的資料,此時根據請求頭中的Accept進行比對,如請求頭“Accept:application/json”時即可比對; @RequestMapping(value = "/produces", produces = "application/xml"):表示将功能處理方法将生産xml格式的資料,此時根據請求頭中的Accept進行比對,如請求頭“Accept:application/xml”時即可比對。 此種方式相對使用@RequestMapping的“headers = "Accept=application/json"”更能表明你的目的。 伺服器控制器代碼詳解cn.javass.chapter6.web.controller.consumesproduces.ProducesController;