spring 常识及不受spring管理的插件里面获取bean

@Autowired
@Qualifier("gdServicePr")
完成di

在main方法中一定是空的。即使有web.xml有配置,有jar包
如需测试用@test集成测试

///spring 第三方插件的工具类,可以直接在不受spring管理的插件里面获取bean、

package com.certusnet.nfv.mano;

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

public class ApplicationContextHolder implements ApplicationContextAware
{
    private static ApplicationContext context;

    public void setApplicationContext(ApplicationContext contex) throws BeansException
    {
        ApplicationContextHolder.context = contex;
    }

    public static ApplicationContext getApplicationContext()
    {
        return context;
    }
   
    /**
    public static Object getBean(String name)
    {
        return context.getBean(name);
    }
    */
   
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name)
    {
        return (T)context.getBean(name);
    }
   
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz)
    {
        return (T)context.getBeansOfType(clazz);
    }
}
///////////////////////////////////////////////////////////////////

应用:


IOperationLogLS operationLogLS=ApplicationContextHolder.getBean("operationLogLS");

猜你喜欢

转载自yuhuiblog6338999322098842.iteye.com/blog/2203096
今日推荐