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) [optional] Upgrade Spring to Latest Version ==
81
82 After adding the struts2-spring-plugin you'll see that the following jars were added to support it:
83
84 spring-aop-3.0.5.RELEASE.jar
85
86 spring-asm-3.0.5.RELEASE.jar
87
88 spring-beans-3.0.5.RELEASE.jar
89
90 spring-context-3.0.5.RELEASE.jar
91
92 spring-core-3.0.5.RELEASE.jar
93
94 spring-expression-3.0.5.RELEASE.jar
95
96 In netbeans 7.1, it is as simple as right clicking the dependancy node and adding the most current dependancy for each of the above a list of availible versions should be provided and you simply select the most current one.
97
98 You should be able to determine the coordinates:
99
100 **Groupd Id **for all the above is: org.springframework
101
102 Artifact Id for all the above is the first two words including the hypen, for example spring-aop-3.0.5.RELEASE.jar is spring-aop, then as previously stated pick the most recent version.
103
104 == 4) [optional] Basic DI example ==
105
106 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]].
107
108 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.
109
110 === Create Service ===
111
112 package com.example.project.service;
113 \\public interface Test {
114 String doTest();
115 }
116
117 === Create Service Implementation ===
118
119 package com.example.project.impl;
120 import com.example.project.service.Test;
121 \\public class TestImpl implements Test{
122 @Override
123 public String doTest() {
124 return "From test.";
125 }
126 }
127
128 === Configure Spring to Inject the Service ===
129
130 In applicationContext.xml add the following on the line before </beans>:
131
132 <bean id="test" class="com.example.project.impl.TestImpl"/>
133
134 === Create an Action to use the Bean ===
135
136 package com.example.project.action.test;
137 \\import com.example.project.service.Test;
138 import com.opensymphony.xwork2.ActionSupport;
139 import org.springframework.beans.factory.annotation.Autowired;
140 \\public class SpringTest extends ActionSupport{
141 @Autowired Test test;
142 public String output;
143 @Override
144 public String execute(){
145 output = test.doTest();
146 return SUCCESS;
147 }
148 }
149
150
151 === The View ===
152
153 Create the JSP// /WEB-INF/content/test/spring-test.jsp//
154
155 <%@taglib prefix="s" uri="/struts-tags"%>
156 <%@page contentType="text/html" pageEncoding="UTF-8"%>
157 <!DOCTYPE html>
158 <html>
159 <head>
160 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
161 <title>Spring Test</title>
162 </head>
163 <body>
164 <h1>Spring Test</h1>
165 Value from spring bean: <s:property value="output"/>
166 </body>
167 </html>
168
169 Run the example... and append **test/spring-test** to the end of your application path and you should see the result.
170
171 == Upgrade Spring to the Latest Version 3.1.1 ==
172
173 ...soon...
This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 14.3.1 - Documentation