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

Spring injects dependencies in constructor without @Autowired annotation

I'm experimenting with examples from this official Spring tutorials and there is a dependency on this code: https://github.com/spring-guides/gs-async-method/tree/master/complete If you look at the code on AppRunner.java class, I have 2…
winter
  • 2,687
  • 4
  • 18
  • 27
120
votes
1 answer

Understanding spring @Configuration class

Following the question Understanding Spring @Autowired usage I wanted to create a complete knowledge base for the other option of spring wiring, the @Configuration class. Let's assume I have a spring XML file that looks like this:
Avi
  • 21,182
  • 26
  • 82
  • 121
117
votes
23 answers

@Autowired - No qualifying bean of type found for dependency

I've started my project by creating entities, services and JUnit tests for services using Spring and Hibernate. All of this works great. Then I've added spring-mvc to make this web application using many different step-by-step tutorials, but when…
Radzikowski
  • 2,377
  • 4
  • 27
  • 39
113
votes
4 answers

Why can't we autowire static fields in spring?

Why can't we autowire the static instance variable in the Spring bean. I know there is another way to achieve this but just want to know why cant we do it in below way. e.g. @Autowired public static Test test;
Ashu
  • 1,703
  • 4
  • 16
  • 23
112
votes
15 answers

Requested bean is currently in creation: Is there an unresolvable circular reference?

i am using spring 3, and i have two beans of view scope: 1- Bean1: @Component("bean1") @Scope("view") public class Bean1 { @Autowired private Bean2 bean2; } 2- Bean2: @Component("bean2") @Scope("view") public class Bean2 { @Autowired private…
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
105
votes
10 answers

Exclude subpackages from Spring autowiring?

Is there a simple way to exclude a package / sub-package from autowiring in Spring 3.1? E.g., if I wanted to include a component scan with a base package of com.example is there a simple way to exclude com.example.ignore? (Why? I'd like to exclude…
HolySamosa
  • 9,011
  • 14
  • 69
  • 102
104
votes
6 answers

How to Autowire Bean of generic type in Spring?

I have a bean Item which is required to be autowired in a @Configuration class. @Configuration public class AppConfig { @Bean public Item stringItem() { return new StringItem(); } @Bean public Item
user3374518
  • 1,359
  • 3
  • 11
  • 11
103
votes
3 answers

@Autowired bean is null when referenced in the constructor of another bean

Shown below is a snippet of code where I try and reference my ApplicationProperties bean. When I reference it from the constructor it is null, but when referenced from another method it is fine. Up until now I have not had no problem using this…
hairyone
  • 1,033
  • 2
  • 7
  • 4
102
votes
6 answers

Injecting @Autowired private field during testing

I have a component setup that is essentially a launcher for an application. It is configured like so: @Component public class MyLauncher { @Autowired MyService myService; //other methods } MyService is annotated with the @Service…
Kyle
  • 14,036
  • 11
  • 46
  • 61
88
votes
4 answers

How to set @Autowired constructor params as "required=false" individually

I'm using the @Autowired annotation under a @Configuration class constructor. @Configuration public class MyConfiguration { private MyServiceA myServiceA; private MyServiceB myServiceB @Autowired public MyConfiguration(MyServiceA…
jcgarcia
  • 3,762
  • 4
  • 18
  • 32
88
votes
4 answers

Spring: get all Beans of certain interface AND type

In my Spring Boot application, suppose I have interface in Java: public interface MyFilter (a good example is Spring's public interface ApplicationListener< E extends ApplicationEvent > ) and I have couple of…
onkami
  • 8,791
  • 17
  • 90
  • 176
83
votes
4 answers

Spring can you autowire inside an abstract class?

Spring is failing to autowire my object? Is it possible to autowire an object within an abstract class. Assume all schemas are supplied in application-context.xml Question: What annotation should be on the base and extending classes (if any)…
stackoverflow
  • 18,348
  • 50
  • 129
  • 196
74
votes
8 answers

Inject bean into enum

I have the DataPrepareService that prepare data for reports and I have an Enum with report types, and I need to inject ReportService into Enum or have access to ReportService from enum. my service: @Service public class DataPrepareService { //…
Andrej Soroj
  • 1,103
  • 1
  • 9
  • 10
73
votes
5 answers

Using Spring 3 autowire in a standalone Java application

Here is my code: public class Main { public static void main(String[] args) { Main p = new Main(); p.start(args); } @Autowired private MyBean myBean; private void start(String[] args) { …
68
votes
4 answers

spring autowiring with unique beans: Spring expected single matching bean but found 2

I am trying to autowire some beans (for dependency injection) using Spring for a webapp. One controller bean contains another bean which in turn holds a hashmap of another set of beans. For now the map only has one entry. When i run in tomcat and…
Rob McFeely
  • 2,823
  • 8
  • 33
  • 50