为什么自动装配在线程中不起作用? [英] Why does Autowiring not function in a thread?

查看:111
本文介绍了为什么自动装配在线程中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring 3.0中制作了一个Maven项目,我制作了一些DAO,服务和控制器,在我的一个控制器中,我将其称为启动线程的服务,问题是在声明的线程中应该使用自动装配注释初始化的服务变量",但是它不起作用并且该变量未初始化且值为空.

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.

这是线程类

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));
  }
}

这是调用线程的代码

    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);

我想强调一点,在我不调用任何线程的所有其他测试中,UserService实例的自动装配功能正常! 我调用的方法:insertRightUserIntoLeftUserListOfFriends(User a,User 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.

推荐答案

对于要由Spring自动装配的bean,该bean必须是Spring bean(即在context.xml文件中声明或用Spring注释进行注释) (@Service@Component等).

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.).

当然,它必须由Spring实例化,而不是由您的代码实例化.如果用new自己实例化一个Spring bean,Spring不会知道该bean,也不会向其中注入任何东西.

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.

这篇关于为什么自动装配在线程中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