Example usage for org.springframework.context ApplicationContext getBean

List of usage examples for org.springframework.context ApplicationContext getBean

Introduction

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

Prototype

Object getBean(String name) throws BeansException;

Source Link

Document

Return an instance, which may be shared or independent, of the specified bean.

Usage

From source file:com.rmzone.core.App.java

public static void main(String[] args) {
    // set up mongo
    ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
    MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");

    // create and save some users
    List<User> users = new ArrayList<User>();
    users.add(new User("1001", "foo", "bar"));
    users.add(new User("1002", "mickey", "mouse"));

    for (User user : users)
        mongoOperation.save("users", user);

    // create and save some documents
    Tire tire = new Tire("Michelin", "Alpine", 1, users.get(0), Calendar.getInstance().getTime(), 14, 12, 3);
    mongoOperation.save("documents", tire);

    Engine engine = new Engine("Model T", "Wow", 1, users.get(0), Calendar.getInstance().getTime());
    engine.setHorsePower(16.4f);//from  w ww.  j a v a2s  . c  o  m
    mongoOperation.save("documents", engine);

    Car car = new Car("Pinto", "don't touch me!", 1, users.get(0), Calendar.getInstance().getTime());
    car.setMake("Ford");
    car.setColor("Blue");
    car.setEngine(engine);

    // sorry not very elegant
    car.setTires(new ArrayList<Tire>());
    for (int i = 0; i < 4; i++)
        car.getTires().add(tire);
    mongoOperation.save("documents", car);

    // dump all the data        
    List<User> listUser = mongoOperation.getCollection("users", User.class);
    System.out.println("Number of users = " + listUser.size());
    for (User user : listUser)
        System.out.println(user.toString());

    List<DocumentMaster> listDocument = mongoOperation.getCollection("documents", DocumentMaster.class);
    System.out.println("Number of documents = " + listDocument.size());
    for (DocumentMaster document1 : listDocument)
        System.out.println(document1.toString());

    // delete everything
    mongoOperation.dropCollection("users");
    mongoOperation.dropCollection("documents");
}

From source file:de.wpsverlinden.dupfind.DupFind.java

public static void main(String[] args) {

    ApplicationContext ctx = new ClassPathXmlApplicationContext("AppConfig.xml");
    DupFind app = (DupFind) ctx.getBean("dupFind");
    try {/*from  ww w.  j  ava 2  s  .  c  om*/
        app.run();
    } catch (IOException ex) {
        Logger.getLogger(DupFind.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.illmeyer.polygraph.dist.simple.Polygraph.java

/**
 * @param args//from   w w  w . j a  v  a  2  s  .  c  o  m
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    ApplicationContext context = new FileSystemXmlApplicationContext(args[1]);
    DefaultGunConfigurator gc = new DefaultGunConfigurator();
    gc.setActiveTemplate(args[0]);
    gc.initialize();
    Gun g = (Gun) context.getBean("gun");
    g.setConfigurator(gc);
    g.initialize();
    g.trigger();
    g.destroy();
}

From source file:Spring.Repaso02.Principal.java

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("Spring/Repaso02/applicationContext.xml");
    ClienteDAO clientedao = (ClienteDAO) context.getBean("ClienteDao");
    ProductoDAO productodao = (ProductoDAO) context.getBean("ProductoDao");
    PedidoDAO pedidodao = (PedidoDAO) context.getBean("PedidoDao");

    demoCliente(clientedao);//  www .  j  a v a 2s.  c om
    demoProducto(productodao);
    demoPedido(pedidodao, productodao);

}

From source file:com.zousu.mongopresser.Presser.java

public static void main(String[] args) {
    // Create the application context
    ApplicationContext ctx = new ClassPathXmlApplicationContext("root-context.xml");
    // Obtain a reference to our DAO
    Presser presser = (Presser) ctx.getBean("presser");
    presser.press();//w ww  . ja va  2 s  .c  o  m
}

From source file:net.unicon.mycourses.services.util.SakaiServiceDriver.java

public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext("sakaiServiceDriverApplicationContext.xml");
    SakaiServiceDriver driver = (SakaiServiceDriver) context
            .getBean("net.unicon.mycourses.services.util.SakaiServiceDriver");
    driver.exec();/* w  w w  .j a va 2 s. c  om*/

}

From source file:com.sundy.motan.client.AnnotationRpcClientDemo.java

public static void main(String[] args) throws InterruptedException {

    ApplicationContext ctx = new ClassPathXmlApplicationContext(
            new String[] { "classpath:motan_demo_client_annotation.xml" });

    DemoRpcHandler handler = (DemoRpcHandler) ctx.getBean("demoRpcHandler");
    handler.test();//from  w w w. j  a va2 s .c  o m

    System.out.println("motan demo is finish.");
    System.exit(0);
}

From source file:org.motechproject.mobile.omp.manager.intellivr.ConvertSerializedIVRSessions.java

/**
 * @param args/*from   w  ww  .ja va  2 s.  c  om*/
 */
public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "META-INF/conv-ivr-session-config.xml" });

    ConvertSerializedIVRSessionsBean convBean = (ConvertSerializedIVRSessionsBean) context.getBean("convBean");

    convBean.convert();

}

From source file:com.edmunds.etm.client.impl.Daemon.java

public static void main(String[] args) {

    if (args.length == 0) {
        System.out.println("Error provide the name of the service as the first argument");
        return;//from www .  j  a  v a 2  s .  c  o  m
    }
    final String serviceName = args[0];

    final Properties properties = new Properties();
    properties.put("serviceName", serviceName);

    final ApplicationContext appCtx = SpringContextLoader.loadClassPathSpringContext(DAEMON_CONTEXT_PATH,
            properties);
    final DaemonConfig daemonConfig = (DaemonConfig) appCtx.getBean("daemonConfig");
    daemonConfig.setServiceName(serviceName);
    final Daemon daemon = (Daemon) appCtx.getBean("daemon");

    if (daemon != null) {
        daemon.run();
    }
}

From source file:com.nabla.project.application.tool.runner.ServiceRunner.java

/**
 * DOCUMENT ME!/*from www. java2s  . co  m*/
 * 
 * @param args DOCUMENT ME!
 * @throws Exception DOCUMENT ME!
 * @throws RuntimeException DOCUMENT ME!
 */
public static void main(String args[]) throws Exception {

    ObjectInputStream ois = new ObjectInputStream(System.in);
    Object methodArgs[] = (Object[]) ois.readObject();

    if (args.length < 3) {

        throw new RuntimeException(
                "Error : usage : java com.nabla.project.application.tool.runner.ServiceRunner configFileName beanName methodName");

    }

    String configFileName = args[0];
    String beanName = args[1];
    String methodName = args[2];
    ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { configFileName });
    Object service = context.getBean(beanName);
    Method serviceMethod = null;
    Method methods[] = service.getClass().getMethods();

    for (Method method : methods) {

        if (method.getName().equals(methodName)) {

            serviceMethod = method;

        }

    }

    if (serviceMethod == null) {

        throw new RuntimeException("Method " + methodName + " not found in class " + service.getClass());

    }

    serviceMethod.invoke(service, methodArgs);

}