初始化Bean到Spring
Posted 专注于技术的小石头
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初始化Bean到Spring相关的知识,希望对你有一定的参考价值。
public static<T> T getBean(Class<T> clazz) { T t = null; try { //throw new NoSuchBeanDefinitionException(""); t = getApplicationContext().getBean(clazz); } catch (NoSuchBeanDefinitionException e) { if(clazz.isInterface()){ HSFSpringConsumerBean consumer=new HSFSpringConsumerBean(); consumer.setInterfaceName(clazz.getName()); consumer.setVersion("1.0.0"); consumer.setGroup("light"); try { consumer.init(); t = (T) consumer.getObject(); } catch (Exception e1) { throw new CommonException(String.format("error to consume the service %s.",clazz.getName()),e); } }else{ String nameString =clazz.getSimpleName(); nameString = (nameString.substring(0, 1)).toLowerCase()+nameString.substring(1); if(SpringContextUtil.registry!=null){ boolean containBean = SpringContextUtil.containsBean(nameString); if (!containBean) { registry.registerBeanDefinition(nameString, SpringContextUtil.getDefinition(clazz)); } Object o = getApplicationContext().getBean(nameString); t = (T) o; } } } return t; }
以上是关于初始化Bean到Spring的主要内容,如果未能解决你的问题,请参考以下文章