title: Spring Autowired null if not found
body: <p>Default <code>@Autowired</code> Spring implementation throws error when bean that should be autowired is not defined. Is it possible to configure Spring, that will assign null to object instead of throwing exception?</p>
  
  <p>EDIT:</p>
  
  <p>I've add <code>required=false</code> to <code>Autowired</code> but it still not working properly. 
  Thats my code:</p>
  
  <pre><code>@Autowired
  private ApplicationContext applicationContext;
  
  @Autowired(required = false)
  private HelloService helloService;
  
  public HelloController() {
      message = "Hello World";
      System.out.println("Controller constructor");
  }
  
  @RequestMapping(method = RequestMethod.GET)
  public ModelAndView helloWorld() {
      ModelAndView modelAndView = new ModelAndView("hello");
      if (helloService == null) {
          System.out.println(message);
      } else {
          helloService.hello();
          BeanDefinitionRegistry factory = (BeanDefinitionRegistry) applicationContext.getAutowireCapableBeanFactory();
          factory.removeBeanDefinition("helloService");
      }
      return modelAndView;
  }
  </code></pre>
  
  <p>In first request it's autowired, but in next request after removing bean with <code>factory.removeBeanDefinition("helloService")</code>, controller bean is construct again, and i get <code>NoSuchBeanDefinitionException</code></p>
  
  <p>EDIT2:</p>
  
  <p>I've created another controller with the following body:</p>
  
  <pre><code>@Autowired(required = false)
  private TestService testService;
  
  @RequestMapping(method = RequestMethod.GET)
  public ModelAndView hello() {
      ModelAndView modelAndView = new ModelAndView("hello");
      return modelAndView;
  }
  </code></pre>
  
  <p>and it works properly - Object is null and it doesn't get error. Maybe i should use different method to remove bean from Spring context?</p>
  
  <p>Stacktrace:</p>
  
  <pre><code>org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'helloService' is defined
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1175) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.resolvedCachedArgument(AutowiredAnnotationBeanPostProcessor.java:508) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.access$200(AutowiredAnnotationBeanPostProcessor.java:115) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:538) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE] 
  ...
  </code></pre>
  
  <p>STEPS TO REPRODUCE:</p>
  
  <p><a href="https://github.com/nowszy94/Autowired-null" rel="nofollow">https://github.com/nowszy94/Autowired-null</a></p>