Thursday 29 October 2009

Sproing: Arbitrary Spring loading

Sometimes is is desirable to load spring when an arbitrary (ie, not containing a main() method) object is created. Sproing provides this for when another application is creating java objects in order to provide a route into your code (for example FitNesse, The Grinder etc) that you need to wire into spring. Sproing does this by loading any application context files you specify and then wiring the object into it, allowing you to inject beans at runtime.

Spring beans are weired using the AutowireCapableBeanFactory.AUTOWIRE_BY_NAME strategy, so your object needs to have setter methods with the same names as the beans you want to wire up. You need to create a subclass of ApplicationWirer and implement the getPaths() method to return the paths of the spring application context files you want to wire up

For example:


public class YourAppWirer extends ApplicationWirer {
public String[] getPaths() {
return new String[] {
"classpath:someApplicationContext.xml",
"classpath:applicationContext.xml"};
}
}




To wire up your object, all you need do is create a new instance of your overridden ApplicationWirer class in a constructor on your object, and call the wire() method

For example, using YourAppWirer in the above example:


public class JediMindTrick {
public JediMindTrick() {
new YourAppWirer().wire(this);
}
}



Provided you have defined public setter methods for any spring dependencies your object needs, and called them the same names as described above, spring will inject these at runtime. You can then write your object methods as normal

Sproing version 0.3.1alpha is available now from Sith Open Source

Thursday 15 October 2009

Creating a Spring application

Creating a standalone (ie non-web) application can seem a little confusing to some. Sproing makes this easy by extending a Sproing class and implementing a method, and that's it!

All you need do is have your main (the class with the main() method that starts the application) extend ApplicationWirer and implement the getPaths() method that returns the path(s) to your application context xml files. Then a single call to the wire() method does the rest



import uk.org.sith.sproing.spring.ApplicationWirer;
public class Main extends ApplicationWirer {
public static void main(String[] args) {
Main main = new Main();
main.wire(main);
}

public String[] getPaths() {
return new String[] {"applicationContext.xml"};
}
}



This code will start your application and load the applicationContext.xml file into spring with all the spring beans defined. Easy!


Sproing version 0.3.1alpha is available now from Sith Open Source

Saturday 3 October 2009

Sproing: Spring static ApplicationContext access

It is often useful to gain access to the ApplicationContext object in order to locate existing beans. Sproing provides a ApplicationContextProvider class that can be used anywhere in a spring application to access the application context like so:


ApplicationContext ctx = ApplicationContextProvider.getApplicationContext();


If you have used Sproing to start the application and/or load Spring, you will automatically have access to this class. If you want to use this feature of Sproing in a web application, all you need to do is declare the ApplicationContextProvider as a spring bean in your spring xml file ie


<bean id="applicationContextProvider" class="uk.org.sith.sproing.spring.ApplicationContextProvider"/>


Sproing version 0.3.1alpha is available now from Sith Open Source