【发布时间】:2014-07-23 17:45:36
【问题描述】:
伙计们,
我正在尝试使用 Spring DI 和 MVC (ver 3.2.x) 设置项目。我尝试注入服务实现,但在某些时候失败了。不抛出异常。只是服务对象在预期的地方为空。
下面是代码。
package com.test.domain.web.controller;
.........
@Controller
@RequestMapping(value = "/system/**")
public class InformationController {
@RequestMapping(value = "/classing/{id}", method = RequestMethod.GET)
public ModelAndView getBaleClassing(@PathVariable String id, Model model, HttpServletRequest request) {
LOG.info("In the system classing...");
InformationModel informationModel = new InformationModel();
model.addAttribute("informationModelVO",informationModel.getResult(id));
return new ModelAndView(SYSTEM_CLASSING_TILE);
}
}
这是业务的模型类。我还尝试在 setter 方法中注释 @Autowired。但没有运气。
package com.test.domain.classing.model;
.........
public class InformationModel {
@Autowired
InformationService informationService;
public InformationVO getResult(String id) {
InformationVO informationVO = null;
try {
informationVO = informationService.getInformationById(id); //informationService is null here
} catch (ServiceException e) {
LOG.error("Exception: ", e);
}
return informationVO;
}
public InformationService getInformationService() {
return informationService;
}
public void setInformationService(InformationService informationService) {
this.informationService = informationService;
}
}
这是我的服务实现,唯一符合条件的候选人。我依赖 Spring 来实例化,我不会自己在任何地方实例化服务或 DAO 实现。
package com.test.domain.classing.service;
.......
public class InformationServiceImpl implements InformationService {
@Autowired
private InformationDAO informationDAO;
public InformationServiceImpl() {
LOG.debug("In the InformationServiceImpl default constructor....");
}
public InformationVO getResult(String id) throws ServiceException {
InformationVO informationVO = informationDAO.getResult(id);
return informationVO;
}
public InformationDAO getInformationDAO() {
return informationDAO;
}
public void setInformationDAO(InformationDAO informationDAO) {
this.informationDAO = informationDAO;
}
}
而且,这是 spring 配置(包含服务和 dao)。我不会在 spring 配置中添加 InformationModel,因为我每次都需要新实例。
applicationContext.xml
..........
<context:annotation-config />
<context:component-scan base-package="com.test.domain, com.test.domain.classing.model, com.test.domain.classing.service, com.test.domain.web.controller" />
<bean name="com.test.domain.classing.service.InformationService" class="com.test.domain.classing.service.InformationServiceImpl">
</bean>
<bean name="com.test.domain.classing.dao.InformationDAO" class="com.test.domain.classing.dao.InformationDAOImpl">
</bean>
..........
mvcContext.xml
<context:component-scan base-package="gov.usda.fsa.pscao.clg.cops.web.controller" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
从日志中,我可以说 DAO 和服务的实现是实例化的。但是没有任何自动装配发生。即将DAO注入服务或将服务注入模型。
有没有办法在 spring 尝试注入时进行调试(如果是的话,在我的情况下)?
非常感谢任何指针。提前致谢。
【问题讨论】:
-
您需要注释模型 (
@Component) 和服务 (@Service) 类,以便 Spring 管理它们。 -
非常感谢大家!我将 InformationModel 作为原型范围的 bean,以便每次都创建它。现在它工作正常。
标签: java spring spring-mvc dependency-injection