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

@Autowired and static method

You can do this by following one of the solutions: Using constructor @Autowired This approach will construct the bean requiring some beans as constructor parameters. Within the constructor code you set the static field with the value got as parameter for constructor execution. Sample: @Component public class Boo { private static Foo foo; @Autowired public … 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

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

Spring @Autowired usage

For a long time I believed that there was a value in having a “centralized, declarative, configuration” like the xml files we all used to use. Then I realized that most of the stuff in the files wasn’t configuration – it was never changed anywhere after development, ever. Then I realized that “centralized” only has … 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

intellij incorrectly saying no beans of type found for autowired repository

I had this same issue when creating a Spring Boot application using their @SpringBootApplication annotation. This annotation represents @Configuration, @EnableAutoConfiguration and @ComponentScan according to the spring reference. As expected, the new annotation worked properly and my application ran smoothly but, Intellij kept complaining about unfulfilled @Autowire dependencies. As soon as I changed back to using … Read more

What exactly is Field Injection and how to avoid it?

Injection types There are three options for how dependencies can be injected into a bean: Through a constructor Through setters or other methods Through reflection, directly into fields You are using option 3. That is what is happening when you use @Autowired directly on your field. Injection guidelines A general guideline, which is recommended by … Read more

Spring @Autowire on Properties vs Constructor

Yes, option B (which is called constructor injection) is actually recommended over field injection, and has several advantages: the dependencies are clearly identified. There is no way to forget one when testing, or instantiating the object in any other circumstance (like creating the bean instance explicitly in a config class) the dependencies can be final, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)