On Java Development

All things related to Java development, from the perspective of a caveman.

Using baseproject’s ApplicationContextManager class

without comments

Introduction

This post focuses on the ApplicationContextManager class that comes with baseproject and shows how it is used to obtain the application’s properties from a bean defined to the Spring Context.

Methods

  • public static ApplicationGlobalProperties getApplicationPropertiesBean()
    This method returns the bean containing the application’s properties that are unique to a given runtimeEnvironment. The names given to each of the bean definitions are listed below.

    • localhostApplicationProperties
    • TESTApplicationProperties
    • PRODApplicationProperties
  • To return the proper bean, this method first determines the environment in which the application is running. It does this by reading the value for “runtimeEnvironment“, a variable established on the application server and which can only be one of 3 values:

    • localhost
    • TEST
    • PROD

    The value obtained (e.g. “localhost”, “TEST” or “PROD”) is then used as the prefix concatenated to “ApplicationProperties” which then references the correct bean to be returned.

  • public static ApplicationGlobalProperties getApplicationGlobalPropertiesBean()
    This method returns the bean containing the application’s properties of a global nature, meaning that they are not unique to a given runtimeEnvironment and will be the same across them. The name given to the bean is “applicationGlobalProperties“. There is only one bean used for this purpose.
  • public static Object getApplicationContextBean(String name)
    This method is used to retrieve any developer-defined bean that has been added to the applicationContext.xml file. This method returns the bean named by the passed parameter. It returns an Object, so the developer must cast it to the proper class.
  • public static DataSource getDataSource()
    Get the data source for the runtimeEnvironment. This method is used to acquire a data source object to be passed to a Spring stored procedure. To see this in action using SETLIBLEXEC, see this post.

 
Spring configuration files

The following Spring configuration files are used by baseproject. They are located under src\main\resources\config\ and contain the definitions for the beans mentioned above.

  • applicationContext.xml
    Contains beans for the project. The beans applicationGlobalProperties and [runtimeEnvironment]ApplicationProperties are defined by the entries in this file as well as any additional beans required by the application.
  • jpaDataSourceContext.xml
    Contains the JPA oriented DataSource definitions needed to setup a JPA based persistence environment that includes support for the Entity Manager and commitment control. The entries in this file are static which lend itself to being placed into its own configuration file.

 
UML Class Diagram

Shown below is a simple UML Class Diagram that shows the relationship of the ApplicationContextManager to other classes. (Click to enlarge.)

ApplicationContextManager

Simply put, ApplicationContextManager is the interface to the Spring context where beans defined to Spring are configured. As mentioned the configuration also includes datasource definitions. Data sources are obtained by all Service classes which is then passed to each DAO being used within the service’s transaction-commit boundary. The BaseService class that is inherited by each service is responsible for obtaining a datasource from Spring.

Written by admin

April 1st, 2014 at 10:13 am

Leave a Reply

You must be logged in to post a comment.