我在概念测试中使用mybatis-spring。当我运行测试时,所有人都在使用我的数据库,但是在服务中使用代码时,我得到一个java.lang.NullPointerException

我的DaoTest:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/spring-config.xml")
public class DaoTest {

    @Autowired
    private IOrdenMgmtDao ordenDao;

    @Test
    public void testDAO(){
        Assert.assertNotNull(ordenDao);
        Integer countForOrden = ordenDao.getCountByState("NEW");
        Assert.assertEquals(new Integer(3), countForOrden);
        List<OrdenDetailStateResult> ordenDetail = ordenDao.getOrderDetail("NEW");
        Assert.assertNotNull(ordenDetail);

        for (OrdenDetailStateResult ordenDetailStateResult : ordenDetail) {
            System.out.println(ordenDetailStateResult.getOrden_id() + " "
                + ordenDetailStateResult.getComprador_email() + " "
                + ordenDetailStateResult.getStatus());
        }
    }
}


测试结果还可以:

...
13:48:29.027 [main] DEBUG o.a.p.b.i.I.getOrderDetail - <==      Total: 3
5835561 [email protected] NEW
5835562 [email protected] NEW
5835761 [email protected] NEW
...


但是,当我尝试使用http://localhost:8080/batisrest/webapi/orders/NEW休息服务时,我得到:

java.lang.NullPointerException org.agonzalez.batisrest.service.OrderService.getCountByState(OrderService.java:22)

我通过OrderResource类访问它:

@Path("/orders")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class OrderResource {

    @Autowired
    private OrderService orderService;

    @GET
    @Path("/{orderStatus}")
    public String getOrdersByStatus(@PathParam("orderStatus") String  orderStatus){
        return Integer.toString(orderService.getCountByState(orderStatus));
    }
}


我的OrderService类:

@ContextConfiguration(locations="/spring-config.xml")
public class OrderService {

    @Autowired
    private IOrdenMgmtDao ordenDao;

    public int getCountByState(String orderStatus){
        Integer countForOrden = ordenDao.getCountByState(orderStatus);
        return countForOrden;
    }

}


与mybatis相关的IOrdenMgmtDao:

public interface IOrdenMgmtDao {

    public Integer getCountByState(
            @Param("ordenStatus") final String ordenStatus);

}


我认为主要问题是来自配置,但我看不到它。有我的spring-config.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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://mybatis.org/schema/mybatis-spring
    http://mybatis.org/schema/mybatis-spring-1.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

    <mybatis-spring:scan base-package="org.agonzalez.batisrest.**" />

    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
        //[Database configuration working]
    </bean>

    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
</beans>


还有我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring-config.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>org.agonzalez</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>


我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.agonzalez</groupId>
    <artifactId>batisrest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>batisrest</name>
    <url>http://maven.apache.org</url>

    <build>
        <finalName>batisrest</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <properties>
        <jersey.version>2.21</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.3-1102-jdbc41</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.2.RELEASE</version>
        </dependency>
    </dependencies>
</project>


如我所说,测试正在与我的数据库连接。

最佳答案

您需要在OrderResource类中自动连接orderService。

更改

OrderService orderService = new OrderService();


至:

@Autowired
private OrderService orderService;


避免在Spring应用程序中使用new运算符创建对象。由于您是使用new运算符创建oderService的,因此不会具有dao对象所需的依赖关系。

07-26 09:27