简体   繁体   English

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

[英]NullPointerException using Spring to @autowire mybatis mapper

I have been doing a webapp (a CRUD interface for a db) and I am using as guideline the jpetstore, with the exception that: 我一直在做一个webapp(一个db的CRUD接口),我使用jpetstore作为指导,但例外情况是:

  1. I am also using the mybatis-generator's code. 我也在使用mybatis-generator的代码。
  2. Services. 服务。 I don't want to have services as a separate part. 我不想将服务作为一个单独的部分。 I want to use the Mappers directly on the Action bean 我想直接在Action bean上使用Mappers
  3. I have all mappers.xml, mappers.java, example.java and entities.java (all the generator's code) in one folder (org.lmb97.data) 我在一个文件夹(org.lmb97.data)中有所有mappers.xml,mappers.java,example.java和entities.java(所有生成器的代码)

And I am encountering a NullPointerException when I try to use a mapper. 当我尝试使用mapper时,我遇到了NullPointerException。 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. 我已经检查了bug跟踪器中的各种错误,这些错误似乎是相关的,但没有。

I am going to paste here some parts that I consider interesting: 我将在这里粘贴一些我认为有趣的部分:

This is the applicationContext.xml 这是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.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 . 正如我所说的,我不想拥有一个服务层,我直接在ActionBean上完成所有操作,在EventsActionBean的第76行抛出异常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. 我第一次打电话给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. 我有apache tomcat的日志和普通日志,在这里配置了log4j,它在.7z中,因为它们是很棒的日志。 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. 配置netbeans项目,以便在克隆repo时,包含所有库,没有外部依赖项。

Any help/idea is welcome, and you can ask me to test, 欢迎任何帮助/想法,你可以让我测试,

Cheers and thank you in advance! 干杯,谢谢你提前!

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 动作类型bean的@SpringBean注释
  • @Autowire annotation for Spring type beans @Autowire对Spring类型bean的注释

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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM