20051009 Adding Spring to my webapp
Sunday October 09, 2005
Category: Spring
Have started to learn about Spring, and am excited about integrating it with my current projects. Today I started making small changes to some of the classes to use the Spring Core package, more for the IoC features. My objective is to make my changes as unobtrusive as possible, and to affected only the few classes that I am changing.


Just to note down the few changes I have made.


1. First, I added the dependency for the Spring Framework in my maven 1 project.xml:
   <dependency>
      <groupId>springframework</groupId>
      <artifactId>spring</artifactId>
      <version>1.2.2</version>
      <type>jar</type>
      <properties>
      	<war.bundle>true</war.bundle>
      </properties>
    </dependency>
2. After this, I added the my context definition file applicationContext-main to the folder under which I store my util classes at src/java/com/mydomain/util. The application context definition can be anyway, as long as it can be found by the context loader later.


3. All my JUnit test classes extend from a BaseTestCase, so I added the following code block in BaseTestCase:
    protected final static ApplicationContext ctx;
    // This static block ensures that Spring's BeanFactory is only loaded
    // once for all tests
    static {
        String pkg = ClassUtils.classPackageAsResourcePath(Util.class);
        String[] paths = {"classpath*:/" + pkg + "/applicationContext-*.xml",
                          "classpath*:META-INF/applicationContext-*.xml"};
        ctx = new ClassPathXmlApplicationContext(paths);
   }
I got the block of code above from Matt Raible's AppFuse, a great project that uses Spring extensively.

The only change I made was to specify Util.class, which tells ClassUtils to return a string converted from the package name of the class. I don't really have any use for specifying the META-INF path yet, but I guess there is no harm leaving it in.


4. In my JUnit tests, I get the references to the objects I need by calling ctx.getBean().


5. In building the classes for running the unit tests, there are two approaches that I take - one is to build the project from within Eclipse, and another is to build the project from the command line using maven.

Somehow in Eclipse the applicationContext-*.xml files are copied to the target folder under the same location as that in the src folder, but in maven, I needed to specifically write a postGoal to perform the copying as follows:
  <goal name="copy.springconfiguration" 
  description="copy applicationContext-*.xml files to the appropriate package">
    <copy todir="${maven.build.dir}/classes/com/mydomain/util">
      <fileset dir="${basedir}/src/java/com/mydomain/util" 
includes="**/applicationContext-*.xml" />
    </copy>
  </goal>
  <postGoal name="java:compile">
    <attainGoal name="copy.springconfiguration" />
  </postGoal>



5. To enable the loading of the application context in the web app, I had to modify the web.xml file with the following additions:
  protected WebApplicationContext _springCtx = null;
  ...
  // Under setServlet, add
  _springCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(getSerlvetContext);
7. In the action class that we need the Spring beans, we call the application context directory to return the object:
    private DateUtil dateUtil = null;
    ... 
    dateUtil = (DateUtil) _springCtx.getBean("dateUtil");
where dateUtil is the id of the bean wired in the context file.


With these steps done, I can start to do IoC in seleted struts action files, and move the code gradually to Spring.
Posted on (2005-10-08 14:10:41.0) Permalink Comments [2]
Trackback URL: http://jroller.com/trackback/dunpanic/Weblog/adding_spring_to_my_webapp
Comments:

You should actually just take advantage of the spring-provided
AbstractDependencyInjectionSpringContextTests
and related classes. See here:
http://static.springframework.org/spring/docs/1.2.x/reference/testing.html

They will allow you to dependency inject your test cases themselves. AbstractTransactionalSpringContextTests will even wrap each test method with a transaction.

Regards,


--
Colin Sampaleanu
Interface21 Principal Consultant
Spring Training, Consulting and Support - "From the Source"
http://www.springframework.com

Posted by Colin Sampaleanu (216.138.221.115) on October 09, 2005 at 02:31 AM SGT
Website: http://blog.exis.com/colin #

Hi, Colin,
Thanks for the pointer. However, as I also wanted to extend my BaseTestCase from another class (in this case, it is MockObjectTestCase from jmock), I was not able to come up with a way to extend both AbstractTransactionalSpringContextTests and this class. Is there any sites/articles you can point me to that discuss this issue about needing to extend from both the AbstractTransactionalSpringContextTests and other classes?

Thanks in advance.

choongyong

Posted by choongyong.koh (203.125.38.114) on October 10, 2005 at 06:15 PM SGT
Website: http://www.jroller.com/page/dunpanic #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: On

Please answer this simple math question

1 + 81 =