I've made a maven project in Spring 3.0, I've made some DAO, services and controllers, in one of mine controller I call a service in which I start a thread, the problem is that in the thread I declare a "service variable" that should be initialized with Autowired annotiation, but it doesn't work and the variable isn't initilized and has the value null.
this is the thread class
package com.project.tasks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;
import com.project.entities.user.User;
import com.project.services.IUserService;
@Component
public class AddFriendInMyFriendListTaskExecutor {
private class AddFriendInMyFriendListTask implements Runnable {
// HERE IS THE PROBLEM
@Autowired
private IUserService uService;
private User a;
private User b;
public AddFriendInMyFriendListTask() {
;
}
public AddFriendInMyFriendListTask(User aA, User bB) {
a = aA;
b = bB;
}
public User getA() {
return a;
}
public void setA(User a) {
this.a = a;
}
public User getB() {
return b;
}
public void setB(User b) {
this.b = b;
}
public void run() {
// FROM HERE IT PRINTS THE VALUE OF uService THAT IS NULL
System.out.println("uService:" + uService);
uService.insertRightUserIntoLeftUserListOfFriends(a, b);
}
}
private TaskExecutor taskExecutor;
public AddFriendInMyFriendListTaskExecutor(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void doIt(User a, User b) {
taskExecutor.execute(new AddFriendInMyFriendListTask(a, b));
}
}
this is the piece of code that calls the thread
User a = uDao.getUser(hrA.getMyIdApp());
User b = uDao.getUser(hrA.getOtherIdApp());
SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
AddFriendInMyFriendListTaskExecutor tmp = new AddFriendInMyFriendListTaskExecutor(taskExecutor);
tmp.doIt(a, b);
I'd like to highlight that in all the other tests in which I don't call any threads, the Autowired of a UserService instance functions correctly! The method I call: insertRightUserIntoLeftUserListOfFriends(User a, User b), works correctly.
There are several reasons @Autowired might not work. When a new instance is created not by Spring but by for example manually calling a constructor, the instance of the class will not be registered in the Spring context and thus not available for dependency injection.
The answer for your question is YES. You can use autowired in thread class.
If we had used Autowired, we would have been getting the following error in the test. Therefore, it is not recommended to use Autowired. Safety — Forces Spring to provide mandatory dependencies. We make sure that the created objects are valid after construction.
There are three weaknesses of autowiring : Overriding: You can still specify dependencies using <constructor-arg> and <property> settings which will always override @Autowired . Primitive data types: Autowiring can't be used to inject primitive and string values. It works with reference only.
For a bean to be autowired by Spring, the bean must be a Spring bean (i.e. be declared in the context.xml file or be annotated with a Spring annotation (@Service
, @Component
, etc.).
And of course, it must be instantiated by Spring, and not by your code. If you instantiate a Spring bean yourself with new
, Spring doesn't know about the bean, and doesn't inject anything into it.
Spring just autowires beans of the context, no instances created by new. But why do you have declared uService
in AddFriendInMyFriendListTask
and not as a bean property of the outer (bean) class AddFriendInMyFriendListTaskExecutor
, that should simply work:
@Component
public class AddFriendInMyFriendListTaskExecutor {
private class AddFriendInMyFriendListTask implements Runnable {
private final User a;
private final User b;
public AddFriendInMyFriendListTask(User aA, User bB) {
a = aA;
b = bB;
}
public void run() {
AddFriendInMyFriendListTaskExecutor.this.uService.insertRightUserIntoLeftUserListOfFriends(a, b);
}
}
@Autowired
private IUserService uService;
@Autowired
private TaskExecutor taskExecutor;
public void doIt(User a, User b) {
taskExecutor.execute(new AddFriendInMyFriendListTask(a, b));
}
}
(removed some unused getter/setter and made taskExecutor also a bean property)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With