Spring DI – Autowired property is null in a REST service

You were right! It seems that the problem is that Jersey is totally unaware of Spring and instantiates its own object. In order to make Jersey aware of Spring object creations (through dependency injection) I had to integrate Spring + Jersey. To integrate: Add maven dependencies <dependency> <groupId>com.sun.jersey.contribs</groupId> <artifactId>jersey-spring</artifactId> <version>1.17.1</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> … Read more

How to Autowire Bean of generic type in Spring?

Simple solution is to upgrade to Spring 4.0 as it will automatically consider generics as a form of @Qualifier, as below: @Autowired private Item<String> strItem; // Injects the stringItem bean @Autowired private Item<Integer> intItem; // Injects the integerItem bean Infact, you can even autowire nested generics when injecting into a list, as below: // Inject … Read more

Get service via class name from iterable – injected tagged services

You no longer (since Symfony 4) need to create a compiler pass to configure a service locator. It’s possible to do everything through configuration and let Symfony perform the “magic”. You can make do with the following additions to your configuration: services: _instanceof: DriverInterface: tags: [‘app.driver’] lazy: true DriverConsumer: arguments: – !tagged_locator tag: ‘app.driver’ The … Read more

How do I mock an autowired @Value field in Spring with Mockito?

You can use the magic of Spring’s ReflectionTestUtils.setField in order to avoid making any modifications whatsoever to your code. The comment from MichaƂ Stochmal provides an example: use ReflectionTestUtils.setField(bean, “fieldName”, “value”); before invoking your bean method during test. Check out this tutorial for even more information, although you probably won’t need it since the method … Read more

Understanding spring @Configuration class

Migrating XML to @Configuration It is possible to migrate the xml to a @Configuration in a few steps: Create a @Configuration annotated class: @Configuration public class MyApplicationContext { } For each <bean> tag create a method annotated with @Bean: @Configuration public class MyApplicationContext { @Bean(name = “someBean”) public SomeClass getSomeClass() { return new SomeClassImpl(someInterestingProperty); // … Read more

Is there a way to @Autowire a bean that requires constructor arguments?

You need the @Value annotation. A common use case is to assign default field values using “#{systemProperties.myProp}” style expressions. public class SimpleMovieLister { private MovieFinder movieFinder; private String defaultLocale; @Autowired public void configure(MovieFinder movieFinder, @Value(“#{ systemProperties[‘user.region’] }”) String defaultLocale) { this.movieFinder = movieFinder; this.defaultLocale = defaultLocale; } // … } See: Expression Language > Annotation … Read more

What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?

Assuming here you’re referring to the javax.inject.Inject annotation. @Inject is part of the Java CDI (Contexts and Dependency Injection) standard introduced in Java EE 6 (JSR-299), read more. Spring has chosen to support using the @Inject annotation synonymously with their own @Autowired annotation. So, to answer your question, @Autowired is Spring’s own annotation. @Inject is … Read more