Answer the question
In order to leave comments, you need to log in
How to connect Spring not to a web project?
Wrote a small test project to check the correct configuration of Spring ( link to GitHub ).
But he can't seem to get the @Autowired instruction to work correctly.
The following error appears in the console:
Exception in thread "main" java.lang.NullPointerException
at com.dashkevich.systemcontrol.systemmonitor.main.main(main.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application. AppMain.main(AppMain.java:134)
Answer the question
In order to leave comments, you need to log in
You need to do approximately what spring does in a web application, namely, at the beginning, create the necessary context manually and tell it where the configuration is, and then get the necessary objects from this context.
For example:
org.springframework.context.annotation.AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext(Config1.class, Config2.class);
MyBean myBean = appContext.getBean(MyBean.class);
There are other types of contexts, for example, which load the configuration from xml. Details in the documentation.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question