Search code examples
javaspringautowiredspring-shell

Autowire object instance in spring shell command?


I have a small server which was in need of a simple console to enter a commands. After a bit of searching i found spring shell, which does the job.

Some of my commands need access to the server instance object.

How can i (autowire?) my server object instance into my spring shell command so that i can access my server object instance in a spring shell command?

Bootstrap constructor seem to creating the application context and immediately finds the user shell commands.

I tried to use:

bootstrap.getApplicationContext().getBeanFactory().registerSingleton(class, myserverinstance)

But this can only be done after Bootstrap constructor is called and then it is too late.


Solution

  • I'm not sure I understand.

    First of all, is the Spring Shell process the same process as what is running your "server"?

    Regarding wiring, Spring Shell is no different than any other Spring application. I understand that you already have an instance of your "server" bean. The best thing to do in that case is to use a FactoryBean

    Hope that helps