我试图从我的Web包中自动装载存储库(后端程序包),但我得到了NullPointerException
。
我有两个包:
我的SRC / MAIN / webapp / WEB-INF / Web.XML:
<!--Load Spring Config -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/spring/applicationContext-db.xml
<!--classpath*:META-INF/spring/applicationContext.xml-->
</param-value>
</context-param>
<!-- The Bootstrap listener to start up and shut down Spring's root WebApplicationContext. It is registered to Servlet Container -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<!-- <param-value>Production</param-value> -->
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
<param-value>false</param-value>
</context-param>
<!-- JSF Servlet is defined to container -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Mapping with servlet and url for the http requests. -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
的applicationContext.xml
<!-- Scan for annotations and register them -->
<context:component-scan base-package="de.haegerconsulting.telculator.*"/>
<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- Detect @Transactional -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- Entity Manager Factory -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<!-- Define Hibernate JPA Vendor Adapter -->
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true"/>
<property name="database" value="H2"/>
</bean>
</property>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
<property name="persistenceUnitName" value="secondary"/>
</bean>
<!-- Datasource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem://localhost/~/telculator;DB_CLOSE_DELAY=-1"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
应该自动装配我的存储库的Java类
@Autowired
private CompanyRepository companyRepository;
视图(上传.csv文件)
@ManagedBean
public class FileUploadController {
private UploadedFile file;
private String destination = System.getProperty("java.io.tmpdir");
public void upload() throws IOException {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = file.getInputstream();
outputStream = new FileOutputStream(destination + file.getFileName());
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
try {
ImportCsvServiceImpl importCsvService = new ImportCsvServiceImpl(destination + file.getFileName());
// Write Sets and store them in Database
importCsvService.readCsvFile();
**importCsvService.storeEntities(); --> NullPointerException!**
} catch (Exception e) {
e.printStackTrace();
}
// File Upload was successful
FacesMessage message = new FacesMessage("Successful", file.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
}
}
单元测试工作得很好。我注入了一个存储库,所有测试都没问题。
你能帮帮我吗? 我错过了什么吗? 更新
栈跟踪
INFORMATION: Server startup in 5240 ms java.lang.NullPointerException at
de.haegerconsulting.telculator.backend.service.impl.ImportCsvServiceImpl.storeEntities(ImportCsvServiceImpl.java:178) 在 de.haegerconsulting.telculator.web.Controller.FileUploadController.upload(FileUploadController.java:41) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:606)at org.apache.el.parser.AstValue.invoke(AstValue.java:278)at at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274) 在 javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149) 在javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) 在 javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813) 在javax.faces.component.UICommand.broadcast(UICommand.java:300)at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) 在 javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) 在 com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) 在javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:78) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) 在 org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:603) 在 org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:312) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:615) 在java.lang.Thread.run(Thread.java:745) java.lang.NullPointerException at de.haegerconsulting.telculator.backend.service.impl.ImportCsvServiceImpl.storeEntities(ImportCsvServiceImpl.java:186) 在 de.haegerconsulting.telculator.web.Controller.FileUploadController.upload(FileUploadController.java:41) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:606)at org.apache.el.parser.AstValue.invoke(AstValue.java:278)at at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274) 在 javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149) 在javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) 在 javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813) 在javax.faces.component.UICommand.broadcast(UICommand.java:300)at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) 在 javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) 在 com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) 在javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:78) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) 在 org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:603) 在 org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:312) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:615) 在java.lang.Thread.run(Thread.java:745) java.lang.NullPointerException at de.haegerconsulting.telculator.backend.service.impl.ImportCsvServiceImpl.storeEntities(ImportCsvServiceImpl.java:194) 在 de.haegerconsulting.telculator.web.Controller.FileUploadController.upload(FileUploadController.java:41) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:606)at org.apache.el.parser.AstValue.invoke(AstValue.java:278)at at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274) 在 javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149) 在javax.faces.event.ActionEvent.processListener(ActionEvent.java:88) 在 javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813) 在javax.faces.component.UICommand.broadcast(UICommand.java:300)at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) 在 javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) 在 com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) 在javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:78) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 在 org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) 在 org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:603) 在 org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:312) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:615) 在java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:1)
ImportCsvServiceImpl importCsvService = new ImportCsvServiceImpl(destination + file.getFileName());
您在这里手动创建服务而不是从Spring容器中获取服务,所以这显然不是Spring管理的。使用此类中的注释自动装配的任何bean也不会被Spring注入,除非你通过在构造函数中从Spring应用程序上下文中询问它或者无论如何都会使用自动连接注释无用的初始化方法来显式初始化它。 )。
您的fileUploadController
是一个JSF托管bean,如果importCsvService
打算进行Spring管理,那么您需要按照here的说明通过自动装配使其工作。