获得@Autowired错误

时间:2016-02-23 04:07:28

标签: java spring autowired

每当我使用@Autowired注释时,我都会收到错误消息。我得到的根错误是

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [jp.co.vmt.qt.C0002.C0002Dao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我的界面是

/**
 *
 */
package jp.co.vmt.qt.C0002;

import java.util.List;

import jp.co.vmt.qt.model.TmtProject;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

/**
 * @author WINDOWS
 */
public interface C0002Dao extends JpaRepository<TmtProject, Long> {

  @Query("SELECT PROJECT_ID, PROJECT_NAME FROM TmtProject")
  public List<TmtProject> getAllProjects();

}

我的实施班是

/**
 *
 */
package jp.co.vmt.qt.C0002;

import java.util.List;

import jp.co.vmt.qt.model.TmtProject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

/**
 * @author WINDOWS
 */
@Repository
public class C0002Service implements C0002Dao {

  @Autowired
  private C0002Dao c0002Dao;

  /*
   * (non-Javadoc)
   * @see jp.co.vmt.qt.C0002.C0002Dao#getAllProjects()
   */
  @Override
  public List<TmtProject> getAllProjects() {

    return this.c0002Dao.getAllProjects();

  }

... other methods from extended interface

}

这是我使用c0002Service的地方

/**
 *
 */
package jp.co.vmt.qt.C0002;

import java.util.List;

import jp.co.vmt.qt.model.TmtProject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author WINDOWS
 */
@Component
public class C0002Logic {

  @Autowired
  private C0002Dao c0002Service;

  public String getProjectList() throws Exception {

    List<TmtProject> projectList = this.c0002Service.getAllProjects();

    ObjectMapper mapper = new ObjectMapper();
    String jsonString = mapper.writeValueAsString(projectList);
    return jsonString;

  }

}

我的C0002Logic已在我的控制器中自动装配,此处未显示。 据我所知,我创建了一个接口(C0002Dao)和该接口的实现类(C0002Service),我用@Repository标记,我将服务自动装入我的Logic类(C0002Logic)。但是,我收到上面显示的错误。关于我哪里出错以及如何解决它们的任何想法?感谢

1 个答案:

答案 0 :(得分:0)

问题是我相信 在C0002Service中,

@Autowired
private C0002Dao c0002Dao;

试图在合格bean(C0002Dao的实例)即将注册的时候注入C0002Service