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.thymeleaf.spring4.resourceresolver.SpringResourceResourceResolverSpring4Test.java

@Test
public void testGetResourceAsStream() throws Exception {

    final String templateLocation = "spring421/view/test.html";

    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "classpath:spring421/view/applicationContext.xml");

    final SpringResourceResourceResolver resolver = (SpringResourceResourceResolver) context
            .getBean("springResourceResourceResolver");

    final TemplateProcessingParameters parameters = new TemplateProcessingParameters(new Configuration(),
            "test", new Context());

    final InputStream is = resolver.getResourceAsStream(parameters, "classpath:" + templateLocation);

    final String testResource = ResourceUtils.normalize(ResourceUtils.read(is, "US-ASCII"));

    final String expected = ResourceUtils
            .read(ClassLoaderUtils.getClassLoader(SpringResourceResourceResolverSpring4Test.class)
                    .getResourceAsStream(templateLocation), "US-ASCII", true);

    Assert.assertEquals(expected, testResource);

}

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

@Test
public void testOutboundAdapterLoadWithFullSettings() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "OutboundChannelAdapterParserTest-full-context.xml", this.getClass());

    Object bean = context.getBean("outbound-adapter");
    assertNotNull("Adapter not found", bean);
    System.out.println(bean.getClass());

    DirectFieldAccessor beanDirect = new DirectFieldAccessor(bean);
    Object value = beanDirect.getPropertyValue("handler");
    assertNotNull("'handler' not found", value);
    System.out.println(value.getClass());

    WritingMessageHandler a = (WritingMessageHandler) value;
    DirectFieldAccessor adapterDirect = new DirectFieldAccessor(a);
    assertNotNull("'template' not found", adapterDirect.getPropertyValue("template"));
    assertEquals(TestPropertiesAccessor.getDefaultContainerName(),
            adapterDirect.getPropertyValue("containerName"));
    assertNotNull("'blobNameBuilder' queue not found", adapterDirect.getPropertyValue("blobNameBuilder"));
    assertTrue(adapterDirect.getPropertyValue("blobNameBuilder") instanceof MockBlobNameBuilder);
}

From source file:se.vgregion.delegation.server.Server.java

private void start() {
    String path = "classpath:/spring/conf.xml";
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(path);
    propertiesBean = (PropertiesBean) ctx.getBean("propertiesBean");

    String hostname = "localhost";

    try {//from ww w  .  java  2s  .com
        hostname = InetAddress.getLocalHost().getHostName();
        LOGGER.info("Host namne = " + hostname);
    } catch (UnknownHostException e) {
        LOGGER.error("Host namne error ", e);
    }

    startServer(ctx, hostname, propertiesBean.getServerPort());
}

From source file:org.constretto.spring.assembly.AssemblyWithMultipleEnvironmentsTest.java

private void loadContextAndInjectConfigurationService() {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
            "org/constretto/spring/assembly/AssemblyWithMultipleEnvironmentsTest-context.xml");
    configurationService = (ConfigurationService) ctx.getBean("configurationService");
}

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

@Override
protected Object doInBackground() throws Exception {
    // 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 {// ww w.ja  v  a 2s . c o m
        springContext = new ClassPathXmlApplicationContext("/META-INF/spring/app-context.xml");
        IModelDelegate modelDelegate = (IModelDelegate) springContext.getBean("ModelDelegate");

        modelDelegate.deleteScan(scan);
        modelDelegate = null;
    } finally {
        if (springContext != null) {
            springContext.close();
        }
        springContext = null;
    }

    return this;
}

From source file:com.chevres.rss.restapi.controller.UserController.java

@CrossOrigin
@RequestMapping(path = "/user/{username}", method = RequestMethod.GET)
@ResponseBody/*  ww w .  j a v a 2s  .  c om*/
public ResponseEntity<String> getUser(@RequestHeader(value = "User-token") String userToken,
        @PathVariable String username) {

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    UserDAO userDAO = context.getBean(UserDAO.class);
    UserAuthDAO userAuthDAO = context.getBean(UserAuthDAO.class);

    UserAuth userAuth = userAuthDAO.findByToken(userToken);
    if (userAuth == null) {
        context.close();
        return new ResponseEntity(new ErrorMessageResponse("invalid_token"), HttpStatus.BAD_REQUEST);
    }

    User user = userDAO.findByUsername(username);
    if (user == null) {
        context.close();
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    }

    boolean isAdmin = userDAO.isAdmin(userAuth.getIdUser());
    if (!isAdmin && (userAuth.getIdUser() != user.getId())) {
        return new ResponseEntity(new ErrorMessageResponse("admin_required"), HttpStatus.FORBIDDEN);
    }

    context.close();

    return new ResponseEntity(new SuccessGetUserResponse(user.getUsername(), user.getType()), HttpStatus.OK);
}

From source file:com.chevres.rss.restapi.controller.UserController.java

@CrossOrigin
@RequestMapping(path = "/users", method = RequestMethod.GET)
@ResponseBody/*  w w w .  ja v  a 2 s.  c om*/
public ResponseEntity<String> getUsers(@RequestHeader(value = "User-token") String userToken) {

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    UserDAO userDAO = context.getBean(UserDAO.class);
    UserAuthDAO userAuthDAO = context.getBean(UserAuthDAO.class);

    UserAuth userAuth = userAuthDAO.findByToken(userToken);
    if (userAuth == null) {
        context.close();
        return new ResponseEntity(new ErrorMessageResponse("invalid_token"), HttpStatus.BAD_REQUEST);
    }

    boolean isAdmin = userDAO.isAdmin(userAuth.getIdUser());
    if (!isAdmin) {
        context.close();
        return new ResponseEntity(new ErrorMessageResponse("admin_required"), HttpStatus.FORBIDDEN);
    }

    List<User> users = userDAO.findEveryone();
    List<SuccessGetUserWithIdResponse> finalList = new ArrayList<>();
    for (User user : users) {
        finalList.add(new SuccessGetUserWithIdResponse(user.getId(), user.getUsername(), user.getType()));
    }

    context.close();

    return new ResponseEntity(new SuccessGetUsersResponse(finalList), HttpStatus.OK);
}

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

@Override
public SwingWorker<Object, Object> doInBackground() {
    // 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 {//from   w ww . jav  a2s  . c  o m
        springContext = new ClassPathXmlApplicationContext("/META-INF/spring/app-context.xml");
        IModelDelegate modelDelegate = (IModelDelegate) springContext.getBean("ModelDelegate");

        modelDelegate.deleteScanDefinition(scanDefinition);
        modelDelegate = null;
    } finally {
        if (springContext != null) {
            springContext.close();
        }
        springContext = null;
    }

    return this;
}

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

@Override
public SwingWorker<Object, Object> doInBackground() {
    // 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 {//ww  w  .  ja va2s . c  om
        springContext = new ClassPathXmlApplicationContext("/META-INF/spring/app-context.xml");
        IModelDelegate modelDelegate = (IModelDelegate) springContext.getBean("ModelDelegate");

        modelDelegate.saveScanDefinition(scanDefinition);
        modelDelegate = null;
    } finally {
        if (springContext != null) {
            springContext.close();
        }
        springContext = null;
    }

    return this;
}

From source file:com.opengsn.controller.manager.CloudManagerClient.java

public CloudManagerClient() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "webapp/WEB-INF/applicationContext.xml" });
    cloudMgr = (ICloudManager) context.getBean("cloudManagerClient");
}