【问题标题】:@Autowire: @repository in @service@Autowire:@service 中的 @repository
【发布时间】:2016-04-15 18:22:18
【问题描述】:

我对 Spring 很陌生,在理解我似乎无法解决的空指针异常时遇到了一些麻烦。 我建模了一个 Event 类,并希望使用 JpaRepository 来查询连接到 Spring 的 PostgreSQL DB。

我注意到有很多不同的方法可以实现某些目标。在做了一些研究后,我想出了这个:

我将EventRepository定义如下:

EventRepository.class

@Repository
 package hello;
 public interface EventRepository extends JpaRepository<Event, Long> {
    List<Event> findByEventID(long eventID);
 }

接下来我定义了EventService,它有一个EventRepository类型的属性。在这个类中,我可以定义各种方法,通过 EventRepository 属性使用 EventRepository 中的方法。我的印象是,为这个 EventRepository 属性使用 @Autowire 会使 spring 实例化并自行配置它。但是,当我们尝试使用存储库时,会引发 NullPointerException。

EventService.class

@Service
public class EventService {

    @Autowired(required=true)
    private EventRepository eventRepository;

    public Event makeAnEvent(long partnerID, String eventDescription, String eventLink, boolean eventIsVisible, String eventPaymentText, Timestamp eventTimeStart, Timestamp eventTimeStop, String location, String eventName) {
        //ApplicationContext ctx = new AnnotationConfigApplicationContext(JpaContext.class);

        //Event e = new Event(3, "This event is awesome", "http://www.ugent.be", true, "This event is free.", new Timestamp(date.getDay()), new Timestamp(date.getTime()), "In het UFO.", "The P@rty!" );
        Event e = new Event(partnerID, eventDescription, eventLink, eventIsVisible, eventPaymentText, eventTimeStart, eventTimeStop, location, eventName);

        eventRepository.save(e);
        return e;
    }

}

这是我得到的错误:

java.lang.NullPointerException: null
    at hello.EventService.makeAnEvent(EventService.java:25) ~[classes/:na]
    at hello.RESTController.jpa(RESTController.java:31) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_73]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_73]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_73]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_73]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:817) ~[spring-webmvc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:731) ~[spring-webmvc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) ~[spring-webmvc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) ~[spring-webmvc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:968) ~[spring-webmvc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859) ~[spring-webmvc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844) ~[spring-webmvc-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.0.32.jar:8.0.32]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) ~[tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522) [tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095) [tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672) [tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) [tomcat-embed-core-8.0.32.jar:8.0.32]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) [tomcat-embed-core-8.0.32.jar:8.0.32]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_73]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_73]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.0.32.jar:8.0.32]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_73]

Event.class

@Entity
@NamedQuery(name="findAllEvent", query="SELECT e from Event e")
@Table(name = "public.Event")
public class Event {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="EventID", nullable=false)
private long eventID;


//@OneToMany annotatie ofzo, FK
@Column(name="partnerID")
private long partnerID;

@Column(name="eventDescription")
private String eventDescription;

@Column(name="eventLink")
private String eventLink;

@Column(name="eventIsVisible")
private boolean eventIsVisible;

@Column(name="eventPaymentText")
private String eventPaymentText;

@Column(name="eventTimeStart")
private Timestamp eventTimeStart;

@Column(name="eventTimeStop")
private Timestamp eventTimeStop;

//OneToMany, FK
@Column(name="locationID")
private String location;

@Column(name="eventName")
private String eventName;

//static int staticint = 1;

//@Version
//private Long version;

protected Event(){} //For JPA

public Event(long partnerID, String eventDescription, String eventLink,
        boolean eventIsVisible, String eventPaymentText, Timestamp eventTimeStart, Timestamp eventTimeStop,
        String location, String eventName) {
    ...
}

//getters + setters
}

有人知道我做错了什么,为什么错了吗?提前致谢! :)

【问题讨论】:

  • 显示您的配置文件以及您如何访问该服务。你在用控制器吗?如果是这样,请同时显示控制器代码。另外,我希望包声明之前的注释只是网站上的一个错字。
  • 请分享您的 spring 配置文件。您是否对使用服务和存储库注释的所有类进行了组件扫描,或者您是否为配置文件中的每个类声明了 bean?没有配置文件,就很难知道了

标签: java spring repository spring-data autowired


【解决方案1】:

正如其他评论者所提到的,看看应用程序是如何配置的将是最有用的。你在使用 Spring Boot 吗?注释驱动的配置? XML 配置?

如果不是配置问题,请仔细检查 RESTController.java 中是否定义了对服务的自动装配依赖项,并且没有实例化新实例。

RESTController.java

@Autowired
private EventService eventService;

并使用eventService.makeEvent(partnerID, eventDescription, ...); 调用它

如果你有

EventService eventService = new EventService();

那么该实例将不会被 Spring 捕获以进行依赖注入,因此 eventRepository 将为空。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 2017-01-21
    • 1970-01-01
    • 2020-05-07
    • 2023-02-02
    • 2020-06-06
    相关资源
    最近更新 更多