Example usage for org.springframework.context.support FileSystemXmlApplicationContext setConfigLocation

List of usage examples for org.springframework.context.support FileSystemXmlApplicationContext setConfigLocation

Introduction

In this page you can find the example usage for org.springframework.context.support FileSystemXmlApplicationContext setConfigLocation.

Prototype

public void setConfigLocation(String location) 

Source Link

Document

Set the config locations for this application context in init-param style, i.e.

Usage

From source file:net.phoenix.thrift.server.Server.java

/**
 * ? spring?/*from   w w w .  ja  v  a  2 s . c o  m*/
 * @param args
 * @throws Exception
 */
public static int main(String[] args) throws Exception {
    if (args.length == 0 || StringUtils.isEmpty(args[0])) {
        LOG.error("Please assign spring configuration file . ");
        return 0;
    }
    FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext();
    context.setConfigLocation(args[0]);
    context.registerShutdownHook();
    context.refresh();
    context.close();
    return 1;
}

From source file:com.predic8.membrane.annot.bean.MCUtil.java

@SuppressWarnings("unchecked")
public static <T> T fromXML(Class<T> clazz, final String xml) {
    final String MAGIC = "magic.xml";

    FileSystemXmlApplicationContext fsxacApplicationContext = new FileSystemXmlApplicationContextExtension(
            MAGIC, xml);/*from  w  ww  . j a v a  2s .  c  om*/
    fsxacApplicationContext.setConfigLocation(MAGIC);

    fsxacApplicationContext.refresh();

    Object bean = null;

    if (fsxacApplicationContext.containsBean("main")) {
        bean = fsxacApplicationContext.getBean("main");
    } else {
        Collection<T> beans = fsxacApplicationContext.getBeansOfType(clazz).values();
        if (beans.size() > 1)
            throw new InvalidParameterException(
                    "There is more than one bean of type '" + clazz.getName() + "'.");
        bean = beans.iterator().next();
    }

    if (bean == null)
        throw new InvalidParameterException("Did not find bean with ID 'main'.");

    if (!clazz.isAssignableFrom(bean.getClass()))
        throw new InvalidParameterException("Bean 'main' is not a " + clazz.getName() + " .");

    return (T) bean;
}

From source file:com.browseengine.bobo.api.BoboIndexReader.java

private static Collection<FacetHandler<?>> loadFromIndex(File file, WorkArea workArea) throws IOException {
    // File springFile = new File(file, SPRING_CONFIG);
    // FileSystemXmlApplicationContext appCtx =
    //   new FileSystemXmlApplicationContext("file:" + springFile.getAbsolutePath());
    //return (Collection<FacetHandler<?>>) appCtx.getBean("handlers");

    Set<Entry<Class<?>, Object>> entries = workArea.map.entrySet();
    FileSystemXmlApplicationContext appCtx = new FileSystemXmlApplicationContext();
    for (Entry<Class<?>, Object> entry : entries) {
        Object obj = entry.getValue();
        if (obj instanceof ClassLoader) {
            appCtx.setClassLoader((ClassLoader) obj);
            break;
        }//from w ww .j a  v a 2 s.co m
    }

    String absolutePath = file.getAbsolutePath();
    String partOne = absolutePath.substring(0, absolutePath.lastIndexOf(File.separator));
    String partTwo = URLEncoder.encode(absolutePath.substring(absolutePath.lastIndexOf(File.separator) + 1),
            "UTF-8");
    absolutePath = partOne + File.separator + partTwo;

    File springFile = new File(new File(absolutePath), SPRING_CONFIG);
    appCtx.setConfigLocation("file:" + springFile.getAbsolutePath());
    appCtx.refresh();

    return (Collection<FacetHandler<?>>) appCtx.getBean("handlers");

}

From source file:org.springframework.data.hadoop.admin.util.HadoopWorkflowUtils.java

/**
 * create and register Spring Batch job/*  w  w w.  j a  v a  2 s .com*/
 * 
 * @param context root application context
 * @param artifacts workflow artifacts which include workflow descriptor and class loader information.
 * @throws Exception
 */
public static void createAndRegisterSpringBatchJob(ApplicationContext context,
        final WorkflowArtifacts artifacts) throws SpringHadoopAdminWorkflowException {
    if (context == null) {
        logger.warn("root applicaton context is null");
        throw new SpringHadoopAdminWorkflowException("root applicaton context is null");
    }
    String workflowDescriptor = getWorkflowDescriptor(artifacts);
    logger.info("create spring batch job:" + workflowDescriptor + ", classloader:"
            + artifacts.getWorkflowClassLoader());

    final FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext();
    ctx.setClassLoader(artifacts.getWorkflowClassLoader());
    ctx.setConfigLocation(HadoopWorkflowUtils.fileURLPrefix + workflowDescriptor);

    try {
        SimpleJob job = new SimpleJob(generateSpringBatchJobName(artifacts));
        TaskletStep step = new TaskletStep(springHadoopTaskName);
        SimpleSpringHadoopTasklet tasklet = new SimpleSpringHadoopTasklet();
        tasklet.setContext(ctx);
        step.setTasklet(tasklet);
        JobRepository jobRepository = context.getBean(JobRepository.class);
        DataSourceTransactionManager transactionManager = context.getBean("transactionManager",
                DataSourceTransactionManager.class);
        step.setTransactionManager(transactionManager);
        step.setJobRepository(jobRepository);
        step.afterPropertiesSet();
        job.addStep(step);
        JobRegistry jobRegistry = context.getBean("jobRegistry", JobRegistry.class);
        job.setJobRepository(jobRepository);
        job.afterPropertiesSet();
        JobFactory jobFactory = new ReferenceJobFactory(job);
        jobRegistry.register(jobFactory);
    } catch (Exception e) {
        logger.warn("create and register Spring Batch Job failed");
        throw new SpringHadoopAdminWorkflowException("create and register Spring Batch Job failed", e);
    }
}

From source file:org.springframework.data.hadoop.admin.workflow.support.FileSystemApplicationContextFactory.java

/**
 * Creates an {@link ApplicationContext} from the provided path.
 * /*from  ww w  .j  a va  2  s .co m*/
 * @see ApplicationContextFactory#createApplicationContext()
 */
public ConfigurableApplicationContext createApplicationContext() {
    logger.info("resource is:" + resource);
    if (resource == null) {
        return parent;
    }
    try {
        logger.info("create application context, resource:" + resource.getFile().getAbsolutePath()
                + ", classloader:" + this.beanClassLoader + ". Parent is:" + parent);
        FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext();
        context.setClassLoader(this.beanClassLoader);
        context.setParent(parent);
        context.setConfigLocation(HadoopWorkflowUtils.fileURLPrefix + resource.getFile().getAbsolutePath());
        context.refresh();
        return context;
    } catch (Exception e) {
        try {
            logger.error(
                    "create application context fail. with resource:" + resource.getFile().getAbsolutePath(),
                    e);
        } catch (IOException e1) {
            logger.error("log error", e1);
        }
    }
    return parent;
}