1

I've created a bean that expects to be injected with all beans of a given type... something like this:

public class MyClass {
  private List<MyOtherBean> myOtherBeansList;
  ....
  @Inject
  public void setMyOtherBeanList(List<MyOtherBean> otherBeanList) {
    this.myOtherBeansList = otherBeanList;
  }

This works fine, except when I have no beans of type 'MyOtherBeans' defined. In my business logic, that's ok! But spring doesn't like it and throws the overly familiar:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type...

exception. Does anyone know of a way to get spring to just pass me null in that case?

ticktock
  • 1,593
  • 3
  • 16
  • 38
  • 1
    possible duplicate of [Spring: how to ignore @Autowired property if bean is not defined](http://stackoverflow.com/questions/7686723/spring-how-to-ignore-autowired-property-if-bean-is-not-defined) – kryger Jun 17 '14 at 22:44

1 Answers1

1

Nevermind.. I'm dumb.

@Autowired(required=false)

ticktock
  • 1,593
  • 3
  • 16
  • 38
  • `required` on [`@Inject`](http://docs.oracle.com/javaee/6/api/javax/inject/Inject.html)? Didn't you mean [`@Autowired`](http://docs.spring.io/spring/docs/4.0.4.RELEASE/javadoc-api/org/springframework/beans/factory/annotation/Autowired.html#required--)? -> http://stackoverflow.com/q/19485878/1240557 – kryger Jun 17 '14 at 22:41
  • Yes, you're correct. Inject and Autowired are synonymous in behavior, so I just assumed. I had switched to Autowired in my code to try to resolve the issue, and didn't bother checking Inject. Mea Culpa. Edited. – ticktock Jun 19 '14 at 17:06
  • See also [here](http://stackoverflow.com/questions/19485878/can-inject-be-made-optional-in-jsr-330-like-autowirerequired-false?lq=1) for more info on my error. – ticktock Jun 19 '14 at 17:08