Spring Application Context

For Command Line Apps

Some may find this so fundamental it is useless.  But since I make a practice of getting and using Spring Beans, not starting a Spring application from scratch, I have to write this stuff down for future – easy – access.

I need a reminder of grabbing my Spring Application Context outside of a web based application.  Normally I would just put a reference into my web.xml file:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

But since I am running essentially a command-line process, I need to get a handle within the application itself.

1) Create a Singleton that will provide the context reference:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fishizle.springsample;

import org.springframework.beans.BeansException;
import org.springframework.context.*;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationContextUtils implements ApplicationContextAware {

  private static ApplicationContext ctx;

  @Override
  public void setApplicationContext(ApplicationContext appContext)
      throws BeansException {
    ctx = appContext;

  }

  public static ApplicationContext getApplicationContext() {
      if (ctx == null) 
          ctx= new ClassPathXmlApplicationContext("SpringSample_annotations.xml");
    return ctx;
  }
}
2) Next get a handle to the context
    private ApplicationContext ctx = ApplicationContextUtils.getApplicationContext();

In context of the functioning class:

public class Startup {

    private IncludeClass include1;
    private ApplicationContext ctx = ApplicationContextUtils.getApplicationContext();

    @Autowired
    private Band band;

    @Autowired
    private Thinker thinker;

    public static void main(String[] args) {
        System.out.println("Application Started Successfully");
        Startup startup = new Startup();
        startup.goStartup();
    }

    public void goStartup() {
        include1 = (IncludeClass) ctx.getBean("include1");
        band = (Band) ctx.getBean("band");
    }
}
3) Lastly, my app context XML::
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
       http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/aop      http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
    >


    <bean id="contestantDelegate" class="com.fishizle.springsample.awards.GraciousContestant" ></bean>


    <!--
       One Advantage of Spring aop: over @AspectJ is, Spring doesn't need Source files to AOP.  @AspectJ requires it.
    -->

    <aop:config>
        <aop:aspect ref="audience">
            <aop:pointcut id="performance" expression="execution(* com.fishizle.springsample.music.Performer.perform(..))" />

            <aop:before pointcut-ref="performance" method="takeSeats" />
            <aop:before pointcut-ref="performance" method="turnOffCellPhones" />
            <aop:after-returning pointcut-ref="performance" method="applaud" />
            <aop:after-throwing pointcut-ref="performance" method="demandRefund" />

            <aop:pointcut id="performance2" expression="execution (* com.fishizle.springsample.music.Performer.perform(..))" />

            <aop:around pointcut-ref="performance2" method="watchPerformance" />

        </aop:aspect>

        <aop:aspect ref="magician">
            <aop:declare-parents
                    types-matching="com.fishizle.springsample.music.Performer+"
                    implement-interface="com.fishizle.springsample.awards.Contestant"
                    delegate-ref="contestantDelegate" />
            <!-- either default-impl   ... impl only for the Aspect
                 , or delegate-ref  ... Spring Bean reusable elsewhere  -->
            <!--default-impl="com.fishizle.springsample.awards.GraciousContestant" />-->


            <aop:pointcut
                    id="thinking"
                    expression="execution
                (* com.fishizle.springsample.acts.Thinker.thinkOfSomething(String)) and args(thoughts)" />

            <aop:before pointcut-ref="thinking" method="interceptThoughts" arg-names="thoughts" />

        </aop:aspect>
    </aop:config>


    <context:annotation-config />
    <!--<context:component-scan />-->
 
    <bean id="include1" class="com.fishizle.springsample.IncludeClass"
          p:includeDos-ref="include2"
          p:wireByName-ref="wireByName"
          p:includeOneA-ref="include1a" />
        <!-- can specify list, set, map, properties as a p:property __ see page 48 Spring in Action -->

    <bean id="include1a" class="com.fishizle.springsample.Include1a" autowire="byName" />

    <!-- include2 -->
    <bean id="include2" class="com.fishizle.springsample.Include2" />

    <bean id="include3" class="com.fishizle.springsample.Include3" />

    <bean id="wireByName"
          class="com.fishizle.springsample.WireByName"
          autowire="byName"
          p:inProp="My inProp" >
    </bean>

    <bean id="wireByType" class="com.fishizle.springsample.WireByType" autowire="byType" />

    <bean id="audience" class="com.fishizle.springsample.music.aspects.Audience" />

    <!-- The band -->
    <bean id="band" class="com.fishizle.springsample.music.Band" />

    <bean id="bass" class="com.fishizle.springsample.music.instruments.Bass" />
    <bean id="drums" class="com.fishizle.springsample.music.instruments.Drums" />
    <bean id="guitar" class="com.fishizle.springsample.music.instruments.Guitar" />
    <bean id="keyboard" class="com.fishizle.springsample.music.instruments.Keyboard" />
    <bean id="singing" class="com.fishizle.springsample.music.instruments.Singing" />


    <bean id="alex" class="com.fishizle.springsample.music.performers.Alex" />
    <bean id="david" class="com.fishizle.springsample.music.performers.David" />
    <bean id="eddie" class="com.fishizle.springsample.music.performers.Eddie" />
    <bean id="michael" class="com.fishizle.springsample.music.performers.Michael" />


    <bean id="magician" class="com.fishizle.springsample.acts.mindReader.Magician" />
    <bean id="thinker" class="com.fishizle.springsample.acts.volunteers.VolunteerThinker" />
</beans>

For the forgetful or unpracticed/un-exercised, I hope you find this quick reference useful.