01: /* SpringUtil.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Thu Jun 1 13:53:53 2006, Created by henrichen
10: }}IS_NOTE
11:
12: Copyright (C) 2006 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: }}IS_RIGHT
16: */
17: package org.zkoss.zkplus.spring;
18:
19: import java.util.Map;
20: import java.util.HashMap;
21: import javax.servlet.ServletContext;
22:
23: import org.zkoss.zk.ui.Execution;
24: import org.zkoss.zk.ui.Executions;
25: import org.zkoss.zk.ui.UiException;
26:
27: import org.springframework.beans.factory.NoSuchBeanDefinitionException;
28: import org.springframework.context.ApplicationContext;
29: import org.springframework.web.context.support.WebApplicationContextUtils;
30:
31: /**
32: * SpringUtil, a Spring utility.
33: *
34: * @author henrichen
35: */
36: public class SpringUtil {
37: /**
38: * Get the spring application context.
39: */
40: public static ApplicationContext getApplicationContext() {
41: Execution exec = Executions.getCurrent();
42: if (exec == null) {
43: throw new UiException(
44: "SpringUtil can be called only under ZK environment!");
45: }
46:
47: return WebApplicationContextUtils
48: .getRequiredWebApplicationContext((ServletContext) exec
49: .getDesktop().getWebApp().getNativeContext());
50: }
51:
52: /**
53: * Get the spring bean by the specified name.
54: */
55: public static Object getBean(String name) {
56: Object o = null;
57: try {
58: o = getApplicationContext().getBean(name);
59: } catch (NoSuchBeanDefinitionException ex) {
60: // ignore
61: }
62: return o;
63: }
64:
65: /**
66: * Get the spring bean by the specified name and class.
67: */
68: public static Object getBean(String name, Class cls) {
69: Object o = null;
70: try {
71: o = getApplicationContext().getBean(name, cls);
72: } catch (NoSuchBeanDefinitionException ex) {
73: // ignore
74: }
75: return o;
76: }
77: }
|