通过构造函数参数0表示的不满意的依赖性

时间:2017-09-22 20:57:19

标签: java spring maven spring-boot

我正在尝试一个示例springboot项目。但是在一半的时间里我遇到了这个问题。

我的Controller.java

package org.springbootdemo5.springbootdemo5.controler;
import org.springboot5.springbootdemo5.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleRestController {
    @Autowired
private TaskService taskService;
@GetMapping("/hello")
public String hello(){
    return "Hello World!!!";
}}

我的任务服务

package org.springboot5.springbootdemo5.service;

import java.util.ArrayList;
import java.util.List;
import javax.transaction.Transactional;
import org.springboot5.springbootdemo5.dao.TaskRepository;
import org.springboot5.springbootdemo5.model.Task;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class TaskService {

private final TaskRepository taskRepository;

public TaskService(TaskRepository taskRepository) {
    this.taskRepository = taskRepository;
}

public List<Task> findAll(){
    List<Task> tasks = new ArrayList<>();
    for(Task task : taskRepository.findAll()){
        tasks.add(task);
    }
    return tasks;
}
public Task findTask(int id){
    return taskRepository.findOne(id);
}

public void save(Task task){
    taskRepository.save(task);
}
public void delete(int id){
    taskRepository.delete(id);
}}

我的任务存储库

package org.springboot5.springbootdemo5.dao;

import org.springboot5.springbootdemo5.model.Task;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;
public abstract class TaskRepository implements CrudRepository<Task, Integer>
{
}

这是我的初始化程序

package org.springbootdemo5.springbootdemo5;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("org.springboot5.springbootdemo5")
public class Springbootdemo5Application {

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

所有注释都是正确提供的,pom.xml也是正确的,但我仍然像

一样异常
Exception 
encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
creating bean with name 'taskService' defined in file 
  Unsatisfied dependency 
expressed through constructor parameter 0; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No 
qualifying bean of type 'org.springboot5.springbootdemo5.dao.TaskRepository' 
available: expected at least 1 bean which qualifies as autowire candidate. 
Dependency annotations: {}

Description:

Parameter 0 of constructor in 
org.springboot5.springbootdemo5.service.TaskService required a bean of type 
'org.springboot5.springbootdemo5.dao.TaskRepository' that could not be 
found.


Action:

Consider defining a bean of type 
'org.springboot5.springbootdemo5.dao.TaskRepository' in your configuration.

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springbootdemo5</groupId>
<artifactId>springbootdemo5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springbootdemo5</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.7.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
        <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
    <dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>9.0.0.M26</version>
</dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

任何人都可以解决这个问题? 随时欢迎任何帮助。 谢谢。

2 个答案:

答案 0 :(得分:0)

尝试更改TaskRepository

public interface TaskRepository extends CrudRepository<Task, Integer>

答案 1 :(得分:0)

检查xml中提到的bean ID,并且Java文件中的成员应该相同,这是区分大小写的。

config.xml

<bean id="reportServicePdf" class="my.local.spring.sprintlex.ReportService">
        <constructor-arg name="numberOfPage" value="11" /> //<====Here
    </bean>

ReportService.java:

public class ReportService {    
@Value("100")
    private int numberOfPage;//<====Here

::
::
}