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:com.dinstone.jrpc.example.spring.ServiceWithSpring.java

private static void case02() {
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            "jrpc-example-case2.xml");

    HelloService rhsv1Netty = (HelloService) applicationContext.getBean("rhsv1-netty");
    System.out.println("rhsv1Netty");
    try {//  w  ww  . j  a va  2  s.c  om
        testHot(rhsv1Netty);
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        testSend1k(rhsv1Netty);
    } catch (Exception e) {
        e.printStackTrace();
    }

    HelloService rhsv1Mina = (HelloService) applicationContext.getBean("rhsv1-mina");
    System.out.println("rhsv1Mina");
    try {
        testHot(rhsv1Mina);
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        testSend1k(rhsv1Mina);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        System.in.read();
    } catch (IOException e) {
    }

    applicationContext.close();
}

From source file:edu.mayo.cts2.uriresolver.dao.DAOUtiltities.java

public static UriDAO connectDB(UriDAO uriDAO) {
    if (uriDAO != null && uriDAO.isConnected()) {
        return uriDAO;
    }//  w w  w . ja  v a  2  s. co  m

    DataSource ds;
    boolean inMemoryDBrequired = false;
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

    uriDAO = (UriDAOJdbc) context.getBean("uriJDBCTemplate");
    ds = (DataSource) context.getBean("dataSource");
    context.close();

    int connectionCode = uriDAO.checkDataSource(ds);

    if (connectionCode != 0) {
        inMemoryDBrequired = true;
        logConnectionError(connectionCode);
    } else if (!checkTablesExist(ds)) {
        inMemoryDBrequired = true;
    }

    // TODO: test data exists?  and correct columns ??

    if (inMemoryDBrequired) {
        ds = createInMemoryDatabase(uriDAO);
        if (ds == null) {
            return null;
        }
    }

    uriDAO.setDataSource(ds);
    return uriDAO;
}

From source file:org.hupo.psi.mi.psicquic.ws.IndexBasedPsicquicServiceTest.java

@BeforeClass
public static void beforeClass() throws Exception {
    InputStream mitabStream = IndexBasedPsicquicServiceTest.class
            .getResourceAsStream("/META-INF/brca2.mitab.txt");
    File indexDir = new File("target", "brca-mitab.index");

    Searcher.buildIndex(indexDir, mitabStream, true, true);

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "/META-INF/beans.spring.test.xml" });
    PsicquicConfig config = (PsicquicConfig) context.getBean("psicquicConfig");
    config.setIndexDirectory(indexDir.toString());

    service = (IndexBasedPsicquicService) context.getBean("indexBasedPsicquicService");
}

From source file:org.semispace.ws.client.SemiSpaceProxy.java

/**
 * An alternative way of performing lookup
 *//*  w  w  w.  j a va2 s  .com*/
protected static WsSpace readSpaceServiceAsSpring() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "org/semispace/ws/client/space-bean.xml" });
    WsSpace hw = (WsSpace) context.getBean("space");
    return hw;
}

From source file:com.thinkbiganalytics.server.KyloServerApplication.java

/**
 * Return the database version for Kylo.
 *
 * @return the version of Kylo stored in the database
 *//*from  w w  w. ja  v  a 2 s.com*/
private static KyloVersion getDatabaseVersion() {

    try {
        //ensure native profile is there for spring to load
        String profiles = System.getProperty("spring.profiles.active", "");
        if (!profiles.contains("native")) {
            profiles = StringUtils.isEmpty(profiles) ? "native" : profiles + ",native";
            System.setProperty("spring.profiles.active", profiles);
        }
        //Spring is needed to load the Spring Cloud context so we can decrypt the passwords for the database connection
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
                "kylo-upgrade-check-application-context.xml");
        ctx.refresh();
        KyloUpgradeDatabaseVersionChecker upgradeDatabaseVersionChecker = (KyloUpgradeDatabaseVersionChecker) ctx
                .getBean("kyloUpgradeDatabaseVersionChecker");
        KyloVersion kyloVersion = upgradeDatabaseVersionChecker.getDatabaseVersion();
        ctx.close();
        return kyloVersion;
    } catch (Exception e) {
        log.error(
                "Failed get the database version prior to upgrade.  The Kylo Upgrade application will load by default. {}",
                e.getMessage());
    }
    return null;

}

