One common request I see quite frequently on the internet is the ability to have spring autowire a main class:
There is no shortage of questions / solutions to this problem which illustrates a need for a standardized approach.
What I am proposing is combination of features between the following existing classes:
1. org.springframework.test.context.junit4.SpringJUnit4ClassRunner - provides functionality of the Spring TestContext Framework to standard JUnit 4.5+ tests
2. org.springframework.test.context.ContextConfiguration - class-level metadata that is used to determine how to load and configure an ApplicationContext for test classes
3. org.springframework.web.context.support.SpringBeanAutowiringSupport a convenient base class for self-autowiring classes that gets constructed within a Spring-based web application.
Thus provinding first class support for console based application context management and DI.
1. Autowiring of main class dependencies
2. Auto-destruction of application context
3. Ability in the future to handle command line arguments as beans
2. Consistency with other APIs
3. Minimal configuration
4. Intent revealing
My visions is something like the following:
The way this would work is as follows:
1. JVM calls main
2. main delegates to super class method MainSupport.execute provided by spring (analog of SpringBeanAutowiringSupport)
3. MainSupport.execute reads the @ContextConfiguration annotation and looks for a @Main annotation (analog of @Test)
4. MainSupport.execute creates the application context
5. MainSupport.execute create an application Main class instance
6. MainSupport.execute injects dependencies
7. MainSupport.execute wraps instance in a proxy that will advise the method annotated with @Main. This proxy will first call the annotated method and then destroy the application context