Java – spring: how to get application context in webapp and standalone programs

I'm new to the spring framework We want to introduce it into the web application (3.1). At present, we use struts in the web layer, service appearance and business objects in the business layer, and JDBC Dao written by ourselves in the persistence layer (all of which are tightly coupled!)

I created several XML configuration, one for servlet configuration, only scan com mydomain. Web package The other is in the service layer appcontext service Scan com.xml mydomain. BS and Bo package, the other is used for the Dao layer appcontext persistence XML scan Dao bag

We have four eclipse projects with appropriate project dependencies: Web, business, common (including domain objects, DTOs, exceptions, etc.), dataaccess

I want to use annotations when possible, and have created an MVC controller, a new service with interface and a new Dao with interface, using JDBC template, all of which are very effective

Now my question is:

>We can't rewrite all the code at once. We'll discuss a larger code base here However, what should I do if I still need to get the newly created services from the services and business objects that do not know spring? They are not beans, nor are they created by spring How can I catch my service bean? > We have several independent applications for batch processing, periodic cleaning of file systems and database tables, etc They are triggered by cron (UNIX cron), so they have their own JVM Consider different XML configuration, how do I best use spring services here? > Does my settings make any sense?

Thank you for any comments

Solution

>It is common for spring to handle the life cycle of all beans, otherwise it may be a little tricky Objects that are not spring beans are expected to initialize somewhere Make the initializer a spring bean and make it aware of the application context

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
    }
}

>Setting up a separate spring application is very easy Just create a spring XML and connect your bean (by scanning / commenting or XML) It is not recommended to do this in the main method, but you can easily figure out how to set this in a stand-alone application Remember, your application itself should not really do a lot of lifecycle logic, but let spring do it

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
  }

}

>Your setup is very meaningful. Even if I can't visually show your whole range, your method is a good starting point for large modular spring applications

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>