spring项目手动获取spring容器内的bean对象

Posted 500年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring项目手动获取spring容器内的bean对象相关的知识,希望对你有一定的参考价值。

spring项目手动获取spring容器内的bean对象

SpringUtils.java


import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

@Component
public class SpringUtils implements ApplicationContextAware, EmbeddedValueResolverAware  

	private static ApplicationContext applicationContext = null;
	
	private static StringValueResolver stringValueResolver;

	public void setApplicationContext(ApplicationContext arg0) throws BeansException 

		if (applicationContext == null) 
			applicationContext = arg0;
		
	

	public static ApplicationContext getApplicationContext() 
		return applicationContext;
	

	public static void setAppCtx(ApplicationContext webAppCtx) 
		if (webAppCtx != null) 
			applicationContext = webAppCtx;
		
	

	/**
	 * 
	 * 拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象
	 * 
	 */

	public static <T> T getBean(Class<T> clazz) 
		return getApplicationContext().getBean(clazz);
	

	public static <T> T getBean(String name, Class<T> clazz) throws ClassNotFoundException 

		return getApplicationContext().getBean(name, clazz);

	

	public static final Object getBean(String beanName) 
		return getApplicationContext().getBean(beanName);
	

	public static final Object getBean(String beanName, String className) throws ClassNotFoundException 

		Class<?> clz = Class.forName(className);

		return getApplicationContext().getBean(beanName, clz.getClass());

	

	public static boolean containsBean(String name) 

		return getApplicationContext().containsBean(name);

	

	public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException 

		return getApplicationContext().isSingleton(name);

	

	public static Class<?> getType(String name) throws NoSuchBeanDefinitionException 

		return getApplicationContext().getType(name);

	

	public static String[] getAliases(String name) throws NoSuchBeanDefinitionException 

		return getApplicationContext().getAliases(name);

	
	
	/**
     * 动态获取配置文件中的值
     * @param name
     * @return
     */
    public static String getPropertiesValue(String name) 
        try 
            name = "$" + name + "";
            return stringValueResolver.resolveStringValue(name);
         catch (Exception e) 
            // 获取失败则返回null
            return null;
        
    
    
	/**
     * 动态获取配置文件中的值
     * @param name
     * @return
     */
    public static String getPropertiesDefaultValue(String name,String defaultValue) 
        try 
            name = "$" + name + ":"+defaultValue+"";
            return stringValueResolver.resolveStringValue(name);
         catch (Exception e) 
            // 获取失败则返回null
            return null;
        
    
    
    @Override
    public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) 
        SpringUtils.stringValueResolver = stringValueResolver;
    
    
    public static <T> Map<String, Object> beanToMap(T bean) 
        Map<String, Object> map = new HashMap<>();
        if (bean != null) 
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) 
                map.put(key+"", beanMap.get(key));
            
        
        return map;
    
    
    /** 
     * 将map装换为javabean对象 
     * @param map 
     * @param bean 
     * @return 
     */  
    public static <T> T mapToBean(Map<String, Object> map,T bean)   
        BeanMap beanMap = BeanMap.create(bean);  
        beanMap.putAll(map);  
        return bean;  
     


应用

AbcService abcService = SpringUtils.getBean(AbcService.class);
					
abcService.uploadLocalFile(file, ef.getFileName(), md5);

补充说明

本方法的使用前提,必须保证spring容器已加载完毕。

以上是关于spring项目手动获取spring容器内的bean对象的主要内容,如果未能解决你的问题,请参考以下文章

获取 Spring 应用程序上下文

手动向Spring容器中注入对象

2Spring之核心容器

Spring IoC 容器的扩展

Spring IoC 容器的扩展

如何解决spring aop无法代理手动new的对象问题