简体   繁体   English

SpringBoot:将存储库注入到服务层中定义的类中

[英]SpringBoot: Inject repository into a class defined in service layer

I searched on SO for similar questions, however I did not find very specific answer. 我在SO上搜索了类似的问题,但是没有找到非常具体的答案。 I am new to Springboot. 我是Springboot的新手。 I have a defined a POJO in service layer. 我在服务层中定义了一个POJO。 I want to inject repository into this class. 我想将存储库注入此类。 Somehow, all the time it comes out to be null . 不知何故,一直以来它都是null Here is my code structure, 这是我的代码结构,

file : service/ResultInstitute.java 文件:service / ResultInstitute.java

@Document(indexName = "result_institute")
public class ResultInstitute implements Serializable {


@Inject
public CourseRepository courseRepository;

/**
 * 
 */
private static final long serialVersionUID = -2168910694195614091L;

public ResultInstitute(Institute institute)  {

    // Initialize all the values.

    for (Course course : institute.getCourses()){
        HashMap<String, String> courseDetails = courseRepository.getCourseDetails(course.getId());
        course.setCourseDetails(courseDetails);
        courses.add(course);
    }
    courses       = institute.getCourses();

    for (Course course : courses){
        subCategories.put(course.getSubCategory().getId(), course.getSubCategory().getDisplayName());
        categories.put(course.getSubCategory()
                             .getCategory()
                             .getId(), 
                       course.getSubCategory()
                             .getCategory()
                             .getDisplayName());
    }
}

public ResultInstitute (){}

private Long id;

private String code; ....

file : repository/CourseRepository.java 文件:repository / CourseRepository.java

public interface CourseRepository extends JpaRepository<Course,Long> {

@Query("select distinct course from Course course left join fetch course.institutes")
List<Course> findAllWithEagerRelationships();

@Query("select course from Course course left join fetch course.institutes where course.id =:id")
Course findOneWithEagerRelationships(@Param("id") Long id);

@Query(value="SELECT DISTINCT(ci.course_details) FROM course_institute ci WHERE ci.courses_id = ?1", nativeQuery = true)
HashMap<String, String> getCourseDetails(Long id);

}

Whenever I'm trying to use courseRepository it gives me NullPointerException . 每当我尝试使用courseRepository它都会给我NullPointerException Can you please help me with this. 你能帮我这个忙吗?

In the comment section you said that your initiating the ResultInstitute class as follows, 在注释部分中,您说您按以下方式启动ResultInstitute类,

ResultInstitute resultInstitute = new ResultInstitute(i); resultInstitute.locationFilterString(i);

The initiation is handled by yourself, so @Autowired will not work in this case. 初始化由您自己处理,因此@Autowired在这种情况下将不起作用。 @Autowired is a spring configuration to inject the beans, so inorder to inject Repository ResultInstitute should be handled by Spring Iteself. @Autowired是用于注入bean的spring配置,因此为了注入Repository ResultInstitute,应该由Spring Iteself处理。

We have to tell spring that ResultInstitute is bean class for that you can annotate ResultIntitute class as @Component 我们必须告诉spring ResultInstitute是bean类,因为您可以将ResultIntitute类注释为@Component。

@Component 
public class ResultInititute

So whenever you wanted to instantiate ResultInstitute, instiate as a bean class using @Autowired 因此,每当要实例化ResultInstitute时,都可以使用@Autowired作为bean类进行初始化。

@Autowired
ResultInstitute resultInstitute;

And Using the Respository in Entity class is not good thing, we have to handle this in separate section. 在实体类中使用Repository不是一件好事,我们必须在单独的部分中进行处理。

You need to annotate your ResultInstitute with @Component . 您需要使用@Component注释ResultInstitute This tells spring that it has to inject dependencies. 这告诉spring它必须注入依赖项。

Add it to your constructor @Document(indexName = "result_institute") public class ResultInstitute implements Serializable { 将其添加到构造函数@Document(indexName =“ result_institute”)公共类ResultInstitute实现Serializable {

    public CourseRepository courseRepository;

    @Autowired
    public ResultInstitute (Institute institute, CourseRepository courseRepository) {
        this.courseRepository = courseRepository;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM