0

I have a Spring 4 application which I deploy as a WAR. Now I have a requirement where I cannot deploy the WAR, and instead use a single class to invoke the functionalities of the Spring application.

package com.company.project.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.company.project.bo.service.CleanupServiceBo;
import com.company.project.bo.service.BackupServiceBo;
import com.company.project.bo.service.HousekeepingServiceBo;

@Component
public class MyStandaloneComponent {

    @Autowired
    private CleanupServiceBo cleanupServiceBo;

    @Autowired
    private BackupServiceBo backupServiceBo;

    @Autowired
    private HousekeepingServiceBo housekeepingServiceBo;

    public static void main(String[] args) {

        try {

             cleanupServiceBo.deleteOldAuditRecords();
             backupServiceBo.backup();
             housekeepingServiceBo.doHousekeeping();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Now each of the three Autowired services have more Autowired components and services in them. And each of the three Autowired services interact with Database using JPA. I have an applicationContext.xml file describing various beans and JPA database connectivity.

Is it possible to create the application context of the spring application and use it? In that case, which application context to use such as AnnotationConfigApplicationContext, ClassPathXmlApplicationContext, AnnotationConfigApplicationContext etc.?

What would I need to run the above standalone class with Autowired services, without deploying the WAR?

user5155835
  • 4,392
  • 4
  • 53
  • 97

1 Answers1

1

Try this

package com.company.project.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import com.company.project.bo.service.CleanupServiceBo;
import com.company.project.bo.service.BackupServiceBo;
import com.company.project.bo.service.HousekeepingServiceBo;

@Component
public class MyStandaloneComponent {

    @Autowired
    private CleanupServiceBo cleanupServiceBo;

    @Autowired
    private BackupServiceBo backupServiceBo;

    @Autowired
    private HousekeepingServiceBo housekeepingServiceBo;

    private void start() {

        try {

             cleanupServiceBo.deleteOldAuditRecords();
             backupServiceBo.backup();
             housekeepingServiceBo.doHousekeeping();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        MyStandaloneComponent component = context.getBean(MyStandaloneComponent.class);
        component.start();
    }
}
debugging
  • 61
  • 3
  • Where can I close the context after the three operations in start() ? Immediately after `component.start();` in main() ? – user5155835 Jul 30 '19 at 04:52
  • Normally, you don't need to close the context, because the JVM is shutdown. But you can use the `try-with-resourse` statement like [this](https://stackoverflow.com/questions/14184059/spring-applicationcontext-resource-leak-context-is-never-closed) – debugging Aug 02 '19 at 14:08