July 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Wiring Java Applications with Spring

  • May 12, 2005
  • By Michael Klaene
  • Send Email »
  • More Articles »

If you are not building an application from scratch, but must bow to the constraints of an existing code base, you can wire dependencies using existing factory objects with Spring's MethodInvokingFactoryBean. To do this, you simply define properties called "targetMethod" and "targetClass" to specify the name of the class and the static method to invoke to retrieve an instance.

A key benefit to creating beans via an ApplicationContext is that you have the ability to invoke 'lifecycle' methods on the objects. ApplicationContext (by way of extending BeanFactory) implements two lifecycle interfaces: InitializingBean and DisposableBean. So, suppose you wanted to setup a connection pool in SalesManagerJdbcDAO using the dbProperties map we specified in the configuration file. You could do this by coding an init() method on this object, and then add the following markup for the bean:

<bean id="salesManagerDAO"
      class="examples.SalesManagerJdbcDAO"
      init-method="init">

After the bean has been instantiated, meaning all properties have been set, Spring automatically invokes the init-method 'init()' for you. Likewise, if you wanted to dispose of the resources by invoking a destroy() method, you could do so by adding an additional element:

<bean id="salesManagerDAO"
      class="examples.SalesManagerJdbcDAO"
      init-method="init"
      destroy-method="destroy">

Building on the connection pool scenario, suppose you decided that you would rather not add these lifecycle methods directly to salesManagerDAO, but prefer to externalize those services as a separate class. You first write this Java class:

//Initialize and Destroy the DAO
public class DaoConfigurer() {

   public void init() {
      SalesManagerDAO salesManagerDAO =
         (SalesManagerDAO)ctx.getBean("salesManagerDAO");
       Map dbProperties = salesManagerDAO.getDbProperties();
      //perform initialization work...
      ...
   }

   public void destroy() {
      ...
   }
}

As you've seen, Spring automatically detects and coordinates the initialization of dependencies specified with <ref-bean>. However, DaoConfigurer does not have a reference to the DAO instance. Rather, it obtains a reference from the ApplicationContext inside the init method. Unless something is done to prevent it, an error may result if the ApplicationContext attempts to create DaoConfigurer before it creates the DAO bean. Spring's solution to this problem is the <depends-on> tag. Essentially, this tag tells Spring to stop what it is doing and instantiate the dependant object first. Here's the correct entry for this in applicationContext.xml:

<bean id="daoConfigurer"
      class="examples.DaoConfigurer"
      <depends-on="salesManagerDAO"/>
      init-method="init"
      destroy-method="destroy">

Although it's beyond the scope of this article, the ApplicationContext provides many other features. One of these is the capability to specify bean 'post processors' to provide a callback mechanism to Spring-managed beans, after they have been created. These callbacks enable you to do various things, such as perform additional 'initialization' tasks after a bean's instantiation. One example of a bean post processor is the PropertyPlaceHolderConfigurer that allows you to replace bean values with those specified in a properties file.

Last, it was mentioned that Spring provides utilities for obtaining the ApplicationContext singleton for use in your code. Unlike JNDI, Spring doesn't tie you to J2EE. You can use Spring in just about any Java-related project. However, if you are writing a J2EE application, you can make use of Spring's ContextLoader to access your ApplicationContext registry. This can be achieved by adding a listener, ContextLoaderListener, to a web.xml file:

<listener>
   <listener-class>
      org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>

An additional <context-param>, named contextConfigLocation, may be added to specify the location of your ApplicationContext's configuration file. It is only necessary if the file is somewhere other than /WEB-INF/applicationContext.xml, which is where it looks by default. You also split may your configuration data among multiple files. In this case, simply delimit the list of files in contextConfigLocation. Once you have configured web.xml, there are a number of ways to obtain the context after application startup. For example, if you are using the Struts framework, Spring provides an ActionSupport class that extends Struts Action. ActionSupport contains a method called getWebApplicationContext() that returns the ApplicationContext for your use.

Conclusion

This article provided an introduction to the Spring Framework. More specifically, an introduction to Spring's most powerful feature—its ability to wire applications through Inversion of Control. With Spring, it is easy to create an application that is both robust and flexible to future changes. Please keep in mind that Spring is much more than just an Inversion of Control container and offers a wide array of features to support enterprise development. You will find extensive documentation at the Spring Web site to assist you in your work.





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel