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
38
votes
2 answers

Autowiring in servlet

i want to use spring autowiring in servlet so here's my code: @Configurable public class ImageServlet extends HttpServlet { @Autowired private SystemPropertyDao systemPropertyDao; @Override public void init() throws ServletException…
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
37
votes
5 answers

@Autowired in static classes

This is an Spring MVC project with Hibernate. I'm, trying to make a Logger class that, is responsible for inputting logs into database. Other classes just call proper methods with some attributes and this class should do all magic. By nature it…
T.G
  • 1,913
  • 1
  • 16
  • 29
36
votes
2 answers

How to autowire by name in Spring with annotations?

I have several beans of the same class defined: @Bean public FieldDescriptor fullSpotField() { FieldDescriptor ans = new FieldDescriptor("full_spot", String.class); return ans; } @Bean public FieldDescriptor annotationIdField()…
Dims
  • 47,675
  • 117
  • 331
  • 600
34
votes
5 answers

How to get beans created by FactoryBean spring managed?

The FactoryBean can be used to programmatically create objects which might require complex instantiation logic. However, it seems that the beans created by the FactoryBean doesn't become spring managed. Is this interpretation correct? If so, are…
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
33
votes
2 answers

How to dynamically inject a service using a runtime "qualifier" variable?

I can't find a simple way to inject a component/service given a runtime value. I started reading @ Spring's doc: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-autowired-annotation-qualifiers but I can't…
maxxyme
  • 2,164
  • 5
  • 31
  • 48
33
votes
2 answers

Where is the @Autowired annotation supposed to go - on the property or the method?

Which is more correct? This (with the @Autowired annotation on the method)? @Controller public class MyController { private MyDao myDao; @Autowired public MyController(MyDao myDao) { this.myDao = myDao; } This (with the…
32
votes
1 answer

Spring 3.0.5 doesn't evaluate @Value annotation from properties

Trying to auto-wire properties to a bean in Spring 3.0.5.RELEASE, I'm using: config.properties: username=myusername main-components.xml: MyClass: @Service public class…
Alex Yarmula
  • 10,477
  • 5
  • 33
  • 32
31
votes
5 answers

Is it possible to wire a Spring MVC Interceptor using annotations?

Is it possible to wire a Spring MVC Interceptor using annotations and if so could someone provide me with an example of how to do so? By wire via annotation I am referring to doing as little in the XML configuration as possible. For example in this…
James McMahon
  • 48,506
  • 64
  • 207
  • 283
31
votes
3 answers

How to mock a autowired list of Spring beans?

I've read plenty of articles about how to mock Spring's bean and their autowired fields. But there is nothing I could find about autowired lists of beans. Concrete problem I've a class called FormValidatorManager. This class loop through several…
Grégory Elhaimer
  • 2,731
  • 1
  • 16
  • 21
30
votes
3 answers

@Autowired vs @Required on setter

I'm curious to know what's the difference between code like this: class MyClass { @Autowired MyService myService; } and code like this: class MyClass { MyService myService; @Required public void setMyService(MyService val) { …
0x56794E
  • 20,883
  • 13
  • 42
  • 58
30
votes
3 answers

Spring circular reference example

I have a circular reference in one of my projects at work using spring, which I am unable to fix, and fails with the following error at startup: 'org.springframework.security.authenticationManager': Requested bean is currently in creation: Is there…
robbin
  • 1,924
  • 4
  • 20
  • 26
29
votes
11 answers

Autowiring BuildProperties bean from Gradle - NoSuchBeanDefinitionException

I am trying to obtain the version in my Java application from the Gradle build file. I am following the instructions here; https://docs.spring.io/spring-boot/docs/current/reference/html/howto-build.html build.gradle buildscript { repositories { …
Jags
  • 1,639
  • 1
  • 16
  • 30
29
votes
2 answers

what is the difference between request and session scope in spring?

In request scope, a bean is defined to an HTTP request whereas in session scope, it is scoped to an HTTP session. So for an instance, if the bean scope is request and, a user makes more than one request for a web page in his user session, then on…
greenHorn
  • 497
  • 1
  • 5
  • 16
29
votes
7 answers

Spring 3.2 Autowire generic types

So I have a number of generics in Spring 3.2 and ideally my architecture would look something like this. class GenericDao{} class GenericService> { // FAILS @Autowired T_DAO; } @Component class…
28
votes
2 answers

How do I manually autowire a bean with Spring?

I have a bean B which I have to create myself (using new B()) and which has @Autowire and @PostConstruct annotations. How do I make Spring process these annotations from my bean A? Related question: In Spring, can I autowire new beans from inside…
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820