码上敲享录 > Spring框架常见问题详解分享 > 普通类获取spring的bean对象

普通类获取spring的bean对象

上一章章节目录下一章 2019-07-08已有1578人阅读 评论(0)

普通类获取spring的bean对象


解决方法:

假如我想在普通类中获取CustomJdbcTemplate对象

ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

if (attributes == null) {

throw new RuntimeException("非web请求,非法调用!");

} else {

HttpServletRequest request = attributes.getRequest();

ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());

CustomJdbcTemplate jdbcTemplate = ac.getBean(CustomJdbcTemplate.class);

}


向大家推荐《Activiti工作流实战教程》:https://xiaozhuanlan.com/activiti
0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交