java - Spring 3 : @Autowired dao fields are null in service beans with @Transactional annotation

标签 java spring spring-aop spring-transactions

我对接口(interface)的编码如下:

DAO 接口(interface):

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);
  }

}

业务逻辑接口(interface):

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:

<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>

但在调试之后,我意识到在'SampleBusinessLogicImpl' 中的 Autowiring 字段'sampleTblDao' 始终为空。

有什么建议吗?

最佳答案

您正在使用 XML 配置内容并(部分)依赖注释。默认情况下,spring 忽略所有注释,如 @Autowired , @Inject之类的。要启用这些注释的处理,请使用 AutowiredAnnotationBeanPostProcessor 的实例和 CommonAnnotationBeanPostProcessor (后者用于@Resource等JSR-250注解处理)需要注册。

这可以手动完成或使用命名空间完成。

<context:annotation-config />

使用 <context:component-scan /> 时已经暗示人们想要使用注释进行配置以及 <context:annotation-config /> 的功能。已包含,因此无需再次添加。

关于java - Spring 3 : @Autowired dao fields are null in service beans with @Transactional annotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21155010/

相关文章:

java - 方法参数类型不匹配异常 : How to find the controller method

java - 无法使用 JDBC 运行 java 程序来连接 postgres?

java - 使用 SseEmitter 的 Spring rest 服务

java - Hibernate + Lucene - 通配符搜索返回空结果

java - Spring Hibernate ehcache 设置

java - Spring AOP @Before通知返回值

java - Spring AOP 与 AspectJ

java - Spring AOP错误: java. lang.NoClassDefFoundError:org/springframework/cglib/core/SpringNamingPolicy

java - 字符(运算符)的扫描仪输入如何工作?

java - 数组开头的额外元素