Question

I have been doing a webapp (a CRUD interface for a db) and I am using as guideline the jpetstore, with the exception that:

  1. I am also using the mybatis-generator's code.
  2. Services. I don't want to have services as a separate part. I want to use the Mappers directly on the Action bean
  3. I have all mappers.xml, mappers.java, example.java and entities.java (all the generator's code) in one folder (org.lmb97.data)

And I am encountering a NullPointerException when I try to use a mapper. I checked all several times, but still not getting to the solution. I don't know which is the problem. I have tried all I could... but no idea. I think it is something with the configuration. I have checked various bugs in the bug tracker that seemed to be related but none was.

I am going to paste here some parts that I consider interesting:

This is the applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

    <!-- transaction manager, use JtaTransactionManager for global tx -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://192.168.56.2/txomon_lmb97"/>
        <property name="username" value="username"/>
        <property name="password" value="password"/>
    </bean>

    <!-- In theory, this has to be for making a transaction manager (don't know what it is for) -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="org.lmb97.data" />
    </bean>
    <!-- enable component scanning (beware that this does not enable mapper scanning!) -->    
    <context:component-scan base-package="org.lmb97" />

    <!-- enable autowire -->
    <context:annotation-config />

    <!-- enable transaction demarcation with annotations -->
    <tx:annotation-driven />

    <!-- scan for mappers and let them be autowired -->
    <bean id="mybatisMapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.lmb97.data.*Mapper" />
    </bean>


</beans>

This is the web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>    
    <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>StripesResources</param-value>
    </context-param>

             <!-- Aqui empiezo a definir Stripes -->
    <filter>
        <display-name>Stripes Filter</display-name>
        <filter-name>StripesFilter</filter-name>
        <filter-class>net.sourceforge.stripes.controller.StripesFilter</filter-class>
        <init-param>
            <param-name>ActionResolver.Packages</param-name>
            <param-value>org.lmb97.web.action</param-value>
        </init-param>
        <init-param>
            <param-name>Interceptor.Classes</param-name>
            <param-value>net.sourceforge.stripes.integration.spring.SpringInterceptor</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>StripesFilter</filter-name>
        <servlet-name>StripesDispatcher</servlet-name>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    <servlet>
        <servlet-name>StripesDispatcher</servlet-name>
        <servlet-class>net.sourceforge.stripes.controller.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>StripesDispatcher</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    <!--Aqui empiezo a definir Spring -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

As I said, I don't want to have a Services layer, I am doing it all directly on the ActionBean, the exception is thrown in line 76 of the EventsActionBean https://github.com/txomon/Universidad/blob/eb7753e32a2eccbf4e5e81e815d68b929bcb2e61/3/LabTD/practica2/src/java/org/lmb97/web/action/EventsActionBean.java#L74 . I the first call to the mapper.

I have the apache tomcat's log and the normal log, with log4j configured in ALL here it is in .7z because they are great logs. And the repo is here , I am putting the exact commit, so that if I work with the repo, you can see the actual state.

The netbeans project is configured so that if you clone the repo, you have all libraries included with it, with no external dependencies.

Any help/idea is welcome, and you can ask me to test,

Cheers and thank you in advance!

Was it helpful?

Solution

When we are talking about the jars used we need to coordinate the specific annotations with their concerned domains:

  • @SpringBean annotation for action type beans
  • @Autowire annotation for Spring type beans

OTHER TIPS

The scanner does not support wildcards, so change this:

<property name="basePackage" value="org.lmb97.data.*Mapper" />

To this:

<property name="basePackage" value="org.lmb97.data" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top