Spring Data Repository自动装配麻烦

时间:2017-11-24 06:17:10

标签: spring spring-boot spring-data spring-data-jpa autowired

我无法在我的Controller中自动装配我的CrudRespository(我正在使用Spring启动)。

这是我的Controller,Entity&资源库实现

存储库:     包com.nbha.micro.ratingService;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;


public interface RaterDORepository extends CrudRepository<RaterDO,       Long>   {

public RaterDO findByRaterName();
}

实体:

@Entity
public class RaterDO {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long raterId;

public RaterDO(){}

public RaterDO(String n) {
    this.raterName = n;
}



public Long getRaterId() {
    return raterId;
}

public void setRaterId(Long raterId) {
    this.raterId = raterId;
}

public String getRaterName() {
    return raterName;
}

public void setRaterName(String raterName) {
    this.raterName = raterName;
}

private String raterName;
}

控制器:

@RestController
@RequestMapping("/raters")
public class RaterController {

@Autowired
private RaterDORepository raterDORepository;

@RequestMapping("/add")
public String addRater(@RequestParam("name") String name) {
    RaterDO raterDO = new RaterDO(name);
    raterDORepository.save(raterDO);
    return "Rater:" + name +" saved.";
}

 @RequestMapping("/allRaters")
 public String getAllRaters() {
        StringBuilder sb = new StringBuilder();
        sb.append("|");
        Iterable<RaterDO> raters = raterDORepository.findAll();
        for(RaterDO rater : raters) {
            sb.append(rater.getRaterName());
            sb.append("|");
        }
        return sb.toString();
 }

}

入门课程:

@EnableBinding(ConsumerChannels.class)
@ComponentScan(basePackages={"com.nbha.micro.ratingService"})
@EntityScan(basePackages={"com.nbha.micro.ratingService"})
@EnableJpaRepositories(basePackages={"com.nbha.micro.ratingService"})
@SpringBootApplication
@EnableEurekaClient
@RestController
@RequestMapping("/ratings")
public class RatingApp 
{

public static void main( String[] args )
{
    System.out.println( "Invoking Rating Service.." );
    SpringApplication.run(RatingApp.class, args);
}




@StreamListener(ConsumerChannels.PRODUCER) 
public void receiveRater(final Rater rater) {
    if(rater != null) {
        System.out.println("Rating received in the rating-service:" + rater.getName());

    }
}




private List<Rating> ratingList = Arrays.asList(
        new Rating("R101","101",5),
        new Rating("R102","102",2),
        new Rating("R103","101",4),
        new Rating("R104","101",1)

        );

@GetMapping("/all")
public List<Rating> findAllRatings() {
    return this.ratingList;
}

@GetMapping("/rating-agency")
public String whichRatingAgency() {
    return "Moody's";
}

@GetMapping("")
public List<Rating> findRatingsByBookId(@RequestParam String bookId) {
    Rating r;
    List<Rating> rList = new ArrayList<Rating>();
    ListIterator iter = this.ratingList.listIterator();
    while(iter.hasNext()) {
        r = (Rating)iter.next();
        if(r.getBookId().equals(bookId)) {
            rList.add(r);
        }
    }
    return rList;

}



}

interface ConsumerChannels {

String PRODUCER = "producer";

 @Input
 SubscribableChannel producer();
}

我将所有这些课程放在同一个套餐中(为了让它起作用,我们绝对不同) 我在应用程序启动时遇到以下错误(无法启动)。有趣的是,类似的设置在另一个实例中无需使用@ComponentScan @EntityScan @EnableJpaRepository注释:)

Application startup failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'raterController': Unsatisfied dependency expressed through field 'raterDORepository'; nested exception is   org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'raterDORepository': Invocation of init method failed; nested exception is java.util.NoSuchElementException
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
....
Caused by:  org.springframework.beans.factory.BeanCreationException:Error  creating bean with name 'raterDORepository': Invocation of init method failed; nested exception is java.util.NoSuchElementException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans- 4.3.11.RELEASE.jar:4.3.11.RELEASE]
at  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.11.RELEASE.jar:4.3.11.RELEASE]
....
Caused by: java.util.NoSuchElementException: null
at java.util.ArrayList$Itr.next(ArrayList.java:860) ~[na:1.8.0_151]
at  org.springframework.data.jpa.repository.query.ParameterMetadataProvider.next(ParameterMetadataProvider.java:122) ~[spring-data-jpa-1.11.7.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:302) ~[spring-data-jpa-1.11.7.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:208) ~[spring-data-jpa-1.11.7.RELEASE.jar:na]
....

任何线索将不胜感激。以下是我对pom.xml的依赖:

<parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.5.7.RELEASE</version>
</parent>

<dependencies>



<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>


<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
 </dependency>

 <dependency>
      <groupId>com.h2database</groupId>
     <artifactId>h2</artifactId>
 </dependency>

<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-kafka</artifactId>
</dependency>



<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>


</dependencies>



<dependencyManagement>
 <dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies`enter code here`</artifactId>
    <version>Brixton.SR5</version>
    <type>pom</type>
    <scope>import</scope>
  </dependency> 
</dependencies>
</dependencyManagement>

1 个答案:

答案 0 :(得分:1)

public RaterDO findByRaterName();需要一个参数,例如public RaterDO findByRaterName(String name)

存储库的构建失败,因为缺少的参数会阻止Spring Data构建正确的方法。