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