Version 4.1 by Ken McWilliams on 2012/03/10 20:02

Show last authors
1 This will add Spring for the purpose of dependancy injection(DI) to our project. We will create a simple example showing DI and we will bring spring up to the latest version (as of this writing 3.1.1 however this method should apply for the near future).
2
3 This assumes that we are starting with [[a basic strusts2 application>>doc:Setting up a new Struts2 Project in Netbeans]].
4
5 == 1) Add the struts2-spring-plugin ==
6
7 The maven coordinates are:
8
9 **Artifact Id**: struts2-spring-plugin
10
11 **Group Id**: org.apache.struts
12
13 **Version**: 2.3.1.2 (or whatever version of struts2 you are using)
14
15 == 2) Configure web.xml and add applicationContext.xml to our project. ==
16
17 **web.xml** will need to look like the following:
18
19 <?xml version="1.0" encoding="UTF-8"?>
20 <web-app version="2.5" xmlns="http:~/~/java.sun.com/xml/ns/javaee" xmlns:xsi="http:~/~/www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:~/~/java.sun.com/xml/ns/javaee http:~/~/java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
21 <filter>
22 <filter-name>struts2</filter-name>
23 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
24 </filter>
25 <filter-mapping>
26 <filter-name>struts2</filter-name>
27 <url-pattern>/*</url-pattern>
28 </filter-mapping>
29 <context-param>
30 <param-name>contextConfigLocation</param-name>
31 <param-value>classpath:applicationContext.xml</param-value>
32 </context-param>
33 <listener>
34 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
35 </listener>
36 <welcome-file-list>
37 <welcome-file>/index.action</welcome-file>
38 </welcome-file-list>
39 </web-app>
40
41
42 **Add applicationContext.xml** to //src/main/resources// (same place log4j.xml)
43
44 <?xml version="1.0" encoding="UTF-8"?>
45 <beans xmlns="http:~/~/www.springframework.org/schema/beans"
46 xmlns:p="http:~/~/www.springframework.org/schema/p"
47 xmlns:context="http:~/~/www.springframework.org/schema/context"
48 xmlns:tx="http:~/~/www.springframework.org/schema/tx"
49 xmlns:xsi="http:~/~/www.w3.org/2001/XMLSchema-instance"
50 xmlns:aop="http:~/~/www.springframework.org/schema/aop"
51 xmlns:jdbc="http:~/~/www.springframework.org/schema/jdbc"
52 xmlns:jee="http:~/~/www.springframework.org/schema/jee"
53 xsi:schemaLocation="
54 http:~/~/www.springframework.org/schema/beans
55 http:~/~/www.springframework.org/schema/beans/spring-beans.xsd
56 http:~/~/www.springframework.org/schema/aop
57 http:~/~/www.springframework.org/schema/aop/spring-aop.xsd
58 http:~/~/www.springframework.org/schema/tx
59 http:~/~/www.springframework.org/schema/tx/spring-tx-3.0.xsd
60 http:~/~/www.springframework.org/schema/jdbc
61 http:~/~/www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
62 http:~/~/www.springframework.org/schema/context
63 http:~/~/www.springframework.org/schema/context/spring-context-3.0.xsd
64 http:~/~/www.springframework.org/schema/jee
65 http:~/~/www.springframework.org/schema/jee/spring-jee-3.0.xsd">
66 <context:annotation-config/>
67 <context:component-scan base-package="com.yourProjectBase" />
68 \\</beans>
69
70 Note the above contains more xml name spaces and schemas than nessasary for simple DI however it sets us up nicely for adding Java Persistene API in the next integration tutorial.
71
72 Beyond the name spaces and schemas there are three things happening:
73
74 1) context:annotation-config is used to activate annotations in beans already registered in the application context. To better understand this and to learn how to avoid using xml to register beans altogether please read this very nice answer on stack overflow: [[Difference between <context:annotation-config/> vs <context:component-scan/>>>url:http://stackoverflow.com/a/7456501/514065]]
75
76 2) Was also explained in the previous link. It allows us to define beans outside of the applicationContext via annotations, for that we supply the package(s) which it will scan. Again the above link is very useful in understanding this.
77
78 **Note**: We will add our beans on the blank line preceding </beans>
79
80 == 3) Create a basic DI example ==
81
82 The following assumes the use of the conventions plugin. For explanation of this plugin see: [[using the convention plugin>>doc:Adding and Using the Conventions Plugin]].
83
84 Our example will require the following things: 1) A service, 2) an implementation of the service, 3) configure spring to inject our service where we need it, 4) an action to use the service and 5) a view.
85
86 === Create Service ===
87
88 package com.example.project.service;
89 \\public interface Test {
90 String doTest();
91 }
92
93 === Create Service Implementation ===
94
95 package com.example.project.impl;
96 import com.example.project.service.Test;
97 \\public class TestImpl implements Test{
98 @Override
99 public String doTest() {
100 return "From test.";
101 }
102 }
103
104 === Configure Spring to Inject the Service ===
105
106 In applicationContext.xml add the following on the line before </beans>:
107
108 <bean id="test" class="com.example.project.impl.TestImpl"/>
109
110 === Create an Action to use the Bean ===
111
112 package com.example.project.action.test;
113 \\import com.example.project.service.Test;
114 import com.opensymphony.xwork2.ActionSupport;
115 import org.springframework.beans.factory.annotation.Autowired;
116 \\public class SpringTest extends ActionSupport{
117 @Autowired Test test;
118 public String output;
119 @Override
120 public String execute(){
121 output = test.doTest();
122 return SUCCESS;
123 }
124 }
125
126
127 === The View ===
128
129 Create the JSP// /WEB-INF/content/test/spring-test.jsp//
130
131 <%@taglib prefix="s" uri="/struts-tags"%>
132 <%@page contentType="text/html" pageEncoding="UTF-8"%>
133 <!DOCTYPE html>
134 <html>
135 <head>
136 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
137 <title>Spring Test</title>
138 </head>
139 <body>
140 <h1>Spring Test</h1>
141 Value from spring bean: <s:property value="output"/>
142 </body>
143 </html>
144
145 Run the example... and append **test/spring-test** to the end of your application path and you should see the result.