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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)