ky_lijin
ky_lijin
采纳率0%
2016-02-25 08:12 阅读 9.0k

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();
    }
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • wojiushiwo945you 毕小宝 2016-02-26 13: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单元测试自动注入依赖了。

    点赞 3 评论 复制链接分享
  • sinat_31535993 sinat_31535993 2016-02-25 08:56

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

    点赞 1 评论 复制链接分享
  • qianwenjun_19930314 chuhan_19930314 2018-01-16 06:35

    你好,你这个问题解决了吗?现在我也遇到这个问题,求指教

    点赞 评论 复制链接分享

相关推荐