HTTP Status 500 - Request Processing failed; nested Exception is java.lang.Illegalargumentexception: Datasource must not be null

Asked

Viewed 454 times

3

When making a query in a dao class, the following error appears:

my spring-context.xml is:

<mvc:interceptors>
    <bean class="br.com.softwarescomerciais.os.interceptor.AutorizadorInterceptor"></bean>
</mvc:interceptors>
<mvc:default-servlet-handler/>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://192.168.101.178/sc"/>
    <property name="username" value="sfilho"/>
    <property name="password" value="123"/>
</bean>

my class dao:

@Autowired
private DataSource dataSource;

public boolean existe(Usuario usuario) {
    if (usuario == null) {
        throw new IllegalArgumentException("Usuário não deve ser nulo");
    } 

    String sql = "select NOME from USUARIOS where NOME = :NOME and SENHA = :SENHA";

    NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource); 
    Map<String, Object> parametros = new HashMap<String, Object>();
    parametros.put("NOME", usuario.getNome());
    parametros.put("SENHA", usuario.getSenha());

    SqlRowSet srs = jdbcTemplate.queryForRowSet(sql, parametros);

    boolean existe = srs.next();
    return existe;
}

error:

HTTP Status 500 - Request Processing failed; nested Exception is java.lang.Illegalargumentexception: Datasource must not be null

type Exception report

message Request Processing failed; nested Exception is java.lang.Illegalargumentexception: Datasource must not be null

Description The server encountered an Internal error that prevented it from fulfilling this request.

Exception

org.springframework.web.util.Nestedservletexception: Request Processing failed; nested Exception is java.lang.Illegalargumentexception: Datasource must not be null org.springframework.web.servlet.Frameworkservlet.processRequest(Frameworkservlet.java:943) org.springframework.web.servlet.Frameworkservlet.doPost(Frameworkservlet.java:833) javax.servlet.http.HttpServlet.service(Httpservlet.java:648) org.springframework.web.servlet.Frameworkservlet.service(Frameworkservlet.java:807) javax.servlet.http.HttpServlet.service(Httpservlet.java:729) org.apache.Tomcat.websocket.server.WsFilter.doFilter(Wsfilter.java:53) root cause

java.lang.Illegalargumentexception: Datasource must not be null org.springframework.util.Assert.notNull(Assert.java:112) org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.(Namedparameterjdbctemplate.java:89) br.com.softwarescomercial.os.dao.UsuarioDao.existe(Usuariodao.java:27) br.com.softwaremercial.os.controller.LoginController.effectLogin(Logincontroller.java:34) sun.reflect.Nativemethodaccessorimpl.invoke0(Native Method) sun.reflect.Nativemethodaccessorimpl.invoke(Unknown Source) sun.reflect.Delegatingmethodaccessorimpl.invoke(Unknown Source) java.lang.reflect.Method.invoke(Unknown Source) org.springframework.web.method.support.InvocableHandlerMethod.invoke(Invocablehandlermethod.java:214) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(Invocablehandlermethod.java:132) org.springframework.web.servlet.mvc.method.Annotation.ServletInvocableHandlerMethod.invokeAndHandle(Servletinvocablehandlermethod.java:104) org.springframework.web.servlet.mvc.method.Annotation.RequestMappingHandlerAdapter.invokeHandleMethod(Requestmappinghandleradapter.java:748) org.springframework.web.servlet.mvc.method.Annotation.RequestMappingHandlerAdapter.handleInternal(Requestmappinghandleradapter.java:689) org.springframework.web.servlet.mvc.method.Abstracthandlermethodadapter.Handle(Abstracthandlermethodadapter.java:83) org.springframework.web.servlet.Dispatcherservlet.doDispatch(Dispatcherservlet.java:945) org.springframework.web.servlet.Dispatcherservlet.doService(Dispatcherservlet.java:876) org.springframework.web.servlet.Frameworkservlet.processRequest(Frameworkservlet.java:931) org.springframework.web.servlet.Frameworkservlet.doPost(Frameworkservlet.java:833) javax.servlet.http.HttpServlet.service(Httpservlet.java:648) org.springframework.web.servlet.Frameworkservlet.service(Frameworkservlet.java:807) javax.servlet.http.HttpServlet.service(Httpservlet.java:729) org.apache.Tomcat.websocket.server.WsFilter.doFilter(Wsfilter.java:53)

  • 1

    You have loaded your spring-context.xml correctly into one of your configuration classes?

  • How so loaded? , sorry I’m new with spring.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.