使用Spring到@autowire mybatis映射器的NullPointerException

时间:2012-04-23 11:52:15

标签: java spring mybatis stripes

我一直在做一个webapp(一个db的CRUD接口),我使用jpetstore作为指导,但例外:

  1. 我也在使用mybatis-generator的代码。
  2. 服务。我不想将服务作为一个单独的部分。我想直接在Action bean上使用Mappers
  3. 我在一个文件夹(org.lmb97.data)中有所有mappers.xml,mappers.java,example.java和entities.java(所有生成器的代码)
  4. 当我尝试使用mapper时遇到NullPointerException。我检查了几次,但仍然没有找到解决方案。我不知道哪个是问题。我尽我所能......但不知道。我认为它与配置有关。我已经检查了bug跟踪器中的各种错误,这些错误似乎是相关的,但没有。

    我将在这里粘贴一些我认为有趣的部分:

    这是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>
    

    这是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>
    

    正如我所说,我不想拥有一个Services层,我直接在ActionBean上完成所有操作,在EventsActionBean https://github.com/txomon/Universidad/blob/eb7753e32a2eccbf4e5e81e815d68b929bcb2e61/3/LabTD/practica2/src/java/org/lmb97/web/action/EventsActionBean.java#L74的第76行抛出异常。我第一次打电话给映射器。

    我有apache tomcat的日志和普通日志,其中log4j在所有here中配置,它在.7z中,因为它们是很棒的日志。回购邮件是here,我正在提交确切的提交,这样如果我使用回购,你可以看到实际的状态。

    配置netbeans项目,以便在克隆repo时,包含所有库,没有外部依赖项。

    欢迎任何帮助/想法,你可以让我测试,

    欢呼并提前感谢你!

2 个答案:

答案 0 :(得分:2)

当我们谈论使用的罐子时,我们需要将特定注释与其相关域进行协调:

  • 动作类型bean的@SpringBean注释
  • @Autowire Spring类型bean的注释

答案 1 :(得分:0)

扫描仪不支持通配符,因此请更改:

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

对此:

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