1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.spring;
23
24 import javax.servlet.ServletContext;
25
26 import org.apache.struts2.StrutsConstants;
27 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
28 import org.springframework.context.ApplicationContext;
29 import org.springframework.web.context.support.WebApplicationContextUtils;
30 import org.springframework.web.context.WebApplicationContext;
31
32 import com.opensymphony.xwork2.inject.Inject;
33 import com.opensymphony.xwork2.spring.SpringObjectFactory;
34 import com.opensymphony.xwork2.util.logging.Logger;
35 import com.opensymphony.xwork2.util.logging.LoggerFactory;
36
37
38
39 /***
40 * Struts object factory that integrates with Spring.
41 * <p/>
42 * Spring should be loaded using a web context listener
43 * <code>org.springframework.web.context.ContextLoaderListener</code> defined in <code>web.xml</code>.
44 *
45 */
46 public class StrutsSpringObjectFactory extends SpringObjectFactory {
47 private static final Logger LOG = LoggerFactory.getLogger(StrutsSpringObjectFactory.class);
48
49
50
51
52
53
54
55
56
57 /***
58 * Constructs the spring object factory
59 * @param autoWire The type of autowiring to use
60 * @param alwaysAutoWire Whether to always respect the autowiring or not
61 * @param useClassCacheStr Whether to use the class cache or not
62 * @param servletContext The servlet context
63 * @since 2.1.3
64 */
65 @Inject
66 public StrutsSpringObjectFactory(
67 @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE,required=false) String autoWire,
68 @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_AUTOWIRE_ALWAYS_RESPECT,required=false) String alwaysAutoWire,
69 @Inject(value=StrutsConstants.STRUTS_OBJECTFACTORY_SPRING_USE_CLASS_CACHE,required=false) String useClassCacheStr,
70 @Inject ServletContext servletContext) {
71
72 super();
73 boolean useClassCache = "true".equals(useClassCacheStr);
74 LOG.info("Initializing Struts-Spring integration...");
75
76 ApplicationContext appContext = (ApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
77 if (appContext == null) {
78
79 String message = "********** FATAL ERROR STARTING UP STRUTS-SPRING INTEGRATION **********\n" +
80 "Looks like the Spring listener was not configured for your web app! \n" +
81 "Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.\n" +
82 "You might need to add the following to web.xml: \n" +
83 " <listener>\n" +
84 " <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>\n" +
85 " </listener>";
86 LOG.fatal(message);
87 return;
88 }
89
90 this.setApplicationContext(appContext);
91
92 int type = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
93 if ("name".equals(autoWire)) {
94 type = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME;
95 } else if ("type".equals(autoWire)) {
96 type = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE;
97 } else if ("auto".equals(autoWire)) {
98 type = AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT;
99 } else if ("constructor".equals(autoWire)) {
100 type = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR;
101 }
102 this.setAutowireStrategy(type);
103
104 this.setUseClassCache(useClassCache);
105
106 this.setAlwaysRespectAutowireStrategy("true".equalsIgnoreCase(alwaysAutoWire));
107
108 LOG.info("... initialized Struts-Spring integration successfully");
109 }
110 }