如何加载Spring应用程序上下文

5

我有一个Spring项目。它可以通过Junit测试用例正常工作。Junit调用applicationcontext.xml文件,项目可以成功运行。但是我想不使用Junit来运行这个项目。

以下是我的Junit测试用例:

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());
    }
}

这个测试用例运行了项目。但是我想使用另一个类来调用applicationcontext.xml并运行该项目。

也就是说,

package com.dataload;

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

有人能向我提供如何编程的建议吗?谢谢

3个回答

11

将此内容添加到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());

谢谢。正在加载中。但是当我运行测试用例时,我不需要调用其他类中的所有方法。它会自动执行。但是在这里,它只加载applicationcontext.xml文件。为什么会发生这种情况? - user2094311
如果您的applicationContext具有所有要初始化的bean,则这将正常工作。如果您想调用其他内容,则必须在应用程序上下文加载后手动执行。您希望您的主类如何运行? - Anubhab
但是在测试用例中,它执行了所有方法而没有调用。 - user2094311
那是因为你只有一个测试方法,伙计 :) - Anubhab
如果您将JUnit作为一个整体运行,所有使用@Test注释的方法都将被执行。您也可以运行特定的测试方法。但在主函数中,您必须调用需要运行的方法。 - Anubhab
需要下载任何JAR包来使用ApplicationContextUtil吗? - user2094311

1

我正在使用这种方式,它对我很有效。

public static void main(String[] args) {
    new CarpoolDBAppTest();

}

public CarpoolDBAppTest(){
    ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
    Student stud = (Student) context.getBean("yourBeanId");
}

在这里,Student是我的同学,你将获得与yourBeanId匹配的类。

现在,使用任何想要执行的操作来处理该对象。


1
package com.dataload;

    public class insertCSV 
    {
        public static void main(String args[])
        {
            ApplicationContext context =
        new ClassPathXmlApplicationContext("applicationcontext.xml");


            // retrieve configured instance
            JobLauncher launcher = context.getBean("laucher", JobLauncher.class);
            Job job = context.getBean("job", Job.class);
            JobParameters jobParameters = context.getBean("jobParameters", JobParameters.class);
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接