我有一个spring项目,并且我在该项目中有单独的包,其中包含休息服务代码。
现在我的疑问是,我有接口和实现类,它已经从spring控制器类成功调用。但是当我尝试从RestService类调用接口和实现类时,通过使用@Autowired注释,我得到的是null值而不是获取对象。
代码是(不工作)
@Path("/sampleList")
public class Sample{
@Autowired
public SampleService sampleService;
@Autowired
public SampleVO sampleVO ;
// This method is called if JSON is request
@GET
@Produces(MediaType.APPLICATION_JSON)
public SampleVo sayXMLHello() throws Exception {
DBConnection dbConnection = new DBConnection();
MongoClient mongo = null;
try{
mongo = dbConnection.mongoDBConnection();
sampleVO .setEngineNameList(sampleService.someList(mongo));
}catch(Exception e){
throw e;
}finally{
dbConnection.mongoDBCloseConnection(mongo);
}
return sampleVO ;
}
}
这里sampleService和sampleVO返回null值,而不是object。
SampleService接口,SampleServiceImpl,SampleVO类
@Component
public interface SampleService {
//methods
}
@Component
public class SampleServiceImpl implements SampleService {
//methods
}
@Component
public class SampleVO {
//methods
}
Servlet上下文:
<context:component-scan base-package="com.sample" />
Spring控制器包如:com.sample.proj.controller Rest类包如:com.sample.proj.serviceapi.controller
Spring config
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<aop:aspectj-autoproxy />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/bootstrap/" />
<context:component-scan base-package="com.sample" />
<context:component-scan base-package="com.sample.proj.serviceapi.controller" />
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
</beans:beans>
代码工作:(服务和ServiceImpl,VO类与上面显示的相同)
@Controller
public class SampleController {
@Autowired
public SampleVO sampleVO;
@Autowired
public SampleService sampleService;
@RequestMapping(value="/", method =RequestMethod.GET)
public ModelAndView sampleMethod(HttpSession session) throws Exception{
sampleService.callSomeMethod();
}
}