Hello. I have such a trivial question. In the spring, it is possible to raise the context by simply passing the xml and bins to the annotation in a similar way:

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/my-config.xml"}). 

This is used to run JUnit tests. The question is - can this be done for a normal application, not JUnit , that is, without writing java-кода with creating a class that stores ClassPathXmlApplicationContext ? That is, create a context immediately from xml , and then drive @Required , @Autowired .

    1 answer 1

    Do you want to be able to set, say, a context, a bin, and a method from which to start the application and everything — we launch it (presumably, it will be necessary to launch something third-party)? What you are looking for is called a server in its most simplified form. For example, there was such an idea at a spring forum, perhaps they have achieved something. But it seems to me that there is hardly any known project like this - if only because there is not much to write there, and the two-line main() is copied very quickly, and if something starts to develop, it turns into something more.

    UPD. The simplest context loader.

     public class ContextLoader { public static void main(String[] args) { String configLocation = args[0]; new ClassPathXmlApplicationContext(configLocation); } } 

    Run java -cp [указываем все нужные пути] ContextLoader classpath:ctx.xml . From IDE is even easier.

    • No. In my example, @ContextConfiguration is in the package that is responsible for supporting JUnit testing. Roughly speaking, I wondered if there was the same @ContextConfiguration but not for tests. In the documentation for 3, I found only raising context through ClassPathXmlApplicationContext. I ask that if I need to raise 2 unfortunate bins, then why should I once again create java code with context initialization - Vladimir
    • Well, yes, I understood that. Those. "Is there such a class (in the spring itself, for example) that contains a main() method that scans the specified packages for a specific annotation and thus initializes the context." If it is not important to use annotations, then you can write it yourself (see UPD). - yozh
    • And he did. Thank you - Vladimir