Java 如何加载Spring应用程序上下文
我有一个春季项目。它在junit测试用例中运行良好。juint调用applicationcontext.xml,项目成功运行。但是我想在不使用jUnit的情况下运行这个项目。 这是我的jUnit测试用例Java 如何加载Spring应用程序上下文,java,spring,Java,Spring,我有一个春季项目。它在junit测试用例中运行良好。juint调用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
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 */
}
}
有人能建议我如何编码吗?谢谢将此添加到主菜单的开头
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());
我正在使用的方式,它是为我工作
public static void main(String[] args) {
new CarpoolDBAppTest();
}
public CarpoolDBAppTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
Student stud = (Student) context.getBean("yourBeanId");
}
学生是我的班级,你将得到与你的班级相匹配的班级
现在,使用您想要执行的任何操作来处理该对象。谢谢。它的装载量。但是当我运行测试用例时,我不需要调用其他类中的所有方法。它是自动执行的。但在这里,它只加载applicationcontext.xml。为什么会发生这种情况?如果您的applicationContext有所有要初始化的bean,那么这将正常工作。如果您想调用其他内容,则必须在加载应用程序上下文后手动执行。您希望您的主类如何运行?但在testcase中,它执行所有方法而不调用。这是因为您只有一个测试方法dude:)如果您将junit作为一个整体运行,则所有带
@test
注释的方法都将执行。您也可以运行特定的测试方法。但总的来说,您必须调用需要运行的方法。
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);
}
}