spring autowiring with unique beans: Spring expected single matching bean but found 2

The issue is because you have a bean of type SuggestionService created through @Component annotation and also through the XML config . As explained by JB Nizet, this will lead to the creation of a bean with name ‘suggestionService’ created via @Component and another with name ‘SuggestionService’ created through XML . When you refer SuggestionService … Read more

Inject bean into enum

public enum ReportType { REPORT_1(“name”, “filename”), REPORT_2(“name”, “filename”); @Component public static class ReportTypeServiceInjector { @Autowired private DataPrepareService dataPrepareService; @PostConstruct public void postConstruct() { for (ReportType rt : EnumSet.allOf(ReportType.class)) rt.setDataPrepareService(dataPrepareService); } } […] } weekens’ answer works if you change inner class to static so spring can see it

Where is the @Autowired annotation supposed to go – on the property or the method?

According to the Javadoc for Autowired, the annotation can be used on “a constructor, field, setter method or config method”. See the full documentation for more details. I personally prefer your first option (constructor injection), because the myDao field can be marked as final: @Controller public class MyControllear { private final MyDao myDao; @Autowired public … Read more

Spring injects dependencies in constructor without @Autowired annotation

Starting with Spring 4.3, if a class, which is configured as a Spring bean, has only one constructor, the @Autowired annotation can be omitted and Spring will use that constructor and inject all necessary dependencies. Regarding the default constructor: You either need the default constructor, a constructor with the @Autowired annotation when you have multiple … Read more

Spring Boot – Environment @Autowired throws NullPointerException

Though your specific problem is solved, here’s how to get Environment in case Spring’s autowiring happens too late. The trick is to implement org.springframework.context.EnvironmentAware; Spring then passes environment to setEnvironment() method. This works since Spring 3.1. An example: @Configuration @PropertySource(“classpath:myProperties.properties”) public class MyConfiguration implements EnvironmentAware { private Environment environment; @Override public void setEnvironment(final Environment environment) … Read more

Autowired Environment is null

Autowiring happens later than load() is called (for some reason). A workaround is to implement EnvironmentAware and rely on Spring calling setEnvironment() method: @Configuration @ComponentScan(basePackages = “my.pack.offer.*”) @PropertySource(“classpath:OfferService.properties”) public class PropertiesUtil implements EnvironmentAware { private Environment environment; @Override public void setEnvironment(final Environment environment) { this.environment = environment; } @Bean public String load(String propertyName) { return … Read more

Autowiring two beans implementing same interface – how to set default bean to autowire?

I’d suggest marking the Hibernate DAO class with @Primary, i.e. (assuming you used @Repository on HibernateDeviceDao): @Primary @Repository public class HibernateDeviceDao implements DeviceDao This way it will be selected as the default autowire candididate, with no need to autowire-candidate on the other bean. Also, rather than using @Autowired @Qualifier, I find it more elegant to … Read more