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

<T> T getBean(String name, Class<T> requiredType) throws BeansException;

Source Link

Document

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

Usage

From source file:nl.han.dare2date.service.AbonneeNotification.java

public static void main(String args[]) {
    ApplicationContext context = new ClassPathXmlApplicationContext("abonneeNotificationContext.xml");

    AbonneeNotification abonneeNotification = context.getBean("abonneeNotification", AbonneeNotification.class);
}

From source file:com.stoxa.SpringXML.TestClass.TestClass.java

public static void main(String[] args) throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("config2.xml");
    FactoryBean factory = context.getBean("contactFactory", ContactBeanFactory.class);

    Contact c1 = (Contact) factory.getObject();
    Contact c2 = (Contact) factory.getObject();
    Contact c3 = (Contact) factory.getObject();
    Contact c4 = (Contact) factory.getObject();
    Contact c5 = (Contact) factory.getObject();

    ContactService contactService = context.getBean("ContactService", ContactService.class);

    System.out.println("ADD CONTACT ==============");
    contactService.addContact(c1);//  w w w .jav a  2 s  .  c  om
    contactService.addContact(c2);
    contactService.addContact(c3);
    contactService.addContact(c4);
    contactService.addContact(c5);
    List<Contact> result1 = contactService.getAllContacts();
    for (Contact c : result1) {
        System.out.println(c);
    }
    System.out.println("******************************************");

    System.out.println("UPDATE CONTACT ==============");
    Contact change = new Contact("??", "", "+380955019248", "sokolov@yandex.ru");
    contactService.updateContact(change);
    List<Contact> result2 = contactService.getAllContacts();
    for (Contact c : result2) {
        System.out.println(c);
    }
    System.out.println("******************************************");

    System.out.println("DELETE CONTACT ==============");
    contactService.deleteContact(c3);
    List<Contact> result3 = contactService.getAllContacts();
    for (Contact c : result3) {
        System.out.println(c);
    }
    System.out.println("******************************************");

    System.out.println("GET CONTACT ==============");
    Contact contact = contactService.getContact("+380674560901");
    System.out.println(contact);

    System.out.println("******************************************");

    System.out.println("CLEAR ALL CONTACTS ==============");
    contactService.clearAll();
    List<Contact> result4 = contactService.getAllContacts();
    for (Contact c : result4) {
        System.out.println(c);
    }
}

From source file:org.jasig.portlet.data.Importer.java

public static void main(String[] args) throws Exception {
    String dir = args[0];/* ww  w .  j  a  va 2 s .  c o  m*/
    String importExportContext = args[1];
    String sessionFactoryBeanName = args[2];
    String modelClassName = args[3];
    String serviceBeanName = args[4];
    String serviceBeanMethodName = args[5];

    ApplicationContext context = PortletApplicationContextLocator.getApplicationContext(importExportContext);
    SessionFactory sessionFactory = context.getBean(sessionFactoryBeanName, SessionFactory.class);
    Class<?> modelClass = Class.forName(modelClassName);
    Object service = context.getBean(serviceBeanName);

    JAXBContext jc = JAXBContext.newInstance(modelClass);

    File folder = new File(dir);
    File[] files = folder.listFiles(new ImportFileFilter());

    for (File f : files) {
        StreamSource xml = new StreamSource(f.getAbsoluteFile());
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement je1 = unmarshaller.unmarshal(xml, modelClass);
        Object object = je1.getValue();
        Session session = sessionFactory.getCurrentSession();
        Transaction transaction = session.beginTransaction();

        Method method = service.getClass().getMethod(serviceBeanMethodName, modelClass);
        method.invoke(service, object);
        transaction.commit();
    }
}

From source file:org.jasig.portlet.data.Exporter.java

