第二个自动连接的REST存储库无法正常工作

时间:2017-05-15 15:04:46

标签: java spring spring-boot autowired

我正在使用我的应用程序的控制器自动装配的存储库,它工作正常。但我也想在另一个班级里面使用我的回购。但是当我从第二个类调用任何方法时,我得到空指针异常,看起来像Autowiring在这个类中没有工作。我究竟做错了什么?是否不可能不止一次自动装配同一个回购?或者我应该实施服务并通过服务致电我的回购?

错误

2017-05-15 16:27:52.193 ERROR 6624 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at com.logic.RecommendationEngine.generatedRecommendation(RecommendationEngine.java:116) ~[classes/:na]
    at com.controllers.HomeController.showRecommendation(HomeController.java:46) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.5.11.jar:8.5.11]

回购界面:

public interface FundRepository extends CrudRepository<Fund, Long>{}

带回购的控制器

@RestController
public class RestServiceController {

    @Autowired
    private FundRepository fundRepo;

    //Some not related methods
}
具有相同回购的

第二类

public class RecommendationEngine {

    @Autowired
    private FundRepository fundRepo;

    //Method that invokes method from fundRepo but causes null pointer because fundRepo is null
}

3 个答案:

答案 0 :(得分:1)

  

Q :但是当我从第二个类调用任何方法时,我得到空指针异常,看起来像Autowiring在这个类中没有工作。我做错了什么?

A :这是正常的,因为您的RecommendationEngine类不是spring bean ==&gt;不受春天环境处理。因此,要使其工作,您应将其配置为bean(使用注释或xml配置)。

  

:不可能多次自动装配同一个回购邮件吗?

A :您可以根据需要调用您的春豆。

答案 1 :(得分:1)

使用@Autowired

初始化您的RecommendationEngine类对象
@Autowired
RecommendationEngine engine;

您的RecommendationEngine类应如此:

@Component
public class RecommendationEngine {

    @Autowired
    private FundRepository fundRepo;

    //Method that invokes method from fundRepo but causes null pointer because fundRepo is null
}

答案 2 :(得分:-1)

尝试使用@Service注释您的RecommendationEngine,因此Spring Container可以将其作为Spring Bean(托管对象)进行注释

@Service
public class RecommendationEngine {

    @Autowired
    private FundRepository fundRepo;
}

其他一些课你可以用它:

@Controller
public class HomeController {

    @Autowired
    private RecommendationEngine recommendationEngine;


    // use it

}

或者,您可以使用Project Lombok: https://projectlombok.org/减少班级中依赖项的Boilerplate代码。

使用Lombok的示例:

@Controller
@RequiredArgsConstructor
public class HomeController {

    private final RecommendationEngine recommendationEngine;
    private final SomeOtherDependeny dependency;


    // use it

}

无需手动@Autowired!