Java 找不到在我的xml文件中定义的Bean
我正在学习与RESTfulWebService相关的java代码,我在控制器中定义了一个Webservice,如下所示。这是别人写的 我想弄清楚一些事情Java 找不到在我的xml文件中定义的Bean,java,spring,hibernate,Java,Spring,Hibernate,我正在学习与RESTfulWebService相关的java代码,我在控制器中定义了一个Webservice,如下所示。这是别人写的 我想弄清楚一些事情 @RequestMapping(value="/updateemployeestatushistory", method=RequestMethod.GET) public String updateemployeestatushistory ( @RequestParam(value="employee_id"
@RequestMapping(value="/updateemployeestatushistory", method=RequestMethod.GET)
public String updateemployeestatushistory
(
@RequestParam(value="employee_id", defaultValue="0") Integer employee_id,
@RequestParam(value="company_id", defaultValue="0") Integer company_id,
@RequestParam(value="new_status_id", defaultValue="0") Integer new_status_id,
@RequestParam(value="new_review_status_id", defaultValue="0") Integer new_review_status_id,
@RequestParam(value="changer_id", defaultValue="0") Integer changer_id,
@RequestParam(value="emp_status_change_comment", defaultValue="") String emp_status_change_comment,
@RequestParam(value="company_status_change_comment", defaultValue="") String company_status_change_comment
)
{
try {
// Get and validate the employee .
employeeDao rpDao = context.getBean("employeeDao", employeeDaoImpl.class);
employee employee = rpDao.findByemployeeId(employee_id);
if (employee == null) { throw new Exception("Invalid employee "); }
// Use the employee status dao to 1) update the employee and 2) add a new history record.
employeeStatusDao rpsDao = context.getBean("employeeStatusDao", employeeStatusDaoImpl.class);
rpsDao.updateStatus(context, changer_id, employee, emp_status_change_comment, new_status_id, company_status_change_comment, new_review_status_id);
result.setWebServiceStatus("Updated employee_ table, Inserted employee__HISTORY record", true);
logger.info("Status history successfully updated");
} catch(Throwable th) {
th.printStackTrace();
}
}
上面的控制器代码在我的主项目中,它引用了一个依赖项(另一个项目)。我在bean所在的位置定义了一个applicationcontext.xml
以以下方式定义并基于上述代码,我希望xml代码中有类似的内容:
<bean id="employeeStatusDao" class="abc.xyz.employee.orm.dao.impl.EmployeeStatusDaoImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/>
</bean>
使用依赖项注入而不是查找。。。。通常,当您访问
ApplicationContext
时,请远离键盘,重新思考您使用的方法通常是错误的。@M.Deinum感谢您的回复。你能告诉我怎么做吗?我不熟悉这一点。为协作者定义字段并在其上放置@Autowired
。根据使用接口的XML名称判断,类型应该与接口匹配,而不是与具体实现匹配。
<bean id="employeeHistoryDao" class="abc.xyz.employee.orm.dao.impl.EmployeeHistoryDaoImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/>
</bean>