简体   繁体   中英

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. I am new to Springboot. I have a defined a POJO in service layer. I want to inject repository into this class. Somehow, all the time it comes out to be null . Here is my code structure,

file : 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

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 . Can you please help me with this.

In the comment section you said that your initiating the ResultInstitute class as follows,

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

The initiation is handled by yourself, so @Autowired will not work in this case. @Autowired is a spring configuration to inject the beans, so inorder to inject Repository ResultInstitute should be handled by Spring Iteself.

We have to tell spring that ResultInstitute is bean class for that you can annotate ResultIntitute class as @Component

@Component 
public class ResultInititute

So whenever you wanted to instantiate ResultInstitute, instiate as a bean class using @Autowired

@Autowired
ResultInstitute resultInstitute;

And Using the Respository in Entity class is not good thing, we have to handle this in separate section.

You need to annotate your ResultInstitute with @Component . This tells spring that it has to inject dependencies.

Add it to your constructor @Document(indexName = "result_institute") public class ResultInstitute implements Serializable {

    public CourseRepository courseRepository;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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