StartUp Class – Spring JavaConfig

package moviefindertest.javaconfig;
 
import moviefindertest.javaconfig.ApplicationConfig;
import moviefindertest.javaconfig.Movie;
import moviefindertest.javaconfig.MovieLister;
 
import org.springframework.config.java.context.JavaConfigApplicationContext;
 
 
public class StartUp {
 
      public static void main(String[] args) {
            JavaConfigApplicationContext context = new JavaConfigApplicationContext(ApplicationConfig.class);
            
            Movie andreiRublev = (Movie) context.getBean("AndreiRublevMovie");
            System.out.println("TEST 1: Configuration and retrieval of an object with (only) string dependencies");
            System.out.println("Should show one Tarkovsky movie:");
            System.out.println(andreiRublev.toString());
            
            // Finder injected into Lister by constructor injection.  Only one Lister
            // class here, configured to use two different Finders (SimpleMovieFinder
            // and ColonDelimitedMovie Finder.  Both of the Finders
            // implement the MovieFinder interface of course, and are injected
            // using that.
            System.out.println("================================================================================");
            System.out.println("TEST 2: Injection of an object into an instance of a class.");
            System.out.println("Should show list of James Cameron movies:");
 
            MovieLister simpleLister = (MovieLister) context.getBean("simpleMovieLister");
            Movie[] jamesCameronMovies = simpleLister.moviesDirectedBy("James Cameron");
            for(Movie movie : jamesCameronMovies){
                  System.out.println(movie.toString());
            }
            
            // Finder injected by setter injection.  See comments above.
            System.out.println("================================================================================");
            System.out.println("TEST 3: Injection of a different object into an instance of the same class as used in test 2.");
            System.out.println("Should show list of Martin Scorsese movies:");
            MovieLister colonDelimitedLister = (MovieLister) context.getBean("colonDelimitedMovieLister");
            Movie[] martinScorseseMovies = colonDelimitedLister.moviesDirectedBy("Martin Scorsese");
            for(Movie movie : martinScorseseMovies){
                  System.out.println(movie.toString());
            }
            
            
            // Test scope by requesting a second lister of each type: 
            // the singleton scope SimpleMovieLister should return the same object
            // as above, the prototype scope ColonDelimitedMovieLister should
            // return a new object (which will have a different ID).
            System.out.println("================================================================================");
            System.out.println("TEST 4: Creation of an object with singleton scope (same object retrieved from the container every time requested).");
            System.out.println("Versions of requested objects should be the same:");
 
            System.out.println("First requested simple lister (singleton) version = " + simpleLister.getVersionID());
            MovieLister simpleLister2 = (MovieLister) context.getBean("simpleMovieLister");
            System.out.println("Second requested simple lister version = " + simpleLister2.getVersionID());
 
            System.out.println("================================================================================");
            System.out.println("TEST 5: Creation of an object with prototype or transient scope (different (new) object retrieved from the container every time requested).");
            System.out.println("Versions of requested objects should be different:");
 
            System.out.println("First requested colon delimited lister (prototype) version = " + colonDelimitedLister.getVersionID());
            MovieLister colonDelimitedLister2 = (MovieLister) context.getBean("colonDelimitedMovieLister");
            System.out.println("Second requested colon delimited lister version = " + colonDelimitedLister2.getVersionID());
            
            System.out.println("================================================================================");
            System.out.println("TESTS 6 AND 7: Use of a factory class and injection of the container to create a dependent object.");
            System.out.println("Should show Tarkovsky movie:");
            // An example with more complex construction via a factory method
            MovieLister complexLister = (MovieLister) context.getBean("complexMovieLister");
            Movie[] tarkovskyMovies = complexLister.moviesDirectedBy("Andrei Tarkovsky");
            for(Movie movie : tarkovskyMovies){
                  System.out.println(movie.toString());
            }
      }
 
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s