Example usage for org.springframework.context.support ClassPathXmlApplicationContext getBean

List of usage examples for org.springframework.context.support ClassPathXmlApplicationContext getBean

Introduction

In this page you can find the example usage for org.springframework.context.support ClassPathXmlApplicationContext getBean.

Prototype

@Override
    public Object getBean(String name) throws BeansException 

Source Link

Usage

From source file:org.alfresco.util.exec.RuntimeExecBeansTest.java

public void testSplitArgumentsAsSingleValue() throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(APP_CONTEXT_XML);
    try {//from w w w.j a  v  a2s. c o m
        RuntimeExec splitExec = (RuntimeExec) ctx.getBean("commandSplitArgumentsAsSingleValue");
        assertNotNull(splitExec);
        String[] splitCommand = splitExec.getCommand();
        assertTrue(
                "Command arguments not split into 'dir', '.' and '..' : " + Arrays.deepToString(splitCommand),
                Arrays.deepEquals(new String[] { "dir", ".", ".." }, splitCommand));
    } finally {
        ctx.close();
    }
}

From source file:org.opencredo.cloud.storage.si.adapter.config.InboundChannelAdapterParserTest.java

@Test
public void testInboundAdapterLoadWithMinimumSettings() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "InboundChannelAdapterParserTest-context.xml", this.getClass());

    Object bean = context.getBean("inbound-adapter");
    assertNotNull("Adapter not found", bean);
    System.out.println(bean.getClass());
    DirectFieldAccessor d = new DirectFieldAccessor(bean);
    Object value = d.getPropertyValue("source");
    assertNotNull("Source not found", value);
    System.out.println(value.getClass());

    ReadingMessageSource rms = (ReadingMessageSource) value;
    DirectFieldAccessor adapterDirect = new DirectFieldAccessor(rms);
    assertNotNull("'template' not found", adapterDirect.getPropertyValue("template"));
    assertNotNull("'filter' queue not found", adapterDirect.getPropertyValue("filter"));
    assertTrue(adapterDirect.getPropertyValue("filter") instanceof AcceptOnceBlobNameFilter);
    assertEquals(TestPropertiesAccessor.getDefaultContainerName(),
            adapterDirect.getPropertyValue("containerName"));
}

From source file:com.ryantenney.metrics.spring.ProxyTargetClassTest.java

@Test
public void positiveContextLoadingTest() {
    ClassPathXmlApplicationContext ctx = null;
    try {// w w w . j  a v  a  2  s. c o m
        ctx = new ClassPathXmlApplicationContext("classpath:proxy-target-class-enabled.xml");
        Assert.assertNotNull("Expected to be able to get ProxyTargetClass by class.",
                ctx.getBean(ProxyTargetClass.class));
        Assert.assertNotNull("Expected to be able to get ProxyTargetClass from AutowiredCollaborator.",
                ctx.getBean(AutowiredCollaborator.class).getDependency());
    } finally {
        if (ctx != null) {
            ctx.close();
        }
    }
}

From source file:net.xqx.controller.web.MarkQueryController.java

/**
 * ?/*from  ww w .  j a va  2  s.co m*/
 * 
 * @return
 * @throws SQLException
 */
@RequestMapping("/markQuery")
public String markQuery(HttpServletRequest request) throws SQLException {
    TMark mark = null;
    String userName = request.getParameter("userName");
    String admissionNo = request.getParameter("admissionNo");
    String licenseNo = request.getParameter("licenseNo");
    String year = request.getParameter("year");

    String input = request.getParameter("rand");

    String rand = (String) request.getSession().getAttribute("rand");
    if (input != null && rand != null) {
        if (!input.equals(rand)) {
            request.setAttribute("CodeError2", "???");
            return "redirect:markList.do";
        }
    }
    System.out.println(System.getProperty("java.endorsed.dirs"));
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "spring/services.xml" });
    AchievementService achievementService = (AchievementService) context.getBean("achievement");
    List<Achievement> list = null;
    try {
        list = (List<Achievement>) achievementService.getAchievements(userName, admissionNo, licenseNo, year);
    } catch (SOAPFaultException e) {
        request.setAttribute("error", "??!");
    } catch (WebServiceException ex) {
        request.setAttribute("error", ",???!");
    }
    Achievement achievement = null;
    if (list != null && list.size() > 0) {
        achievement = list.get(0);
    }
    request.setAttribute("achievement", achievement);
    // 
    Sort hotNewsSort = new Sort(Direction.DESC, "fdjTimes", "ffbTime");
    Pageable hotNewsRecPageable = new PageRequest(0, 8, hotNewsSort);
    List<TNews> hotNewsList = newsDao.getHotNews(hotNewsRecPageable).getContent();
    request.setAttribute("hotNewsList", hotNewsList);

    // ??
    Sort recNewsSort = new Sort(Direction.DESC, "fIsRecord", "ffbTime");
    Pageable recNewsRecPageable = new PageRequest(0, 8, recNewsSort);
    List<TNews> recNewsList = newsDao.getNewsRec(recNewsRecPageable).getContent();
    request.setAttribute("recNewsList", recNewsList);
    request.setAttribute("mark", mark);
    return "web/markDetail";
}

From source file:com.ryantenney.metrics.spring.ReporterTest.java

