spring 註解

有關spring的註解,今天瞭解了下,現在一方面做下學習總結,另一方面給學習的筒子做個借鑑。

 

spring提供相關的幾個Annotation來標註bean先列出來

@Component:標註一個普通的spring bean

@Controller:標註一個控制器組件類如action

@Service:標註一個邏輯控制類如Service層

@Repository:標註一個持久層Dao組件類

 

再列幾個

@Scope:相信大家對這個不陌生吧,表示bean的作用域,使用方式:Scope("prototype")

@Resource:配合依賴,使用方式:Resource(name="XXXX")等同於xml中的配置<property …… ref="XXXX" />

@Autowired:自動裝配,默認按照type裝配,如果需要按照name裝配就需要和下面的相結合了

@Qualifier

                        針對自動裝配下面展示兩種寫法分別表示屬性修飾和set方式修飾:

                        @Autowried

                        @Qualifier("XXXX")

                         private XXXX xxxx;

                        -----------------------------------------------------

                        @Autowried

                        public void setXXX(@Qualifier("xxxx") XXX xxx){}

 

基本常用的註解也就上面的了,現在貼上代碼:

要想讓註解生效,首先要在配置文件中指明掃描那些包下的Bean類,

 

包結構:

      cn.life.routine

                           -action

                           -dao

                           -service

 

引入ContextSchema,spring配置

[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="  
  6.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.        http://www.springframework.org/schema/context <a href="http://www.springframework.org/schema/context/spring-context-3.0.xsd">http://www.springframework.org/schema/context/spring-context-3.0.xsd</a>"  
  8.     >  
  9.           
  10.     <!-- action暫未用註解 -->  
  11.     <bean id="NoticeAction" class="cn.life.routine.action.NoticeAction" scope="prototype" ></bean>  
  12.       
  13.     <!-- 註解測試,routine -->  
  14.     <context:component-scan base-package="cn.life.routine"></context:component-scan>  
  15. </beans>  


現在依次貼出service,dao

service接口

[java] view plain copy
  1. /** 
  2.  * 註解 
  3.  * @author Francis.Hu 
  4.  * @createDate Oct 21, 2012 
  5.  */  
  6. public interface TestService {  
  7.   
  8.     /** 
  9.      * 註解測試 
  10.      * @return 
  11.      */  
  12.     public String getTestAnnotation();  
  13. }  

service實現類


 

[java] view plain copy
  1. package cn.life.routine.service;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.beans.factory.annotation.Qualifier;  
  5. import org.springframework.stereotype.Service;  
  6.   
  7. import cn.life.routine.dao.TestDao;  
  8.   
  9. /** 
  10.  * 註解測試 
  11.  * @author Francis.Hu 
  12.  * @createDate Oct 21, 2012 
  13.  */  
  14. @Service("testService")  
  15. public class TestServiceImp implements TestService{  
  16.   
  17.     /** 
  18.      * 自動裝配 
  19.      */  
  20.     @Autowired  
  21.     @Qualifier("testDao")  
  22.     //@Resource(name="testDao"), 等價於<property ………… ref="testDao" />  
  23.     private TestDao testDao;  
  24.       
  25.     public String getTestAnnotation() {  
  26.         return testDao.getTestDaoAnnotation();  
  27.     }  
  28.   
  29.     public TestDao getTestDao() {  
  30.         return testDao;  
  31.     }  
  32.   
  33.     public void setTestDao(TestDao testDao) {  
  34.         this.testDao = testDao;  
  35.     }  
  36.   
  37. }  


 dao層接口

[java] view plain copy
  1. /** 
  2.  * 測試註解 
  3.  * @author Francis.Hu 
  4.  * @createDate Oct 21, 2012 
  5.  */  
  6. public interface TestDao {  
  7.   
  8.     /** 
  9.      * 得到dao層註解 
  10.      * @return 
  11.      */  
  12.     public String getTestDaoAnnotation();  
  13. }  

dao層實現類       

[java] view plain copy
  1. /** 
  2.  * 測試註解 
  3.  * @author Francis.Hu 
  4.  * @createDate Oct 21, 2012 
  5.  */  
  6. @Repository("testDao")  
  7. public class TestDaoImpl implements TestDao {  
  8.   
  9.     public String getTestDaoAnnotation() {  
  10.         return "This is testDao Annotation";  
  11.     }  
  12.   
  13. }  


下面是action中的調用

[java] view plain copy
  1. /** 
  2.  * 測試註解 
  3.  * @return 
  4.  */  
  5. @Resource(name="testService")  
  6. private TestService testService;  
  7. public String testAnnotation(){  
  8.     String result = testService.getTestAnnotation();  
  9.     System.out.println(result);  
  10.     return SUCCESS;  
  11. }  
  12. public TestService getTestService() {  
  13.     return testService;  
  14. }  
  15.   
  16. public void setTestService(TestService testService) {  
  17.     this.testService = testService;  
  18. }  
  19. /*********************測試結束******************************/  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章