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
28
votes
3 answers

Spring dependency injection with @Autowired annotation without setter

I'm using Spring since a few months for now, and I thought dependency injection with the @Autowired annotation also required a setter for the field to inject. So, I am using it like this: @Controller public class MyController { @Autowired …
Tony
  • 1,246
  • 2
  • 16
  • 29
26
votes
6 answers

@Autowire default mode

How does Spring @Autowire beans: byName or byType? If one is not possible, is a second trial done using another mode?
user689842
26
votes
4 answers

@autowired on method in Spring

I am learning Spring, and as far as I understand, when we use @annotation on a method which has a generic name (not a setter method), then the method's arguments are autowired. public class MovieRecommender { private MovieCatalog…
Nishit
  • 1,276
  • 2
  • 11
  • 25
26
votes
5 answers

How do I inject a logger into a field in the sample spring boot application?

What I want is to make spring autowire a logger. So, in other words, I want to have this working: import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import…
vic
  • 378
  • 1
  • 3
  • 10
25
votes
1 answer

Does Spring @Autowired inject beans by name or by type?

I am reading beginning spring (wiley press) book. In chapter 2 there is an example about Java configuration and @Autowired. It provides this @Configuration class @Configuration public class Ch2BeanConfiguration { @Bean public AccountService…
Lidovic
  • 273
  • 1
  • 3
  • 6
25
votes
4 answers

Spring Boot - Environment @Autowired throws NullPointerException

I have a project setup using Spring Boot 0.5.0.M5. In one of the configuration files I am trying to @Autowire Environment but that fails with a NullPointerException. Here's what I have so…
imme
  • 299
  • 1
  • 4
  • 8
24
votes
2 answers

Spring wiring by type is slower by magnitude than wiring by name

In my project, I am trying to migrate all usages of Foo foo = (Foo) beanFactory.getBean("name"); into Foo foo = beanFactory.getBean(Foo.class); The benefits are obvious: type safety, less convoluted code, less useless constants, etc. Typically…
mindas
  • 26,463
  • 15
  • 97
  • 154
23
votes
2 answers

Spring: Selecting @Service based on profile

I have an interface define as below: public interface MyService { } And two classes implementing it: @Service @Profile("dev") public class DevImplementation implements MyService { } and @Service @Profile("prod") public class ProdImplementation…
shyam
  • 6,681
  • 7
  • 28
  • 45
23
votes
3 answers

what are @Repository and @Autowired used for. (Spring)

I am learning java for 3 months and sometimes i can not understand the usage purpose of something. one topic was dependency injection and spring beans i figured out the finally =) now i confused with the two annotations @Autowired and…
mehmet6parmak
  • 4,777
  • 16
  • 50
  • 71
23
votes
3 answers

@Autowire strange problem

I have a strange behaviour when autowiring I have a similar code like this one, and it works @Controller public class Class1 { @Autowired private Class2 object2; ... } @Service @Transactional public class Class2{ ... } The problem…
Javi
  • 19,387
  • 30
  • 102
  • 135
21
votes
2 answers

@Autowired HttpServletResponse

I'm looking for a way to autowire HttpServletResponse. It doesn't work with spring out of the box, but I've found this description. This works but is sort of annoying, in that spring obviously has a mechanism to make objects request scoped (i.e.…
Kevin
  • 24,871
  • 19
  • 102
  • 158
21
votes
1 answer

How to Autowire conditionally in spring boot?

I have created one scheduler class public class TestSchedulderNew { @Scheduled(fixedDelay = 3000) public void fixedRateJob1() { System.out.println("Job 1 running"); } @Scheduled(fixedDelay = 3000) public void fixedRateJob2()…
Rajesh Khore
  • 325
  • 1
  • 3
  • 7
20
votes
3 answers

Is it possible to use @Resource on a constructor?

I was wondering if it possible to use the @Resource annotation on a constructor. My use case is that I want to wire a final field called bar. public class Foo implements FooBar { private final Bar bar; …
Marco
  • 15,101
  • 33
  • 107
  • 174
20
votes
2 answers

Clean code - Where should @Autowired be applied?

I'll start with a simple example. You have a Spring boot application that runs a CommandLineRunner class on initialization. // MyCommandLineRunner.java public class MyCommandLineRunner implements CommandLineRunner { private final Log logger =…
Toza
  • 1,348
  • 2
  • 14
  • 35
20
votes
1 answer

Can't I @Autowire a Bean which is present in a dependent Library Jar?

I have a Spring Boot application (Y) which relies upon a set of Library files packed as x.jar and mentioned as a dependency in the pom.xml of the application Y. x.jar has a bean named (User.java) Application Y has a java class named…
yathirigan
  • 5,619
  • 22
  • 66
  • 104