Using an autowired singleton bean in a non spring managed java class

Lucas :

Alright, this might seem pretty stupid to all the veterans out there, but bear with me here, as I'm only finding my way around Spring & Spring Boot.

I've got a Controller class here,

@RestController
public class Controller {

    private static final Logger logger = LogManager.getLogger(Controller.class);

    private static Controller controller = null;

    @Autowired
    private ApplicationParameters applicationParameters;

    public static Controller getInstance() {
        if (controller == null) {
            synchronized (Controller.class) {
                if (controller == null) {
                    controller = new Controller();
                }
            }
        }
        return controller;
    }

    public Controller() {}

    public ApplicationParameters getApplicationParameters() {
        return applicationParameters;
    }

    @RequestMapping("/")
    public void init() {
        try {
            for (Entry<String, String> prop : applicationParameters.getProperties().entrySet())
                logger.info("Loaded System Property: " + prop.getKey() + " -> " + prop.getValue());

                Utils.concatenate("key1", "key2");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
} 

which autowires the ApplicationParameters bean with properties from a Property file.

Utils Class

public class Utils {

protected static final Logger logger = LogManager.getLogger(Utils.class);

    //Need to get the value of the property keys propKey1 & propKey2 and concat them.
    public static String concatenate(String propKey1, String propKey2) throws Exception {
        if(StringUtils.isNoneEmpty(propKey2) && StringUtils.isNoneEmpty(propKey1)) {
            return Controller.getInstance().getApplicationParameters().getProperties().get(propKey1) + Controller.getInstance().getApplicationParameters().getProperties().get(propKey2)
        } else {
            logger.error("System Property is undefined." );
            return null;
        }
    }

So, I'd like use this autowired ApplicationParameters bean as a singleton instance throughout the lifecycle of my project.

For instance, I'd like to use it in the Utils class. Clearly Utils class is not spring managed, its just a regular old java class.

So I'd like to know how to use fully initialized applicationParameters in my Utils class.

This is what I've tried so far:

  1. Autowiring the ApplicationParameters again in the Utils class, like this,

    public class Utils {
    
    @Autowired
    private ApplicationParameters applicationParameters;
    
    protected static final Logger logger = LogManager.getLogger(Utils.class);
    

But applicationParameters will be null here as, I'm presuming, this is because, Utils is not a spring managed bean.

  1. Make Controller class singleton. (Not sure how to go about doing this as init() needs to get invoked when web server starts, then where to call getInstance()?)

Hence, would someone be so kind as to assist a novice here.

P.S. The Utils class is shown only as a sample to bring home the fact that, a spring managed autowired bean has to be used in a regular java class.

Dirk :

You could make the spring context accessible from outside with a helper class like this one:

public class SpringContextUtil implements ApplicationContextAware {
   static ApplicationContext applicationContext;     

   public void setApplicationContext(ApplicationContext context) throws BeansException {
     applicationContext = context;
   }

   public static ApplicationContext getApplicationContext() {
     return applicationContext;
   }
}

Then, you could do something like this: SpringContextUtil.getApplicationContext.getBean("applicationParameters")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=356548&siteId=1
Recommended