@Test
public void realReporters() throws Throwable {
    ClassPathXmlApplicationContext ctx = null;
    try {//from  w  w w .  j  a v  a 2  s.  c  om
        ctx = new ClassPathXmlApplicationContext("classpath:reporter-test.xml");
        // intentionally avoids calling ctx.start()

        Assert.assertNotNull(ctx.getBean(ConsoleReporter.class));
        Assert.assertNotNull(ctx.getBean(JmxReporter.class));
        Assert.assertNotNull(ctx.getBean(Slf4jReporter.class));
        Assert.assertNotNull(ctx.getBean(GraphiteReporter.class));
        Assert.assertNotNull(ctx.getBean(GangliaReporter.class));
    } finally {
        if (ctx != null) {
            ctx.close();
        }
    }
}

From source file:org.datalift.sdmxdatacube.SDMXDataCubeModel.java

/**
 * Creates a new SDMXDataCubeModel instance.
 * /*from   w w w . j a  v a2  s .  c o m*/
 * @param name
 *            Name of the module.
 */
public SDMXDataCubeModel(String name) {
    super(name);

    // Initialize the sdmxDataCubeTransformer, which is a Spring bean.
    // It is also referenced in spring-beans.xml.
    Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();
    ctx.setConfigLocation("spring/spring-beans.xml");
    ctx.refresh();

    sdmxDataCubeTransformer = ctx.getBean(SDMXDataCubeTransformer.class);
}

From source file:org.jvnet.hudson.mojo.MetaBuildMojo.java

@Override
public void execute() throws MojoExecutionException, MojoFailureException {

    // TODO://from   ww w .  j  a  va  2  s  .c o m
    // TODO: 1) Parse the metabuild.xml

    MetaBuildParser parser = new MetaBuildParser(metaBuildInputFile);
    metaBuild = parser.parse();

    // TODO: 2) Generate job config.xml-s
    // TODO:
    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
            "classpath:/META-INF/spring/core-context.xml");
    JobConfigGenerator generator = (JobConfigGenerator) appContext.getBean("jobConfigGenerator");
    for (JobConfig jobConfig : metaBuild.getJobConfigs()) {
        getLog().info(jobConfig.toString());

        getLog().info(" + Generating config file for project '" + jobConfig.getProjectName() + "':");
        getLog().info("   - Subversion URL: " + jobConfig.getScmURL());
        getLog().info("   - Maven goals:    " + jobConfig.getMavenGoals());
        getLog().info("");

        File dir = new File(outputDirectory + File.separatorChar + jobConfig.getModuleName());
        if (!dir.exists())
            //noinspection ResultOfMethodCallIgnored
            dir.mkdirs();

        // if (jobConfig.allowsGoalOverriding() && metaBuild.getGoals().get)
        //    jobConfig.set

        generator.setJobConfig(jobConfig);
        generator.setOutputDirectory(dir.getAbsolutePath());
        generator.generate();
    }
}

From source file:com.digitalgeneralists.assurance.ui.workers.RestoreDeletedItemWorker.java

@Override
protected ComparisonResult doInBackground() throws Exception {
    this.notifier.fireEvent(new DeletedItemRestoreStartedEvent(this.result));

    ComparisonResult result = null;// w  w  w . java2  s  . c  o m

    // Because we are operating in a Swing application, the session context
    // closes after the initial bootstrap run. There appears to be an
    // "application"-level session configuration that should enable the
    // behavior we want for a desktop application, but there is no documentation
    // for it that I can find. 
    // So, Assurance mimics a web app request/response
    // cycle for calls into the model delegate through the Swing
    // application. All calls to the model initiated directly from the UI
    // essentially operate as a new "request" and rebuild the Hibernate and
    // Spring session contexts for use within that operation.
    ClassPathXmlApplicationContext springContext = null;
    try {
        springContext = new ClassPathXmlApplicationContext("/META-INF/spring/app-context.xml");
        IModelDelegate modelDelegate = (IModelDelegate) springContext.getBean("ModelDelegate");

        result = modelDelegate.restoreDeletedItem(this.result, this);
        modelDelegate = null;
    } finally {
        if (springContext != null) {
            springContext.close();
        }
        springContext = null;
    }

    return result;
}

From source file:org.alfresco.util.exec.RuntimeExecBeansTest.java

public void testDeprecatedSetCommandMap() throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(APP_CONTEXT_XML);
    try {/*from  w ww. jav  a2  s .  c o m*/
        RuntimeExec deprecatedExec = (RuntimeExec) ctx.getBean("commandCheckDeprecatedSetCommandMap");
        assertNotNull(deprecatedExec);
        // Execute it
        deprecatedExec.execute();
    } finally {
        ctx.close();
    }
    // The best we can do is look at the log manually
    logger.warn("There should be a warning re. the use of deprecated 'setCommandMap'.");
}

From source file:org.alfresco.util.exec.RuntimeExecBeansTest.java

public void testFailureModeOfMissingCommand() {
    File dir = new File(DIR);
    dir.mkdir();// w ww .  j  a  v a  2 s  .c o m
    assertTrue("Directory not created", dir.exists());

    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(APP_CONTEXT_XML);
    try {
        RuntimeExec failureExec = (RuntimeExec) ctx.getBean("commandFailureGuaranteed");
        assertNotNull(failureExec);
        // Execute it
        ExecutionResult result = failureExec.execute();
        assertEquals("Expected first error code in list", 666, result.getExitValue());
    } finally {
        ctx.close();
    }
}