Results 1 to 6 of 6

Thread: Dependency injection in batchs

  1. #1
    Join Date
    May 2007
    Posts
    9

    Default Dependency injection in batchs

    Hi,
    I've a simple need :
    I wish to use dependency injection on some batches.
    All of me batches inherit the same class (Batch). I'm searching for a class which I could use exactly as AbstractDependencyInjectionSpringContextTests.
    I would like to make the Batch commun class extends this class and just defined a method which would point to the needed configuration files.

    Like this :
    Code:
    public class Batch extends TheClassI'mSearchingFor{
        
    	protected String[] getConfigLocations() {
            return new String[] {"classpath:spring-application-context-batch.xml", "classpath:spring-application-context-service.xml","classpath:spring-application-context-persistance.xml" };
        }
    }
    Does such a class exist ? I'm still searching but I didn't managed to find it yet!
    Thank you in advance !

  2. #2
    Join Date
    Dec 2007
    Posts
    130

    Default

    I don't think that exists. AbstractDependencyInjectionSpringContextTests works, because it is executed within JUnit framework, which instantiates a test class and executes all the setup and test methods (Even easier when you use an IDE like eclipse which understands JUnit framework). You would need some class with a main method, that instantiates a batch and calls getConfigLocations.

  3. #3
    Join Date
    May 2007
    Posts
    9

    Default

    Hi Robert2D2 and thank you for your reply !
    I'm really amazed that such a class doesn't exist. How am I suppose to managed the dependency injection in the batch tier then?
    My problem is this : I've integrated Spring in a old application. There are 4 tiers : the web, the batch, the service and the dao tiers. I wish to use Spring Ioc in these tiers (to inject the dao's in hte service tier and the services in the web tier). It's working perfectly fine for the web tier (I defined a ContextLoaderPlugIn and a RequestContextFilterin my web.xml) and my Junit test (since my test classes inherit from AbstractDependencyInjectionSpringContextTests). But now I need to use Spring too in order for my batches to work (the batch tier is using the same services and dao's than the web tier). Does anyone have any idea how I could get this mess to work ?

  4. #4
    Join Date
    Dec 2007
    Posts
    130

    Default

    I am not sure how it can be done the way you are trying to do. I have some batches, all of them instantiated by Spring IoC, and with dependecies, but you need a class with a main method that loads context files. For example:

    Code:
    	private static String[] _config = new String[] {
    		"applicationContext.xml",
    		"applicationContext-batches.xml"};
    	
    	private static String BATCH_BEAN = "listaProcesos";
    
    	public static void main(String[] args) throws Exception {		
    		ApplicationContext appContext = new ClassPathXmlApplicationContext(_config);
    		IBatch batch = (IBatch)appContext.getBean(BATCH_BEAN, IBatch.class);
    		batchInicial.execute();		
    	}
    IBatch, is just an interface which declares a method called "execute()"

    That is, you may have a file with the configuration of all your batches, and get the one desired with appContext.getBean

    If you want more flexibility, think that the bean name and configuration files may be sent as command line arguments

  5. #5
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Quote Originally Posted by benfont View Post
    Hi Robert2D2 and thank you for your reply !
    I'm really amazed that such a class doesn't exist. How am I suppose to managed the dependency injection in the batch tier then?
    My problem is this : I've integrated Spring in a old application. There are 4 tiers : the web, the batch, the service and the dao tiers. I wish to use Spring Ioc in these tiers (to inject the dao's in hte service tier and the services in the web tier). It's working perfectly fine for the web tier (I defined a ContextLoaderPlugIn and a RequestContextFilterin my web.xml) and my Junit test (since my test classes inherit from AbstractDependencyInjectionSpringContextTests). But now I need to use Spring too in order for my batches to work (the batch tier is using the same services and dao's than the web tier). Does anyone have any idea how I could get this mess to work ?
    It's not clear what do you want to achieve. You said about the aim to perform dependency injection of particular layer beans and extending from AbstractDependencyInjectionSpringContextTests. The former is usual application configuration concern; the later is unit-testing concern. If you need to get dependencies configured by spring I don't see any difference in resolving dependencies for web layer and mysterious 'batch' layer. If you're about unit testing please specify the problem more precisely.

    P.S. try to avoid using the terms specific to your application to describe the problem because it becomes really unclear for another people.

  6. #6
    Join Date
    May 2007
    Posts
    9

    Smile

    I think I just got my answer thanks to Rober2D2 ! It's exactly what I need ! Thank you very much !! I didn't knew it could be done like that

    >>denis.zhdanov
    I'm sorry if my last message wasn't clear enough (I'm not use to write in English so I tend to cut my posts a little too much)! I just wanted to use Spring for dependency injection for my batch layer (a layer right on top of the service layer which contains any kind of treatments on my application datas, treatments whose are launch by a command line) and I didn't knew how to do it. I thought that there was a way to do it easily by using a class similar to AbstractDependencyInjectionSpringContextTests except that it would not concern unit-testing but batch process. But I was obviously wrong.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •