How to use Spring Framework with a Command Line JAVA Application

What is Spring

Spring is an open source framework created with the purpose of enabling developers to build and deliver applications faster. The Framework is available for both the JAVA and .NET platforms. Spring provides a set of core features that can be used for most applications as well as modules that provide additional features.

Sampling of Modules

  • Data Access
    The Data Access Module allows for streamline configuration of Relational Database Management Systems (RDBMS)using JDBC and ORM‘s such as Hibernate and ibatis
  • Security | Authentication
    Spring Security Module provides advanced authorization and authentication features to your project/application. Some Key Authentication features being:
    LDAP, JAAS, Basic Access Authentication, and Digest Access Authentication
  • Model View Controller
    The Model View Controller module provides a MVC framework for use in developing Web Applications.

Initializing Spring from within an Application

To initialize Spring from within an application means to instantiate the ApplicationContext by use of a configuration file. I will not go in to the details as to how to set up the configuration file but will assume that you have created such an xml file called applicationContext.xml.

First Within your application import ApplicationContext and FileSystemXMLApplicationContext classes which will be used to instantiate the Application Context.

import org.springframework.context.ApplicationContext;
import org.springframework.context.support
                     .FileSystemXmlApplicationContext;

Then we Instantiate the Application Context.

ApplicationContext appContext =  
         new FileSystemXmlApplicationContext( contextFileName );

Last we utilize the beans defined in our xml file.

MyObject obj= appContext.getBean( "ObjectName" );

Resources

// Java //

Comments & Questions

Add Your Comment