【问题标题】:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'voteService'org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“voteService”的bean时出错
【发布时间】:2018-11-03 05:42:31
【问题描述】:

我是 spring 新手,正在尝试使用 mongodb 创建一个 spring 项目。运行时出现以下错误。

    2018-05-23 22:15:20.077  WARN 11610 --- [main] 
    ConfigServletWebServerApplicationContext : Exception encountered during 
    context initialization - cancelling refresh attempt: 
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
    creating bean with name 'voteController': Unsatisfied dependency 
    expressed through field 'voteSvc'; nested exception is 
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
    creating bean with name 'voteService': Unsatisfied dependency expressed 
    through field 'voteRepo'; nested exception is 
    org.springframework.beans.factory.BeanCreationException: Error creating 
    bean with name 'votesRepository': Invocation of init method failed; 
    nested exception is 
    org.springframework.data.mapping.PropertyReferenceException: No 
    property findAll found for type Vote!
    2018-05-23 22:15:20.081  INFO 11610 --- [main] 
    o.apache.catalina.core.StandardService   : Stopping service [Tomcat]

这是我的 Application.java 文件

    package com.hello;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;

    @SpringBootApplication
    @ComponentScan("com.hello")
    public class Application implements CommandLineRunner {

    @Autowired
    private UserRepository userRepo;
    private IdeaRepository ideaRepo;
    private VotesRepository voteRepo;


    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {


        }
     }

这是我的 VoteController.java

    package com.hello;

    import java.util.*;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.stereotype.*;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.*;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.boot.autoconfigure.*;

    @Controller
    @RequestMapping("/api")

    public class VoteController{

    @Autowired
    IVoteService voteSvc;

    @RequestMapping(value = "/votes", method = RequestMethod.GET)
    public List<Vote> findByIdeaId(int ideaId) 
    {
        List<Vote> votes = voteSvc.GetVotesByIdeaId(ideaId);
        return votes;
    }

    @RequestMapping(value = "/votes", method = RequestMethod.POST)
    public Vote PostByIdeaId(Vote vote) 
    {
        Vote votes = voteSvc.SaveVotesByIdeaId(vote);
        return votes;
    }

    @RequestMapping(value = "/votes", method = RequestMethod.DELETE)
    public void DeleteByIdeaId(Vote vote) 
    {
        voteSvc.DeleteVotesByIdeaId(vote);
     }
    }

这里是 VoteService.java

package com.hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.*;
import  java.util.List;
import  java.util.*;

@Service
public class VoteService implements IVoteService {

    @Autowired
    VotesRepository voteRepo;

    public List<Vote> GetVotesByIdeaId(int ideaId)
    {
        List<Vote> votes = voteRepo.FindAll(ideaId);
        //List<Vote> votes = new ArrayList<Vote>();
        return votes;
    }

    public Vote SaveVotesByIdeaId(Vote vote) 
    {
        Vote voteIdea = voteRepo.save(vote);
        return voteIdea;
    }

    public void DeleteVotesByIdeaId(Vote vote)
    {
        voteRepo.delete(vote);
    }

}

这是我的 VoteRepository.java

package com.hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.*;
import  java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;

@Repository
public interface VotesRepository extends MongoRepository<Vote, String> {

    public List<Vote> FindAll(int IdeaId);

}

这是我的界面

package com.hello;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.*;

@Service
public interface IVoteService {


    public List<Vote> GetVotesByIdeaId(int ideaId);

    public Vote SaveVotesByIdeaId(Vote vote);

    public void DeleteVotesByIdeaId(Vote vote);

}

这是我的 gradle.build 文件

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.1.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'idea-platform'
    version =  '0.1.0'
}

jar {
  manifest {
    attributes(
      'Main-Class': 'hello.Application'
    )
  }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8 

repositories {
    mavenCentral()
}



dependencies {
   compile("org.springframework.boot:spring-boot-starter-web")
   compile("org.springframework.boot:spring-boot-starter-thymeleaf")
   compile("org.springframework.boot:spring-boot-devtools")
    compile("org.springframework.boot:spring-boot-starter-data-mongodb")
   testCompile("junit:junit")
}

这是我的实体

package com.hello;
import org.springframework.data.annotation.Id;


public class Vote {

    @Id
    public String id;

    public String user;
    public Integer ideaId;

    public Vote() {}


}

我查看了其他类似的问题,但没有一个能帮助我解决。

【问题讨论】:

    标签: java mongodb spring-boot gradle


    【解决方案1】:

    您在存储库中定义的方法与 Spring Data Repositories 方法不匹配。假设ideaIdVote 上的一个属性,使用给定ideaId 查找所有Vote 的方法应该是这样的:

    @Repository
    public interface VotesRepository extends MongoRepository<Vote, String> {
    
        public List<Vote> findByIdeaId(int ideaId);
    
    }
    

    查看here,了解有关如何实现 Spring Data Repositories 的更多详细信息。

    P.S.:附带说明,请尝试遵守 Java 命名约定,以小写开头的方法和参数名称。

    【讨论】:

      【解决方案2】:

      这不是完整的解决方案,但我也注意到了这个问题。您的每个存储库可能都需要自动装配,而不仅仅是第一个。

      你有:

      @Autowired
      private UserRepository userRepo;
      private IdeaRepository ideaRepo;
      private VotesRepository voteRepo;
      

      但我认为你想要:

      @Autowired
      private UserRepository userRepo;
      
      @Autowired
      private IdeaRepository ideaRepo;
      
      @Autowired
      private VotesRepository voteRepo;
      

      【讨论】:

        猜你喜欢
        • 2019-01-22
        • 2021-11-17
        • 2021-08-25
        • 2021-08-13
        • 2021-09-18
        • 2019-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多