Recently I came across one of his articles, A beginners guide to Dependency Injection. Dependency Injection is one of the interesting new design patterns mainly for web based applications. The moral of this pattern is injecting a required resource on demand rather creating the resource upfront. Inversion of control (IoC) is another idiom used for this design pattern. Some of the DI/IoC containers are ...
Spring framework PicoContainerHiveMind
XWork
XWork used in conjunction with WebWork has an inbuilt mechanism for IoC. But I decided to implement IoC mechanism for Webwork applications of my own. In an action class, sometimes we need some resources in the execute() method. These resources can be injected by a DI container. I wanted to build a DI/IoC mechanism which will pluggable and will not need any special configuration. The base model is ready now.
This model supports following features.
- Use of java annotations for injecting the resource. (A resource can be any object)
- No xml configuration is required (as there is no bean factory).
- Support for factory method for resource creation.
- Can create a singleton resource on demand.
- Lazy bean (resource) instantiation (unless specified as singleton).
- SINGLETON : Creates and maintains a single instance of the resource
- FACTORY : The resource has to be instantiated using factory method
- DEFAULT : A simple bean resource
package com.yogesh.container.test;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ActionSupport;
import com.yogesh.container.annotations.IoCResource;
import com.yogesh.container.annotations.ResourceType;
public class TestAction extends ActionSupport {
static final long serialVersionUID = -3473501884603263686L;
private Resource resource;
@IoCResource(Type=ResourceType.DEFALUT)
public void setResource(Resource resource) {
this.resource = resource;
}
public Resource getResource() {
return resource;
}
public String execute(){
//Do some work using resource
return Action.SUCCESS;
}
}
If not specified, the type of the annotation IoCResource will be default (ResourceType.DEFAULT). Other options which can be defined in the IoCResource are...
Class : Name of the class of the resource. Generally not required.Can be used to instantiate object of subclass of the argument object class.
FactoryClass : Name of the factory class which will create the resource. This should be used if Type is ResorceType.FACTORY
FactoryMethod : Name of the factory method which will actually create the resource. Has to be used in conjunction with FactoryClass
So if there exists a factory class which can return an instance of the resource, the annotation can be declared as
@IoCResource(Type=ResourceType.FACTORY,
FactoryClass="com.yogesh.container.test.ResourceFactory",
FactoryMethod = "getResource")
I am releasing this project under GPL version 2 and looking out for some webspace to upload it. I will put a link in this blog if I get some.
By that time, happy coding.
Yogesh Pandit
No comments:
Post a Comment