1

I would like to be able to use a bean via auto-wiring and without having to directly use an ApplicationContext. Below is a dummy example of what I would like to be able to do.

Configuration Class

@Configuration
public class CoffeeConfig
{
     @Bean
     public CoffeeMachine provideCoffeeMachine()
     {
         return new CoffeeMachine(provideCoffeeBean());
     }

     @Bean
     public CoffeeBean provideCoffeeBean()
     {
          return new CoffeeBean(Type.BEST);
     }
}

Coffee Shop Class

@Component
public class CoffeeShop
{    
    @Autowired
    private CoffeeMachine cMachine;

    public void pourCoffee()
    {
         System.out.print("Pouring cup of coffee: " + cMachine.pour(Amount.8OZ));
    }
}

In order to solve this, I have been reading through spring documentation and spring tutorials. The problem is, I haven't seen anyone attempt to illustrate how to do something as simple as this, and when they do, they end up resorting to using an application context. That being said, I know that if I am running unit tests with Spring, I can do the following:

Test Class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CoffeeConfig.class, loader=AnnotationConfigContextLoader.class)
public class SpringIOCTests
{
    @Autowired 
    public CoffeeMachine cMachine;

    @Test
    public void influxDevTest()
    {
        assertEquals(Type.BEST, cMachine.getBeans());
    }
}

The way this configures leads me to believe that using auto-wiring in such a way should be attainable in the actual application instead of using these test-only dependencies such as the ContextConfiguration. I should also note that this unit test does pass.

Does Spring offer a methodology in which one can auto-wire dependencies in a nice and clean way avoiding the direct use of an application contexts?

moonboy
  • 1,296
  • 4
  • 16
  • 29
  • That's impossible. The `ApplicationContext` is the center piece of Spring's IOC, it is the container. Your test case simply hides it from you in the `SpringJUnit4ClassRunner`. – Sotirios Delimanolis Feb 22 '17 at 00:23
  • Do wish to avoid application context creation in test cases? If so why not do mocking of the autowired classes on your test-targeted class? – buræquete Feb 22 '17 at 00:25
  • No, the test cases work fine because the application context is handled by the ContextConfiguration annotation. What I want is to be able to apply this same sort of idea, of avoiding directly using the application context. I would prefer to be able to hide it in some sort of Spring annotation. – moonboy Feb 22 '17 at 00:29
  • 1
    Tests aren't magic. There's a `main` method that detects the `SpringJUnit4ClassRunner`, instantiates it and uses it to create an `ApplicationContext` which then loads the classes you've mentioned in the `@ContextConfiguration` and runs the tests. If you want something similar, write it. Annotations aren't magic either. Something processes them. – Sotirios Delimanolis Feb 22 '17 at 00:35
  • In case it wasn't clear, I understand how and why they work in the test cases. I am not concerned with how or where the application context is called under the hood so long as the auto-wiring works as I would like it to. From what you are telling me, it sounds as though there is absolutely nothing built in spring to accommodate auto-wiring in this manner. Is that correct? – moonboy Feb 22 '17 at 00:42
  • That is correct. Something has to create the `ApplicationContext` that links up your beans. – Sotirios Delimanolis Feb 22 '17 at 00:53
  • @SotiriosDelimanolis I'm not sure if it is what he is asking or not but wouldn't this `@ComponentScan("his.package.hierarchy")` be what he is seeking? – Jorge Campos Feb 22 '17 at 00:59
  • @JorgeCampos That _alone_ doesn't avoid the direct use of an application context. – Sotirios Delimanolis Feb 22 '17 at 01:04

0 Answers0