- 讲师:刘萍萍 / 谢楠
- 课时:160h
- 价格 4580 元
特色双名师解密新课程高频考点,送国家电网教材讲义,助力一次通关
配套通关班送国网在线题库一套
在基于struts+spring+hibernate的开发框架下,一般service都是直接通过在Struts的action中getBean("yourServiceName")来获取,那么如果在serviceA中想调用serviceB中的方法该如何呢?
直接new 一个serviceB是不行的,因为里面可能还有依赖注入的dao等其他本来需要容器管理的资源,可以象在action中一样getBean()么?
获得applicationContext就可以了:
AppContext : public class AppContext {
return applicationContext;
public static void setApplicationContext(
AppContext.applicationContext = applicationContext;
}
public class SpringBeanService {
private static SpringBeanService instance;
private ApplicationContext applicationContext;
public static synchronized SpringBeanService getInstance() {
if (instance == null) {
instance = new SpringBeanService();
}
return instance;
}
public ApplicationContext getApplicationContext() {
return this.applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public UserService getUserService(){
return (UserService)AppContext.getApplicationContext().getBean("userService");
}
}
ApplicationContext的初始化:
public class ConfigLoadListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent contextEvent) {
WebApplicationContext context =WebApplicationContextUtils.getRequiredWebApplicationContext(contextEvent.getServletContext());
try {
String path=context2.getInitParameter("setting.properties");
Properties properties = new Properties();
GlobalConstant.setCmdbProperties(properties);
} catch (IOException e) {
}
System.out.println("系统无法初始化,异常退出");
}
}
这篇文章大家可以参考一下,和有类似之处:
责编:罗莉
课程专业名称 |
讲师 |
课时 |
查看课程 |
---|
课程专业名称 |
讲师 |
课时 |
查看课程 |
---|
点击加载更多评论>>