Spring无法绑定DAOImpl类java.lang.NullPointerException

时间:2015-05-06 09:05:51

标签: spring-mvc spring-boot spring-jdbc

我的服务端点在我的DAO上调用了get()方法,但是在java.lang.NullPointerException中失败了。虽然调试它似乎Spring无法找到我的DAO的实现

ContactEndoint

package com.spring.contacts;

@Endpoint
@Configuration
@ComponentScan(basePackages="com.spring.contacts")
@EnableWebMvc
public class ContactEndpoint {
private static final String NAMESPACE_URI = "http://spring.io/guides/gs-producing-web-service";

@Autowired
private ContactDAO contactDAO;

//Default constructor
public ContactEndpoint(){
}

@Autowired
public ContactEndpoint(ContactDAO contactDAO) {
this.contactDAO = contactDAO;
}

@Cacheable(cacheName="imageCache",    keyGenerator=@KeyGenerator(name="StringKeyCache"))
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getContactRequest")
@RequestMapping(value = "/editContact", method = RequestMethod.GET)
@ResponsePayload
public GetContactResponse getContact(@RequestPayload GetContactRequest request){
  GetContactResponse response = new GetContactResponse();
  response.setContact(contactDAO.get(request.getId()));
  return response;
}   

的ContactDAO

package com.spring.contacts.dao;


public interface ContactDAO {

public Contact get(int contactId);

ContactDAOImpl

package com.spring.contacts.dao;

@Repository("contactDAO")
public class ContactDAOImpl implements ContactDAO {

private JdbcTemplate jdbcTemplate;

//default constructor
public ContactDAOImpl(){
}

public ContactDAOImpl(DataSource dataSource) {
    jdbcTemplate = new JdbcTemplate(dataSource);
}

@Override
public Contact get(int contactId) {
    String sql = "SELECT * FROM contact WHERE contact_id=" + contactId;
    return jdbcTemplate.query(sql, new ResultSetExtractor<Contact>() {

        @Override
        public Contact extractData(ResultSet rs) throws SQLException,
                DataAccessException {
            if (rs.next()) {
                Contact contact = new Contact();
                contact.setId(rs.getInt("contact_id"));
                contact.setName(rs.getString("name"));
                contact.setEmail(rs.getString("email"));
                contact.setAdress(rs.getString("address"));
                contact.setTelephone(rs.getString("telephone"));
                return contact;
            }

            return null;
        }

    });
}

WebServiceConfig

package com.spring.contacts;

@EnableWs
@Configuration
@ComponentScan("com.spring") 
public class WebServiceConfig extends WsConfigurerAdapter {
@Bean
public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
    MessageDispatcherServlet servlet = new MessageDispatcherServlet();
    servlet.setApplicationContext(applicationContext);
    servlet.setTransformWsdlLocations(true);
    return new ServletRegistrationBean(servlet, "/ws/*");
}

@Bean(name = "countries")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
    DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
    wsdl11Definition.setPortTypeName("CountriesPort");
    wsdl11Definition.setLocationUri("/ws");
    wsdl11Definition.setTargetNamespace("http://spring.io/guides/gs-producing-web-service");
    wsdl11Definition.setSchema(countriesSchema);
    return wsdl11Definition;
}

 @Bean(name = "contacts")
 public DefaultWsdl11Definition defaultContactsWsdl11Definition(XsdSchema contactsSchema){
   DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
   wsdl11Definition.setPortTypeName("ContactsPort");
   wsdl11Definition.setLocationUri("/ws/JDBC");
   wsdl11Definition.setTargetNamespace("http://spring.io/guides/gs-producing-web-service");
   wsdl11Definition.setSchema(contactsSchema);
return wsdl11Definition;

  }


@Bean
public XsdSchema countriesSchema() {
    return new SimpleXsdSchema(new ClassPathResource("countries/countries.xsd"));
}

@Bean
public XsdSchema contactsSchema(){
  return new SimpleXsdSchema(new ClassPathResource("contacts/contact.xsd"));
}

栈跟踪

java.lang.NullPointerException: null
at com.spring.contacts.dao.ContactDAOImpl.get(ContactDAOImpl.java:85) ~[ContactDAOImpl.class:na]
at com.spring.contacts.dao.ContactDAOImpl$$FastClassBySpringCGLIB$$f0feb335.invoke(<generated>) ~[spring-core-4.1.6.RELEASE.jar:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717) ~[spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653) ~[spring-aop-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at com.spring.contacts.dao.ContactDAOImpl$$EnhancerBySpringCGLIB$$5f3048d3.get(<generated>) ~[spring-core-4.1.6.RELEASE.jar:na]
at com.spring.contacts.ContactEndpoint.getContact(ContactEndpoint.java:45) ~[ContactEndpoint.class:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_25]
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_25]
at org.springframework.ws.server.endpoint.MethodEndpoint.invoke(MethodEndpoint.java:134) ~[spring-ws-core-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.ws.server.endpoint.adapter.DefaultMethodEndpointAdapter.invokeInternal(DefaultMethodEndpointAdapter.java:291) ~[spring-ws-core-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.ws.server.endpoint.adapter.AbstractMethodEndpointAdapter.invoke(AbstractMethodEndpointAdapter.java:55) ~[spring-ws-core-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.ws.server.MessageDispatcher.dispatch(MessageDispatcher.java:236) [spring-ws-core-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.ws.server.MessageDispatcher.receive(MessageDispatcher.java:176) [spring-ws-core-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.ws.transport.support.WebServiceMessageReceiverObjectSupport.handleConnection(WebServiceMessageReceiverObjectSupport.java:89) ~[spring-ws-core-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.ws.transport.http.WebServiceMessageReceiverHandlerAdapter.handle(WebServiceMessageReceiverHandlerAdapter.java:61) ~[spring-ws-core-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.ws.transport.http.MessageDispatcherServlet.doService(MessageDispatcherServlet.java:293) ~[spring-ws-core-2.2.1.RELEASE.jar:2.2.1.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) ~[servlet-api.jar:na]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[servlet-api.jar:na]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) ~[catalina.jar:8.0.21]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[catalina.jar:8.0.21]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-websocket.jar:8.0.21]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) ~[catalina.jar:8.0.21]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) ~[catalina.jar:8.0.21]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) ~[catalina.jar:8.0.21]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) ~[catalina.jar:8.0.21]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) ~[catalina.jar:8.0.21]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) ~[catalina.jar:8.0.21]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) ~[catalina.jar:8.0.21]
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) ~[catalina.jar:8.0.21]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) ~[catalina.jar:8.0.21]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) ~[catalina.jar:8.0.21]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) ~[tomcat-coyote.jar:8.0.21]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668) ~[tomcat-coyote.jar:8.0.21]
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) ~[tomcat-coyote.jar:8.0.21]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1517) ~[tomcat-coyote.jar:8.0.21]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1474) ~[tomcat-coyote.jar:8.0.21]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) ~[na:1.7.0_25]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) ~[na:1.7.0_25]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-util.jar:8.0.21]
at java.lang.Thread.run(Thread.java:724) ~[na:1.7.0_25]

1 个答案:

答案 0 :(得分:0)

我通过使用@Autowired

将数据源注入DAOImpl来修复它
@Autowired
public ContactDAOImpl(DataSource dataSource) {
    jdbcTemplate = new JdbcTemplate(dataSource);
}