Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: how to get hold of Application context in Webapp and Standalone program

I'm new to the Spring Framework. We want to introduce it (3.1) in a web application, currently using struts in the web layer, service facades and business objects in the business layer and self-written JDBC DAOs in the persistence layer (all of it closely coupled!)

I created several .xml configurations, one for the servlet config, scanning the com.mydomain.web package only. Another one on the service layer appContext-service.xml which scans com.mydomain.bs and .bo packages and one for the DAO layer appContext-persistence.xml scanning the .dao package.

We have four Eclipse projects with appropriate project dependencies: Web, Business, Common (contains domain objects, DTOs, Exceptions, etc), DataAccess.

I want to use annotations where possible and already created a MVC controller, a new service with interface and a new dao with interface, using the JDBC template, which all works great.

Now my questions are:

  1. We can't re-write all the code at once, we're talking about a larger code base here. But what do I do, when the newly created service is also needed from services and business objects that are not (yet) Spring aware? They're not beans or not being created by Spring. How would I get hold of my service bean?

  2. We have several standalone applications for batch processing, cleaning up the file system and database tables periodically, etc. They're triggered by cron (UNIX cron) and therefore have their own JVM. How would I best use Spring services here, given the different .xml configurations?

  3. Does my setup make any sense at all?

Thanks for any insight.

like image 937
marc82ch Avatar asked Jul 27 '12 06:07

marc82ch


People also ask

How do I get Spring application context?

To get a reference to the ApplicationContext in a Spring application, it can easily be achieved by implementing the ApplicationContextAware interface. Spring will automatically detect this interface and inject a reference to the ApplicationContext: view rawMyBeanImpl. java hosted by GitHub.

Which context is used to load standalone application in Spring?

FileSystemXmlApplicationContext is used to load XML-based Spring Configuration files from the file system or from URL. We can get the application context using Java code. It is useful for standalone environments and test harnesses.

How can you access the application context?

To access the application context, we can autowire the ApplicationContext interface or implement the ApplicationContextAware .

Can we have two application context in Spring?

We can have multiple application contexts that share a parent-child relationship. A context hierarchy allows multiple child contexts to share beans which reside in the parent context. Each child context can override configuration inherited from the parent context.


1 Answers

  1. It's very common that one let spring handle the lifecycle of all the beans, otherwise it might get a bit tricky. The objects that are not spring beans are hopefully initialized somewhere. Make that initializer a spring bean and make it application context aware

     public class SpringContextHolder implements ApplicationContextAware {
    
       private static ApplicationContext applicationContext = null;
    
        public static ApplicationContext getApplicationContext() {
            return applicationContext;
        }
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
             this.applicationContext = applicationContext;
        }
        public void init(){
    
            ServiceBean1 srv1 = (ServiceBean1)applicationContext.getBean("serviceBean1");
    
            myNonSpringObject.setService1(srv1); // Or something
        }
    }
    
  2. Setting up a standalone spring app is very easy. Just create a Spring XML and wire your beans (either via scanning/annotations or XML). It is not really recommended to do this in the main method, but you could easily figure out how to get this setup in your standalone application. Keep in mind that your application itself should not really do much lifecycle logic but let Spring do that.

    public class StandaloneSpringApp{
      public static void main(String[] args){
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    
        SomeBeanType bean = (SomeBeanType)ctx.getBean("SomeBeanName");
        bean.doProcessing(); // or whatever
      }
    
    }
    
  3. Your setup makes perfect sense, even though I cannot visualize your entire scope, your approach is a good starting point for a large modularized spring application.

like image 58
Petter Nordlander Avatar answered Sep 29 '22 01:09

Petter Nordlander