Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load Spring Application Context

Tags:

java

spring

I Have a Spring project. It works fine with junit test case. juint calls the applicationcontext.xml and the project run successfully. But i want to run the project without using jUnit. Here is my jUnit Test Case

package com.dataload;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.util.StopWatch;

@ContextConfiguration(locations = "classpath:com/dataload/applicationcontext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
public class SICSVDataTestCase {

    private final static Logger logger = Logger
            .getLogger(SICSVDataTestCase.class);

    @Autowired
    private JobLauncher launcher;

    @Autowired
    private Job job;
    private JobParameters jobParameters = new JobParameters();

    @Test
    public void testLaunchJob() throws Exception {
        StopWatch sw = new StopWatch();
        sw.start();
        launcher.run(job, jobParameters);
        sw.stop();
        logger.info(">>> TIME ELAPSED:" + sw.prettyPrint());
    }
}

This testcase run the project. But i want to use another class that can call the applicationcontext.xml and run the project.

That is,

package com.dataload;

public class insertCSV 
{
    public static void main(String args[])
    {
        /*  Code to run the project */
    }
}

Can anyone suggest me how to code? Thanks

like image 679
Shiju K Babu Avatar asked Mar 12 '13 09:03

Shiju K Babu


People also ask

How do I refresh application context in Spring boot?

For a Spring Boot Actuator application, some additional management endpoints are available. You can use: POST to /actuator/env to update the Environment and rebind @ConfigurationProperties and log levels. /actuator/refresh to re-load the boot strap context and refresh the @RefreshScope beans.

How do I load a spring context file in web xml?

Spring can be easily integrated into any Java-based web framework. All you need to do is to declare the ContextLoaderListener in your web. xml and use a contextConfigLocation to set which context files to load. You can then use the WebApplicationContext to get a handle on your beans.


1 Answers

Add this at the start of main

ApplicationContext context = new ClassPathXmlApplicationContext("path/to/applicationContext.xml");

JobLauncher launcher=(JobLauncher)context.getBean("launcher");
Job job=(Job)context.getBean("job");

//Get as many beans you want
//Now do the thing you were doing inside test method
StopWatch sw = new StopWatch();
sw.start();
launcher.run(job, jobParameters);
sw.stop();
//initialize the log same way inside main
logger.info(">>> TIME ELAPSED:" + sw.prettyPrint());
like image 67
Anubhab Avatar answered Sep 27 '22 17:09

Anubhab