i have spring boot, hibernate application , android application client side. using java.net.socket
api socket connection. before creating server socket new server(12346);
, enough. need access database socket class e.g. @autowired usersdao
field, of course null
because socket
class not visible spring framework. how make dependency injection on socket
class using port constructor argument , make userdao
non-null?
you can access spring application context
static method , use static method load repository bean in server
class instead of autowiring it.
you need create following classes (found here):
applicationcontextprovider
@component public class applicationcontextprovider implements applicationcontextaware { private static applicationcontext context; public applicationcontext getapplicationcontext() { return context; } @override public void setapplicationcontext(applicationcontext ctx) { context = ctx; } }
springconfiguration
@configuration public class springconfiguration { @bean public static applicationcontextprovider contextprovider() { return new applicationcontextprovider(); } }
and non-spring managed server
class:
public class server { //your code public void dousersdaostuff() { usersdao usersdao = (usersdao) springconfiguration.contextprovider().getapplicationcontext().getbean("usersdao"); // own stuff usersdao here... } }
Comments
Post a Comment