2014-01-16 100 views
1

我的代碼的接口服務豆類空如下:春3:@Autowired DAO字段與@Transactional註釋

DAO接口:

public interface SampleTblDao extends BaseDao<SampleTbl> { 

/** 
* 
* @param mapper 
* @param sampleKey 
* @return 
* @throws DataAccessException 
*/ 
public SampleTbl selectSampleTbl(SampleKey sampleKey) throws DataAccessException; 

} 

DAO實現

public class SampleTblDaoImpl extends AbstractBaseDao<SampleTbl, SampleTblMapper> implements SampleTblDao { 

    public SampleTblDaoImpl() { 
    super(SampleTblMapper.class); 
    } 

    @Override 
    public SampleTbl selectSampleTbl(SampleKey sampleKey) throws DataAccessException { 
    return getSqlSession().getMapper(SampleTblMapper.class).selectSampleTbl(sampleKey); 
    } 

} 

業務-logic接口:

public interface SampleBusinessLogic { 

    /** 
    * 
    * @param sample 
    * @return 
    * @throws SampleException 
    */ 
    public Sample createSample(Sample sample) throws SampleException; 

    /** 
    * 
    * @param sample 
    * @return 
    * @throws SampleException 
    */ 
    public Sample updateSample(Sample sample) throws SampleException; 

    /** 
    * @param sampleKey 
    * @return 
    * @throws SampleException 
    */ 
    public Sample getSample(SampleKey sampleKey) throws SampleException; 

    } 

業務邏輯的實現:

public class SampleBusinessLogicImpl implements SampleBusinessLogic { 

    /** 
    * sample table dao 
    */ 
    @Autowired 
    @Qualifier(value = "sampleTblDao") 
    private SampleTblDao sampleTblDao; 

    @Override 
    @Transactional(rollbackFor = Exception.class) 
    public Sample createSample(Sample sample) throws SampleException { 

     try { 
      // sample table 
      createSampleTbl(sample.getSampleTbl()); 

     } catch (Exception e) { 

      List <String> messageList = new ArrayList <String>(); 
      String message = "{createSample : " + "{itemId=" + sample.getSampleTbl().getItemId() + "}," + "{itemName=" + sample.getSampleTbl().getItemName() + "}}"; 

      messageList.add("createShop system error"); 

      throw new SampleException(message, messageList, e); 

     } 

     return sample; 
    } 

    @Override 
    @Transactional(rollbackFor = Exception.class) 
    public Sample updateSample(Sample sample) throws SampleException { 

     List <String> messageList = null; 
     String message = null; 

     try { 

      // global shop table 
      updateSampleTbl(sample.getSampleTbl()); 

     } catch (IllegalDataException e) { 

      message = "{updateSample : " + "{itemId=" + sample.getSampleTbl().getItemId() + "}," + "{itemName=" + sample.getSampleTbl().getItemName() + "}}"; 

      messageList = new ArrayList <String>(); 
      messageList.add("updateSample illegal data error"); 

      throw new SampleException(message, messageList, e); 

     } catch (Exception e) { 

      message = "{updateSample : " + "{itemId=" + sample.getSampleTbl().getItemId() + "}," + "{itemName=" + sample.getSampleTbl().getItemName() + "}}"; 

      messageList = new ArrayList <String>(); 
      messageList.add("updateSample system error"); 

      throw new SampleException(message, messageList, e); 

     } 

     return sample; 

    } 

    @Override 
    @Transactional(rollbackFor = Exception.class, readOnly = true) 
    public Sample getSample(SampleKey sampleKey) throws SampleException { 

     Sample sample = new Sample(); 

     String message = null; 
     List <String> messageList = null; 

     try { 

      sample.setSampleTbl(getSampleTbl(sampleKey)); 

     } catch (DataNotFoundException e) { 

      message = "{getSample : " + "{itemId=" + sampleKey.getItemId() + "}}"; 

      messageList = new ArrayList <String>(); 
      messageList.add("getSample data not found error"); 

      throw new SampleException(message, messageList, e); 

     } catch (Exception e) { 

      message = "{getSample : " + "{itemId=" + sampleKey.getItemId() + "}}"; 

      messageList = new ArrayList <String>(); 
      messageList.add("getSample system error"); 

      throw new SampleException(message, messageList, e); 

     } 

     return sample; 
    } 