From source file:com.setronica.ucs.server.MessageServer.java

private static ClassPathXmlApplicationContext bootstrapMongo(String mongoUrls) throws UnknownHostException {
    List<ServerAddress> seeds = new ArrayList<>();
    for (String s : mongoUrls.split(",")) {
        int idx = s.indexOf(":");
        ServerAddress address;//from   w  w w  .j  a v a 2s .c o  m
        if (idx > 0) {
            address = new ServerAddress(s.substring(0, idx), Integer.parseInt(s.substring(idx + 1)));
        } else {
            address = new ServerAddress(s);
        }
        seeds.add(address);
    }
    //create parent BeanFactory
    DefaultListableBeanFactory parentBeanFactory = new DefaultListableBeanFactory();
    //register your pre-fabricated object in it
    parentBeanFactory.registerSingleton("addresses", seeds);
    ClassPathXmlApplicationContext applicationContext = initApplication("mongo", parentBeanFactory);

    MongoClient client = (MongoClient) applicationContext.getBean("mongoClient");
    logger.info("addresses: " + client.getAllAddress());
    return applicationContext;
}

From source file:org.hupo.psi.mi.psicquic.ws.IndexBasedPsicquicRestServiceTest.java

@BeforeClass
public static void beforeClass() throws Exception {
    File indexDir = new File("target", "brca-mitab.index");

    Searcher.buildIndex(indexDir,/*from w  ww  .ja v  a 2s .  c  om*/
            IndexBasedPsicquicServiceTest.class.getResourceAsStream("/META-INF/brca2.mitab.txt"), true, true);
    Searcher.buildIndex(indexDir,
            IndexBasedPsicquicServiceTest.class.getResourceAsStream("/META-INF/400.mitab.txt"), false, true);

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "/META-INF/beans.spring.test.xml" });
    PsicquicConfig config = (PsicquicConfig) context.getBean("psicquicConfig");
    config.setIndexDirectory(indexDir.toString());

    service = (IndexBasedPsicquicRestService) context.getBean("indexBasedPsicquicRestService");
}

From source file:com.digitalgeneralists.assurance.model.ModelUtils.java

public static String calculateHashForFile(File file) {
    Logger logger = Logger.getLogger(ModelUtils.class);

    String result = null;//from   w ww.j a  v  a  2 s.c  om

    if (file != null) {
        // 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");
            IFileComparor comparor = (IFileComparor) springContext.getBean("FileCompareValidator");

            byte[] hash = comparor.calculateHashForFile(file);
            if (hash != null) {
                result = hash.toString();
            } else {
                result = "";
            }
            if (result.length() > 512) {
                result = result.substring(0, 511);
            }

            comparor = null;
        } catch (NoSuchAlgorithmException e) {
            result = "";
            logger.warn(e);
        } catch (IOException e) {
            result = "";
            logger.warn(e);
        } finally {
            if (springContext != null) {
                springContext.close();
            }
            springContext = null;
        }
    }

    return result;
}

From source file:com.digitalgeneralists.assurance.model.ModelUtils.java

public static IInitializableEntity initializeEntity(IInitializableEntity entity, String propertyKey) {

    IInitializableEntity result = null;//  w ww.ja  v  a  2  s  .c o m

    if (entity != null) {
        if (!Hibernate.isInitialized(entity.getPropertyToInitialize(propertyKey))) {
            // 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.initializeEntity(entity, propertyKey);

                modelDelegate = null;
            } finally {
                if (springContext != null) {
                    springContext.close();
                }
                springContext = null;
            }
        } else {
            result = entity;
        }
    }

    return result;
}

From source file:org.semispace.ws.client.SemiSpaceTokenProxy.java

/**
 * An alternative way of performing lookup
 *//*from ww  w .  ja v a  2s.  co m*/
protected static TokenWsSpace readSpaceServiceAsSpring() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "org/semispace/ws/client/tokenspace-bean.xml" });
    TokenWsSpace hw = (TokenWsSpace) context.getBean("space");
    return hw;
}