码迷,mamicode.com
首页 > 编程语言 > 详细

初始化Bean到Spring

时间:2018-05-27 20:23:11      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:ica   simple   one   int   bean   err   ini   catch   for   

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

标签:ica   simple   one   int   bean   err   ini   catch   for   

原文地址:https://www.cnblogs.com/fjinlong/p/9097023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!