2 ky lijin ky_lijin 于 2016.02.25 16:12 提问

service利用注解注入,会空指针异常。而用getBean方式则可以获取到service。

spring配置文件如下:

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

<bean id="placeholderProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:conf/config.properties</value>
        </list>
    </property>
    <property name="fileEncoding" value="utf-8" />
</bean>

<import resource="spring-hibernate.xml" />

<!-- Import basic SpringMVC Resteasy integration -->  
<import resource="classpath:springmvc-resteasy.xml" />

<!-- 扫描文件 -->
<context:component-scan base-package="com.sinopec.carlife" />

service接口如下:

package com.sinopec.carlife.service.api;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.sinopec.carlife.model.Region;

@Path("/regions")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public interface RegionService {

/**
 * 获取所有城市
 * @return
 * @throws Exception
 */
@POST
@Path("/all")
public List<Region> findRegions() throws Exception;

}

service实现类如下:

package com.sinopec.carlife.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinopec.carlife.dao.RegionDAO;
import com.sinopec.carlife.model.Region;
import com.sinopec.carlife.service.api.RegionService;

@Service("regionService")
public class RegionServiceImpl implements RegionService {

@Autowired
private RegionDAO regionDAO;

@Override
public List<Region> findRegions() throws Exception {
    return regionDAO.findRegions();
}

}

junit测试如下:

package test.com.sinopec.carlife;

import java.util.List;

import javax.annotation.Resource;

import junit.framework.TestCase;

import org.junit.Test;

import com.sinopec.carlife.model.Region;
import com.sinopec.carlife.service.api.RegionService;

public class RegionServiceTest extends TestCase {

@Resource(name="regionService")
private RegionService regionService;

@Test
public void testFindRegions() {
    try {
        /*ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring.xml");
        regionService = (RegionService) context.getBean("regionService");*/
        List<Region> regions = regionService.findRegions();
        System.out.println(regions.size());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

2个回答

wojiushiwo945you
wojiushiwo945you   Ds   Rxr 2016.02.26 21:40

因为你的注解是spring容器解析的,要想让注解能够生效的话是需要spring容器启动的,这就是web项目启动环境下使用的。
你在junit里面测试,要想让Spring容器管理bean完成自动注入的话,可以使用Spring的junit测试工具注解。试试使用如下代码:

 import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import javax.annotation.Resource;
import junit.framework.TestCase;

import com.sinopec.carlife.model.Region;
import com.sinopec.carlife.service.api.RegionService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ={"classpath:spring/spring.xml"})
@Transactional
public class RegionServiceTest extends TestCase {
@Resource(name="regionService")
private RegionService regionService;


@Test
public void testFindRegions() {
    try {
        List<Region> regions = regionService.findRegions();
        System.out.println(regions.size());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这样就能使用spring自动为junit单元测试自动注入依赖了。

sinat_31535993
sinat_31535993   Rxr 2016.02.25 16:56

tomcat没启动,怎么加载配置?
用main方法测试,当然只能手动加载啦

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!