问题描述
- 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 {@Autowiredprivate RegionDAO regionDAO;@Overridepublic 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;@Testpublic 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(); }}
}
解决方案
因为你的注解是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""})@Transactionalpublic class RegionServiceTest extends TestCase {@Resource(name=""regionService"")private RegionService regionService;@Testpublic void testFindRegions() { try { List<Region> regions = regionService.findRegions(); System.out.println(regions.size()); } catch (Exception e) { e.printStackTrace(); }}
这样就能使用spring自动为junit单元测试自动注入依赖了。
解决方案二:
tomcat没启动,怎么加载配置?
用main方法测试,当然只能手动加载啦