zoukankan      html  css  js  c++  java
  • 在多线程中使用spring的bean

      由于spring在java开发中的广泛运用大大的方便了开发的同时,当运用一些技术比如多线程等

    在由spring管理的配置文件中,可以通过封装spring提供工具,手动获得spring管理的bean,这样

    既可以方便使用bean,又可以同时使用其他技术。

      可以方便的使用多种技术,而不至于由于使用spring导致不好用。

    package com.jd.app.server.irp.service.task;

    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;

    /**
    * Created by liubaofeng on 2017/1/20.
    */
    public class SpringBeanUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    SpringBeanUtil.applicationContext = applicationContext;
    }

    public static Object getBeanByName(String beanName) {
    if (applicationContext == null){
    return null;
    }
    return applicationContext.getBean(beanName);
    }

    public static <T> T getBean(Class<T> type) {
    return applicationContext.getBean(type);
    }
    }

    spring xml中配置<bean id="springBeanUtil" class="com.jd.app.server.irp.service.task.SpringBeanUtil"/>

    xml中配置很关键,因需要spring加载时感知,不配置取不到spring管理的bean。

    相关连接

    http://www.cnphp6.com/archives/135859

  • 相关阅读:
    Use the Microsoft C++ toolset from the command line
    libeay32.dll 和 ssleay32.dll 在1.1.0之后没有了
    libeay32.dll 1.0.2j crash
    VHD VHDX 区别
    char * 转 wchar *
    C++ 0xC0000094: Integer division by zero
    爬取人人网
    伪造请求头向url传递参数爬取百度默认翻译
    爬取百度贴吧
    urllib爬取实例
  • 原文地址:https://www.cnblogs.com/freedommovie/p/6323259.html
Copyright © 2011-2022 走看看