Questions tagged [autowired]

Autowiring is a DI container feature where the dependencies are automatically looked for according to some criteria.

In a like , the beans to be injected can be explicitely configured or they can be searched for automatically in what is called "autowiring".

Although the concept is not exclusive to Spring, the bulk of questions in Stack Overflow and Google seem to be directed towards this framework.

Spring

In the particular case of Spring, when "autowiring" is enabled, the container will look in the current ApplicationContext for a bean that matches the type/name of the corresponding setter method.

Autowiring can be configured globally or on each bean to be disabled; or enabled by type, by name, by constructor type. It can also be configured using the Autowired annotation.

2781 questions
66
votes
3 answers

Inject and Resource and Autowired annotations

What's the difference between @Inject and @Resource and @Autowired annotations? When should we use each of them?
oxygenan
  • 1,023
  • 2
  • 12
  • 21
59
votes
8 answers

No matching bean of type ... found for dependency

after some days of trying and waitin' for answers on the springsource forums I'll try it here. Running my application results in these exception: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type…
dtrunk
  • 4,685
  • 17
  • 65
  • 109
58
votes
5 answers

Autowired Environment is null

I have an issue with connecting environment to my Spring project. In this class @Configuration @ComponentScan(basePackages = "my.pack.offer.*") @PropertySource("classpath:OfferService.properties") public class PropertiesUtil { @Autowired …
LeYar
  • 714
  • 1
  • 5
  • 7
54
votes
7 answers

Spring boot autowiring an interface with multiple implementations

In normal Spring, when we want to autowire an interface, we define it's implementation in Spring context file. What about Spring boot? how can we achieve this? currently we only autowire classes that are not interfaces. Another part of this…
user666
  • 1,750
  • 3
  • 18
  • 34
54
votes
5 answers

Autowiring spring bean by name using annotation

In Springs latest version, we can autowire a bean using annotation as @Autowired. This will autowire the bean using its type(or constructor, if applied on it). Is there any way I can use the @Autowired annotation based on the bean name which we were…
Anand
  • 20,708
  • 48
  • 131
  • 198
52
votes
4 answers

Injection of autowired dependencies failed;

I am developing a small Java EE Hibernate Spring application and an error appeared: Error creating bean with name 'articleControleur': Injection of autowired dependencies failed; oct. 26, 2011 3:51:44 PM org.apache.catalina.core.ApplicationContext…
achref05
  • 1,653
  • 4
  • 16
  • 18
52
votes
4 answers

How does Spring annotation @Autowired work?

I came across an example of @Autowired: public class EmpManager { @Autowired private EmpDao empDao; } I was curious about how the empDao get sets since there are no setter methods and it is private.
Anthony
  • 1,685
  • 4
  • 22
  • 29
52
votes
8 answers

Can spring @Autowired Map?

Here's the Map @Autowired private Map converters; and ISendableConverter public interface ISendableConverter { ISendableMsg convert(BaseMessage baseMessage); String getType(); } There are some classes that…
Joe
  • 3,581
  • 4
  • 23
  • 28
51
votes
5 answers

Using @Spy and @Autowired together

I have a Service Class with 3 methods, Service class is also using some @Autowired annotations. Out of 3 methods, I want to mock two methods but use real method for 3rd one. Problem is: If I am using @Autowired with @Spy, all three real method…
Shashi K Kalia
  • 673
  • 1
  • 5
  • 8
48
votes
14 answers

Failed Autowired of BuildProperties Spring Boot 2.1.5 & eclipse

I'm creating a very simple application with a few REST API's and it's currently working correctly until I try to use the BuildProperties on my health check API. While starting my application I get the following error: Error starting…
ROOTKILL
  • 575
  • 1
  • 4
  • 7
44
votes
4 answers

spring 3 autowiring and junit testing

My code: @Component public class A { @Autowired private B b; public void method() {} } public interface X {...} @Component public class B implements X { ... } I want to test in isolation class A. Do I have to mock class B? If…
mike27
  • 965
  • 3
  • 12
  • 22
43
votes
7 answers

how to use spring annotations like @Autowired or @Value in kotlin for primitive types?

Autowiring a non-primitive with spring annotations like bellow works: @Autowired lateinit var metaDataService: MetaDataService But this doesn't work: @Value("\${cacheTimeSeconds}") lateinit var cacheTimeSeconds: Int Error: lateinit modifier is…
fkurth
  • 868
  • 1
  • 8
  • 11
43
votes
1 answer

Spring Autowiring class vs. interface?

I have this Spring config: The class TheClass implements TheInterface. Then I have this (hypothetical) Java code: @Autowired TheInterface x; @Autowired TheClass y; The autowiring of TheInterface works but…
Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
39
votes
6 answers

Spring autowire a list

Is it possible to use @Autowired with a list? Like I have properties file with mimetypes and in my class file I have something like this @Autowired private List mimeTypes = new ArrayList();
karq
  • 849
  • 2
  • 14
  • 27
39
votes
2 answers

Spring Boot Unit Test Autowired

I have the following classes : ApplicationAndConfiguration class package mypackage.service; import mypackage.service.util.MyUtility; import org.springframework.boot.SpringApplication; import…
hublo
  • 1,010
  • 2
  • 12
  • 33