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
20
votes
4 answers

Could not autowire. No beans of SimpMessagingTemplate type found

I am configuring Websockets in Spring basically by following the guide provided in the documentation. I am currently trying to send a message from the server to the client as explained in the section "Sending messages from anywhere" Following the…
Tk421
  • 6,196
  • 6
  • 38
  • 47
20
votes
4 answers

How does Mockito @InjectMocks work?

Here's my question: I have several web services classes to test that all inherit their methods from a generic service. Rather than write a unit test for each, I figure I can break the test suite down by functional areas (i.e. three groups of test…
DYezek
  • 324
  • 1
  • 3
  • 7
20
votes
3 answers

2 beans with same name but in different packages; how to autowire them?

I have an application that has 2 beans with the same name, but which are in different packages. My Spring application fails because it cannot decide on which bean to take. Is there any solution for this? The beans do not currently implement specific…
Marco
  • 15,101
  • 33
  • 107
  • 174
19
votes
2 answers

Spring inject without autowire annotation

I find some answer: https://stackoverflow.com/a/21218921/2754014 about Dependency Injection. There isn't any annotation like @Autowired, @Inject or @Resource. let's assume that there isn't any XML configuration for this example TwoInjectionStyles…
kuba44
  • 1,838
  • 9
  • 34
  • 62
19
votes
3 answers

Spring @Autowired not working on new thread

When I run TaskJob I am getting null pointer exception because Spring doesn't autowiring serviceJob service. Is new thread causing this problem because Spring autowired mysqlService without any problem? public class TaskJob implements Runnable { …
hellzone
  • 5,393
  • 25
  • 82
  • 148
19
votes
7 answers

Spring Integration Test is Slow with Autowiring

I am trying to speed up the Integration tests in our environment. All our classes are autowired. In our applicationContext.xml file we have defined the following:
Tihom
  • 3,384
  • 6
  • 36
  • 47
18
votes
6 answers

Spring @Autowired on a class new instance

I'm not so familiar with Spring and I have the following situation: A repository class: @Repository public class MyRepository { // ... } A class that uses the repository class: public class MyClass extends AbstractClass { @Autowired …
João Menighin
  • 3,083
  • 6
  • 38
  • 80
18
votes
1 answer

Inject request scoped bean into another bean

I want to create a UUID that is unique in a request life cycle. To do this, I create a UUID bean with the @Scope("request") annotation. @Bean @Scope(scopeName = WebApplicationContext.SCOPE_REQUEST) public UUID requestUUID() { return…
YLombardi
  • 1,755
  • 5
  • 24
  • 45
18
votes
4 answers

How to make instance of CrudRepository interface during testing in Spring?

I have a Spring application and in it I do not use xml configuration, only Java Config. Everything is OK, but when I try to test it I faced problems with enabling autowiring of components in the tests. So let's begin. I have an…
Xelian
  • 16,680
  • 25
  • 99
  • 152
18
votes
2 answers

Spring expected at least 1 bean which qualifies as autowire candidate for this dependency

I have a trouble with this Autowire: @Controller public class ChiusuraController { @Autowired private ChiusuraProvider chiusuraProvider; } with this bean: @Service @Transactional public class ChiusuraProvider extends ThreadProvider { …
Tobia
  • 9,165
  • 28
  • 114
  • 219
17
votes
4 answers

Is Spring @autowired not meant for non-singleton containers?

I have a MyTask class which implements Runnable and there can be many such objects instantiated at any given moment. There are certain properties that I would like to autowire into MyTask class. But I think that if I mark MyTask with @Component then…
pulkitsinghal
  • 3,855
  • 13
  • 45
  • 84
17
votes
4 answers

Spring DI - Autowired property is null in a REST service

I'm getting started with Spring DI, but I'm struggling with dependency injection and the worse part is that I'm not even sure why as it seems ok to me. Hopefully you guys can help me out! The problem is that a property annotated as @Autowired is…
diegosasw
  • 13,734
  • 16
  • 95
  • 159
16
votes
5 answers

Spring bean injection in a main method class

I have a web application with spring 3.0. I need to run a class with main method from a cron that uses beans defined in appcontext xml(using component scan annocations). I have my main class in same src directory. How can I inject beans from web…
16
votes
3 answers

Spring Dependency Injection Autowiring Null

I was able to use RestTemplate and autowire it. However I want to move my rest template related part of code into another class as follows: public class Bridge { private final String BASE_URL = "http://localhost:8080/u"; @Autowired …
kamaci
  • 72,915
  • 69
  • 228
  • 366
16
votes
3 answers

What is the best way to inject mocked Spring @Autowired dependencies from a unit test?

import org.springframework.beans.factory.annotation.Autowired; class MyService { @Autowired private DependencyOne dependencyOne; @Autowired private DependencyTwo dependencyTwo; public void doSomething(){ //Does something with…
Daniel Alexiuc
  • 13,078
  • 9
  • 58
  • 73