ky_lijin 2016-02-25 08:12 采纳率: 0%
浏览 9241

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条回答 默认 最新

  • sinat_31535993 2016-02-25 08:56
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog