public class SocketOperate extends Thread
这个类我能用spring注解注入吗?
@Autowired
private xxxService Service;
我这样写service为空,不知道怎么注入spring

解决方案 »

  1.   

    你这个需求挺怪异的,要用 spring 管理线程对象。
    需求能否说下?让大家看看能否有替代解决方案。
      

  2.   

    不是spring负责SocketOperate的bean的生成把,
      

  3.   

    SocketOperate  加 @Component 
      

  4.   

    去掉extend Thread就能注入了吗
      

  5.   

    定义成static类型的,或者定义在run方法内看看。
      

  6.   

    只要是交给spring管理的,提供了公共的无参构造方法的,都可以注入srping管理的对象,
    加注解@Server ,@Component,@Controller,@Component 就可以了,继承什么,实现什么都可以,
    或者使用配置文件加入了spring配置文件里
      

  7.   

    想在该内中调用service服务你是想在自己定义的 Thread 类中调用 Service 的某个方法吧?
      

  8.   

    想在该内中调用service服务你是想在自己定义的 Thread 类中调用 Service 的某个方法吧?
    是的,我找到方法了,代码如下:@Component
    public class SpringContextUtil implements ApplicationContextAware { // Spring应用上下文环境
    private static ApplicationContext applicationContext; /**
     * 实现ApplicationContextAware接口的回调方法,设置上下文环境
     */
    public void setApplicationContext(ApplicationContext applicationContext)
    throws BeansException {
    SpringContextUtil.applicationContext = applicationContext;
    } public static ApplicationContext getApplicationContext() {
    return applicationContext;
    } /**
     * 获取对象 这里重写了bean方法,起主要作用
     */
    public static Object getBean(String beanId) throws BeansException {
    return applicationContext.getBean(beanId);
    }
    }
      

  9.   

    首先想要注入的话需要你把Thread交由Spring管理,也就是你取Thread时要通过Spring来取,这样就可以注入了.
      

  10.   

    处理完就结束,最好不要把它交给 spring 管理,用线程池管理吧。
    你现在没问题,是因为并发数太小,线程还没吃光你的内存和 CPU。
      

  11.   

    BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(10);
    ThreadPoolExecutor pool = new ThreadPoolExecutor(3, 10, 500, TimeUnit.SECONDS, queue);
    pool.execute(null);
    pool.shutdown();
      

  12.   

    手动注入可以使用这种方式:AutowireCapableBeanFactory autowireFactory = applicationContext.getAutowireCapableBeanFactory();
    autowireFactory.autowireBean(processor);