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
789
votes
11 answers

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

I am going through some blogs on SpringSource and in one of the blogs, author is using @Inject and I suppose he can also use @Autowired. Here is the piece of code: @Inject private CustomerOrderService customerOrderService; I am not sure about the…
Rachel
  • 100,387
  • 116
  • 269
  • 365
761
votes
21 answers

Why is my Spring @Autowired field null?

Note: This is intended to be a canonical answer for a common problem. I have a Spring @Service class (MileageFeeCalculator) that has an @Autowired field (rateService), but the field is null when I try to use it. The logs show that both the…
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
709
votes
16 answers

Spring: @Component versus @Bean

I understand that @Component annotation was introduced in spring 2.5 in order to get rid of xml bean definition by using classpath scanning. @Bean was introduced in spring 3.0 and can be used with @Configuration in order to fully get rid of xml file…
user1396576
  • 7,101
  • 3
  • 14
  • 4
582
votes
11 answers

How does autowiring work in Spring?

I'm a little confused as to how the inversion of control (IoC) works in Spring. Say I have a service class called UserServiceImpl that implements UserService interface. How would this be @Autowired? And in my Controllers, how would I instantiate an…
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
439
votes
11 answers

@Resource vs @Autowired

Which annotation, @Resource (jsr250) or @Autowired (Spring-specific) should I use in DI? I have successfully used both in the past, @Resource(name="blah") and @Autowired @Qualifier("blah") My instinct is to stick with the @Resource tag since it's…
mlo55
  • 6,663
  • 6
  • 33
  • 26
350
votes
3 answers

Understanding Spring @Autowired usage

I am reading the spring 3.0.x reference documentation to understand Spring Autowired annotation: 3.9.2 @Autowired and @Inject I am not able to understand the below examples. Do we need to do something in the XML for it to work? EXAMPLE 1 public…
NewQueries
  • 4,841
  • 11
  • 33
  • 39
320
votes
10 answers

Spring @Autowire on Properties vs Constructor

So since I've been using Spring, if I were to write a service that had dependencies I would do the following: @Component public class SomeService { @Autowired private SomeOtherService someOtherService; } I have now run across code that uses…
GSUgambit
  • 4,459
  • 6
  • 25
  • 31
290
votes
4 answers

What exactly is Field Injection and how to avoid it?

I read in some posts about Spring MVC and Portlets that field injection is not recommended. As I understand it, field injection is when you inject a Bean with @Autowired like this: @Component public class MyComponent { @Autowired private…
T. Jung
  • 3,327
  • 3
  • 11
  • 19
260
votes
50 answers

intellij incorrectly saying no beans of type found for autowired repository

I have created a simple unit test but IntelliJ is incorrectly highlighting it red. marking it as an error No beans? As you can see below it passes the test? So it must be Autowired?
Robbo_UK
  • 11,351
  • 25
  • 81
  • 117
258
votes
9 answers

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

I'm using Spring 3.1.4.RELEASE and Mockito 1.9.5. In my Spring class I have: @Value("#{myProps['default.url']}") private String defaultUrl; @Value("#{myProps['default.password']}") private String defaultrPassword; // ... From my JUnit test,…
Dave
  • 15,639
  • 133
  • 442
  • 830
222
votes
9 answers

Spring @Autowired usage

What are the pros and cons of using @Autowired in a class that will be wired up by Spring? Just to clarify, I'm talking specifically about the @Autowired annotation, not auto-wiring in XML. I probably just don't understand it, but to me it almost…
Andy White
  • 86,444
  • 48
  • 176
  • 211
201
votes
13 answers

Can you use @Autowired with static fields?

Is there some way to use @Autowired with static fields. If not, are there some other ways to do this?
user124722
146
votes
5 answers

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

Background: I have a Spring 2.5/Java/Tomcat application. There is the following bean, which is used throughout the application in many places public class HibernateDeviceDao implements DeviceDao and the following bean which is new: public class…
simon
  • 12,666
  • 26
  • 78
  • 113
144
votes
9 answers

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

I'm using Spring 3.0.5 and am using @Autowire annotation for my class members as much as possible. One of the beans that I need to autowire requires arguments to its constructor. I've looked through the Spring docs, but cannot seem to find any…
Eric B.
  • 23,425
  • 50
  • 169
  • 316
140
votes
7 answers

@Autowired and static method

I have @Autowired service which has to be used from within a static method. I know this is wrong but I cannot change the current design as it would require a lot of work, so I need some simple hack for that. I can't change randomMethod() to be…
Taks
  • 2,033
  • 4
  • 18
  • 23
1
2 3
99 100