com.pdemartino.test.springctxs.App.java Source code

Java tutorial

Introduction

Here is the source code for com.pdemartino.test.springctxs.App.java

Source

/*
 * Copyright (C) 2014 pdemartino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.pdemartino.test.springctxs;

import com.pdemartino.test.springctxs.beans.Dummy;
import com.pdemartino.test.springctxs.beans.DummyContainer;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author pdemartino
 */
public class App {

    static ClassPathXmlApplicationContext springContext;
    static final Map<String, Context> contexts = new HashMap<String, Context>() {
        {
            put("a", new Context("a", "XML Context", "spring-context"));
            put("b", new Context("a", "JavaConf Context", "spring-javaconf"));
        }
    };

    public static void main(String[] args) {
        System.out.println("\nSPRING TEST START\n");

        springContext = new ClassPathXmlApplicationContext("spring/" + askForContextFile() + ".xml");

        // run tests
        System.out.println("Running " + springContext.getBean("context"));
        testScopePrototype(2);
        testDerivedDummy();
        testAutoWiringAnnotation();

        // close context to call destroy methods
        springContext.close();

        System.out.println("\nSPRING TEST END\n");
    }

    private static void testScopePrototype(int n) {
        System.out.println("\nTest scope=prototype");
        for (int i = 0; i < n; i++) {
            System.out.println("Bean magic number: " + ((Dummy) springContext.getBean("dummy")).getMagicNumber());
        }
    }

    private static void testDerivedDummy() {
        System.out.println("\nTest Derived");
        System.out.println("SingletonBean magic number: "
                + ((Dummy) springContext.getBean("singletonDummy")).getMagicNumber());
        System.out.println(
                "SpellBean magic number: " + ((Dummy) springContext.getBean("derivedDummy")).getMagicNumber());
    }

    private static void testAutoWiringAnnotation() {
        System.out.println("\nTest @Autowired");
        DummyContainer dummyContainer = (DummyContainer) springContext.getBean("dummyContainer");
        System.out.println("Autowired content: " + dummyContainer.getDummy() != null);

    }

    // Support
    private static String askForContextFile() {
        String springType;

        // define prompt string
        String promptString = "";
        for (Map.Entry<String, Context> ctx : contexts.entrySet()) {
            promptString += "\n" + ctx.getKey() + ": " + ctx.getValue().description;
        }

        // User Input
        Scanner scanIn = new Scanner(System.in);
        do {
            System.out.println("Choose configuration type:" + promptString);
            springType = scanIn.nextLine().trim();
        } while (!contexts.containsKey(springType));

        return contexts.get(springType).contextFile;
    }

    static private class Context {

        public String key;
        public String description;
        public String contextFile;

        public Context(String key, String description, String contextFile) {
            this.key = key;
            this.description = description;
            this.contextFile = contextFile;
        }
    }

}