'Spring @Bean Inject Enum

In my Config I define two beans of the same class that differ in the value of an enum:

@Bean
public MyClass myClassNew() {
  return new MyClass(Source.NEW);
}

@Bean
public MyClass myClassOld() {
  return new MyClass(Source.OLD);
}

The Class looks like this:

@Component
@RequiredArgsConstructor
public class MyClass {
  private final Source source; // Source is an enum
}

When I autowire a bean in a test:

public class MyClassTest {

  @Autowired
  private MyClass myClassNew;

}

Spring gives me the following error:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type '...AClass$Source' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

This is totally weird. Why does Spring trying to find a bean of an enum?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source