public static void main(String[] args) throws Exception {
    String dir = args[0];//www . ja v a2  s  .co m
    String importExportContext = args[1];
    String sessionFactoryBeanName = args[2];
    String modelClassName = args[3];
    String serviceBeanName = args[4];
    String serviceBeanMethodName = args[5];

    ApplicationContext context = PortletApplicationContextLocator.getApplicationContext(importExportContext);
    SessionFactory sessionFactory = context.getBean(sessionFactoryBeanName, SessionFactory.class);
    Class<?> modelClass = Class.forName(modelClassName);

    Object service = context.getBean(serviceBeanName);
    Session session = sessionFactory.getCurrentSession();
    Transaction transaction = session.beginTransaction();

    JAXBContext jc = JAXBContext.newInstance(modelClass);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Method method = service.getClass().getMethod(serviceBeanMethodName);
    List<?> objects = (List<?>) method.invoke(service, null);

    for (Object o : objects) {
        session.lock(o, LockMode.NONE);
        JAXBElement je2 = new JAXBElement(new QName(modelClass.getSimpleName().toLowerCase()), modelClass, o);
        String output = dir + File.separator + UUID.randomUUID().toString() + ".xml";
        try {
            marshaller.marshal(je2, new FileOutputStream(output));
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    transaction.commit();
}

From source file:com.home.ln_spring.ch5.javaconfig.JavaConfigSimpleExample.java

public static void main(String[] args) {
    ApplicationContext context =
            //new ClassPathXmlApplicationContext("classpath:app-context.xml");
            new AnnotationConfigApplicationContext(AppConfig.class);

    MessageRenderer renderer = context.getBean("messageRenderer", MessageRenderer.class);

    renderer.render();//w w  w. ja  v  a 2 s .co m
}

From source file:org.duracloud.account.db.util.DbUtilDriver.java

public static void main(String[] args) throws IOException {
    if (args.length != 2) {
        usage("Two arguments are required, you supplied: " + args.length);
        System.exit(1);/* www .  j  a v a2s . c om*/
    }

    DbUtil.COMMAND command = null;
    String commandArg = args[0];
    if (commandArg.equalsIgnoreCase(DbUtil.COMMAND.PUT.name())) {
        command = DbUtil.COMMAND.PUT;
    } else {
        usage("The first argument must PUT. " + "The previously supported command GET and CLEAR "
                + "have been removed since the move to MC 2.0.0 " + "You supplied: " + commandArg);
        System.exit(1);
    }

    File workDir = new File(args[1]);
    if (!workDir.exists()) {
        usage("The work directory must exist: " + workDir.getPath());
        System.exit(1);
    } else if (!workDir.isDirectory()) {
        usage("The work directory must be a directory: " + workDir.getPath());
        System.exit(1);
    }

    ApplicationContext context = new ClassPathXmlApplicationContext("jpa-config.xml");
    DuracloudRepoMgr repoMgr = context.getBean("repoMgr", DuracloudRepoMgr.class);

    DbUtil dbUtil = new DbUtil(repoMgr, workDir);
    dbUtil.runCommand(command);
}

From source file:uk.co.techsols.mentis.worker.Main.java

public static void main(String args[]) {

    if (args.length != 3) {
        System.out.println(//from w  w  w.  j a  v a  2s.c  o m
                "Usage: type url cores\n\ttype:\n\t\tr - renderer\n\t\tt - transformer\n\turl: the url to Eiocha.\n\tcores: the number of cores to use\n\n\te.g. node.jar t http://localhost:8080/server 4\n\n");
        System.exit(1);
        return;
    }

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/applicationContext.xml");
    Worker worker;
    switch (args[0]) {
    case "r":
        worker = applicationContext.getBean("renderWorker", Worker.class);
        break;
    case "t":
        worker = applicationContext.getBean("transformWorker", Worker.class);
        break;
    default:
        System.out.println("Error: unreconised worker type.");
        System.exit(1);
        return;
    }

    worker.setup(URI.create(args[1]), Integer.parseInt(args[2]));
}

From source file:org.bakujug.springrental.Main.java

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

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml",
            "Spring-DataSource.xml");

    CustomerRepository customerRepository = applicationContext.getBean("customerRepository",
            CustomerRepository.class);

    Customer customer = new Customer();
    customer.setLastname("Ilkin");
    customer.setFirstname("Abdullayev");
    customer.setAge(23);/*w  ww.  j av a 2 s .  co  m*/
    customerRepository.save(customer);

    //   Customer customer = customerRepository.getCustomerByName("Sunal");
    //  System.out.println(customer);

    // RentalService rentalService = (RentalService) applicationContext.getBean("rentalService");

    //        Rental rental = rentalService.rentACar("Ilkin", new Car("Fiesta"), getRentalBegin(), getRentalEnd());
    //
    //        System.out.println("Rental status: "+ rental.getCustomer());
}

From source file:com.hikuns.springdata.gemfire.Main.java

/**
 * @param args/*w ww. j  ava2  s.  c o  m*/
 */
public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/gemfire/cache-config.xml");

    @SuppressWarnings("unchecked")
    Region<Object, Object> region = context.getBean("myRegion", Region.class);

    log.debug("populating region ...");
    region.put(1, "apple");
    region.put(2, "banana");
    region.put(3, "pear");

    log.debug("retreiving region values...");

    for (Object obj : region.values()) {
        log.debug(obj);
    }

    log.debug("retreiving region keys...");
    for (Object obj : region.keySet()) {
        log.debug(obj + ":" + region.get(obj));
    }
}

From source file:stsspringframeworkdatagemfire.Main.java

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

    ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring/gemfire/cache-config.xml");

    @SuppressWarnings("unchecked")
    Region<Object, Object> region = context.getBean("regionName", Region.class);

    log.debug("populating region ...");
    region.put(1, "apple");
    region.put(2, "banana");
    region.put(3, "pear");

    log.debug("retreiving region values...");

    for (Object obj : region.values()) {
        log.debug(obj);
    }

    log.debug("retreiving region keys...");
    for (Object obj : region.keySet()) {
        log.debug(obj + ":" + region.get(obj));
    }
}