简体   繁体   English

如何加载Spring Application Context

[英]How to load Spring Application Context

I Have a Spring project. 我有一个Spring项目。 It works fine with junit test case. 它适用于junit测试用例。 juint calls the applicationcontext.xml and the project run successfully. juint调用applicationcontext.xml并且项目成功运行。 But i want to run the project without using jUnit. 但我想在不使用jUnit的情况下运行该项目。 Here is my jUnit Test Case 这是我的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());
    }
}

This testcase run the project. 此测试用例运行该项目。 But i want to use another class that can call the applicationcontext.xml and run the project. 但是我想使用另一个可以调用applicationcontext.xml并运行项目的类。

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 谢谢

Add this at the start of main 在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());

I am using in the way and it is working for me. 我正在使用它,它正在为我工​​作。

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

}

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

Here Student is my classm you will get the class matching yourBeanId. 这里学生是我的经典,你将获得与yourBeanId匹配的类。

Now work on that object with whatever operation you want to do. 现在使用您想要执行的任何操作来处理该对象。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Cucumber runner加载Spring应用程序上下文 - How to load Spring application context with Cucumber runner 如何在子模块中加载spring应用程序上下文 - How to load spring application context within submodule 如何加载应用程序上下文以使用 JUnit 4 测试 Spring 应用程序? - How to load application context for testing Spring application with JUnit 4? Spring Application Context Load Order - Spring Application Context Load Order 如何将bean加载到Spring MVC的应用程序上下文中? - How do you load a bean into Spring MVC's application context? 如何在Spring 2.5中从字符串加载应用程序上下文? - How to load application context from string in Spring 2.5? 如何在Spring 4.1.0应用程序上下文中加载系统属性和自定义资源? - How to load system properties and custom resources in the Spring 4.1.0 application context? Spring如何在不带OSGi的情况下通过Spring动态加载具有应用程序上下文的jar文件? - How to dynamically load jar files with application context by Spring (with no OSGi)? 如何在Tomcat / Java / Spring应用程序中将文件作为上下文参数值加载? - How to load a file as a context parameter value in a Tomcat/Java/Spring application? 使用 Camel 的 Spring Boot 无法加载应用程序上下文 - Spring Boot with Camel failed to load application context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM