Spring Boot - 考虑定义一个类型的bean

时间:2017-10-05 13:14:00

标签: java spring spring-mvc spring-boot

尝试运行Spring Boot应用程序时收到错误:

  

com.adam.rest.client.impl.StudentClientImpl 中的字段studentMapper   需要一个类型为'com.adam.rest.mapper.StudentMapper'的bean   无法找到。

你可以建议出现什么问题吗? (见下面的文件)

App.java

angular-translate

StudentClientImpl.java

package com.adam.rest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

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

StudentMapper.java

package com.adam.rest.client.impl;

import com.adam.rest.mapper.StudentMapper;
import com.adam.rest.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository
public class StudentClientImpl {

    @Autowired
    private StudentMapper studentMapper;

    private static Map<Integer, Student> students;
         // DUMMY DATA GOES HERE

    Collection<Student> getStudents() { return this.students.values(); }

    Student getStudent(Integer id) { return this.students.get(id); }

    ...

1 个答案:

答案 0 :(得分:0)

为您的界面创建一个实现类:

public class StudentMapperImpl implements StudentMapper {
 ...
}

然后将@Bean方法添加到@SpringBootApplication类或另一个@Configuration类。

例如:

@SpringBootApplication
public class App {

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

    @Bean
    public StudentMapperImpl studentMapper() {
        return new StudentMapperImpl ();
    }
}