• 普通javaBean获取Spring托管对象


    最近在做项目中的Socket通信的时候,同时要获取数据里的数据,但是在Socket的线程却没法通过注解的方式获取(一些thread类或servlet不能通过spring注解的方式调用spring容器里面的类),我解决的方法是通过实现ApplicationContextAware接口

    public class SpringUtil implements ApplicationContextAware{
    
    	private static ApplicationContext applicationContext;
    
    	public static ApplicationContext getApplicationContext() {
    		return applicationContext;
    	}
    
    	public  void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
    		SpringUtil.applicationContext = applicationContext;
    	}
    	
    	
    }
    

     Spring配置如下:

    <bean id="applicationContext" class="com.ncjk.utcs.common.servlet.SpringUtil">
    </bean>
    

     通过下面的方法就能获取到Spring里的类了

    SignalControlerService signalControlerService = (SignalControlerService) SpringUtil.getApplicationContext().getBean("signalControlerService",SignalControlerService.class);或者SignalControlerService signalControlerService = (SignalControlerService) SpringUtil.getApplicationContext().
    getBean("signalControlerService");

     SignalControlerService类使用注解

    @Service("signalControlerService")
    public class SignalControlerService implements ISignalControlerService {
    
        @Resource
        private ICommonDAO commonDAO;
        /*省略后续代码*/    
        
    }
    
  • 相关阅读:
    Redis 查看、删除keys
    gitlab 备份和恢复
    gitlab的搭建
    certbot 域名续期及证书查看
    晴天(周杰伦)
    SSH Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
    jenkins miaration section 1
    jenkins 忘记管理员密码
    Yangk's-树状数组 模板
    codeforces-977F-Consecutive Subsequence【动态规划】
  • 原文地址:https://www.cnblogs.com/flycoding/p/5981129.html
Copyright © 2020-2023  润新知