    /** 
    * 
    * @param sampleTbl 
    * @throws Exception 
    */ 
    private void createSampleTbl(SampleTbl sampleTbl) throws Exception { 

     sampleTbl.setItemId(new UUID().toString()); 
     sampleTblDao.insert(sampleTbl); 
    } 

    /** 
    * @param sampleTbl 
    * @throws Exception 
    */ 
    private void updateSampleTbl(SampleTbl sampleTbl) throws Exception { 

     if (sampleTbl.isTransactionTarget(SampleTbl.class)) { 

      String message = null; 
      SampleKey sampleKey = new SampleKey(); 
      sampleKey.setItemId(sampleTbl.getItemId()); 

      SampleTbl sampleTblPre = sampleTblDao.selectSampleTbl(sampleKey); 

      if (sampleTblPre == null) { 
       // if sample table is empty 
       message = "{illegal data error:{sampleTblPre=null}}"; 
       throw new IllegalDataException(message); 
      } 

      sampleTbl.setItemId(sampleTblPre.getItemId()); 
      sampleTblDao.update(sampleTbl); 

     } 

    } 

    /** 
    * @param sampleKey 
    * @return 
    * @throws Exception 
    */ 
    private SampleTbl getSampleTbl(SampleKey sampleKey) throws Exception { 

     String message = ""; 

     SampleTbl sampleTbl = sampleTblDao.selectSampleTbl(sampleKey); 

     if (sampleTbl == null) { 
      // if sample tbl is empty 
      message = "{data not found error:{SampleTbl=null}}"; 
      throw new DataNotFoundException(message); 
     } 

     return sampleTbl; 
    } 

    public void setSampleTblDao(SampleTblDao sampleTblDao) { 
     this.sampleTblDao = sampleTblDao; 
    } 
} 

在應用程序上下文XML如下我已經配置豆類:

<bean id="sampleTblDao" class="com.rakuten.gep.sample.dao.impl.SampleTblDaoImpl" parent="baseDAO" scope="singleton"> 
     <property name="namespace" value="com.rakuten.gep.sample.dao.mapper.SampleTblMapper" /> 
</bean> 
<bean id="sampleBusinessLogic" class="com.rakuten.gep.sample.businesslogic.impl.SampleBusinessLogicImpl" scope="singleton"/> 

<!-- and annocation based transaction is configured as follows --> 
<tx:annotation-driven transaction-manager="trx-manager" proxy-target-class="true" /> 

<bean id="trx-manager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
     <property name="dataSource" ref="dataSource"/> 
</bean> 

但調試後,我意識到在自動裝配Autowired場「sampleTblDao「SampleBusinessLogicImpl '始終爲空。

任何提示?

+0

當你使用它時,你如何獲得'SampleBusinessLogicImpl'實例?另外,您的註釋語法很奇怪 – 2014-01-16 06:52:50

+0

我在Jersery Resources.I中使用它們,我使用@AutoWire註釋很好地注入它們。 –

+1

我在配置中看不到''或''。基本上忽略'@ Autowired'。 –

回答

4

您正在配置XML中的內容並且依賴於(部分)註釋。默認情況下,Spring會忽略所有註釋,如@Autowired,@Inject等。爲了能夠處理這些註釋,需要註冊AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor(後者用於@Resource和其他JSR-250註釋處理)的實例。

這可以手動完成或使用命名空間。

<context:annotation-config /> 

當使用<context:component-scan />它已經暗示一個想用於配置註釋和作爲<context:annotation-config />這樣的功能已經包括在內,因此無需重新添加。