编程式配Spring bean

    今晚翻看了以前写的rpc框架。发现这个框架中编程式配置spring bean的技巧还是比较通用的,其他的一些框架或基础服务可能也会用到。记得当时也是从struts2的代码里找到的,在此先做下记录,以供以后参考。
public class rpcmethodhelper implements applicationcontextaware {	protected static final logger logger = loggerfactory.getlogger(rpcmethodhelper.class);	protected applicationcontext appcontext;	protected autowirecapablebeanfactory autowiringfactory;	protected int autowirestrategy = autowirecapablebeanfactory.autowire_by_name;	/**	 * 初始化action如果已经在spring中注册过则从spring容器中得到action实例, 如果spring容器中没有注册则先注册并返回实例	 * 返回的action实例已经注入了依赖的其他对象及请求参数	 * 	 * @param <t>	 * @param clazz	 * @return	 */	protected <t> t createaction(class<t> clazz) {		t action = null;		string beanname = clazz.getname();		try {			logger.debug("trying the application context... appcontext = {},\n bean name = {}" ,appcontext,beanname);			action = clazz.cast(appcontext.getbean(beanname));//先从spring容器中得到bean		} catch (nosuchbeandefinitionexception e) {			//如果spring容器中没有注册则先注册并返回实例			logger.debug("did not find bean definition for bean named {}, creating a new one...",beanname);			if (autowiringfactory instanceof beandefinitionregistry) {				beandefinitionregistry registry = (beandefinitionregistry) autowiringfactory;				rootbeandefinition def = new rootbeandefinition(clazz,						autowirestrategy);//设置autowire策略,根据具体应用来决定				// def.setsingleton(false);				def.setscope("prototype");//这里的scope类型可以根据需要设置				logger.debug("registering a new bean definition for class "						+ beanname);				registry.registerbeandefinition(beanname, def);				try {										action = clazz.cast(appcontext.getbean(beanname));				} catch (nosuchbeandefinitionexception e2) {					logger.warn("could not register new bean definition for bean {}"									,beanname);				}			}		}		return action;	}	public void setapplicationcontext(applicationcontext context)			throws beansexception {		appcontext = context;		autowiringfactory = findautowiringbeanfactory(this.appcontext);	}	/**	 * 	 * @see com.opensymphony.xwork2.spring.springobjectfactory#findautowiringbeanfactory	 * @param context	 */	protected autowirecapablebeanfactory findautowiringbeanfactory(			applicationcontext context) {		if (context instanceof autowirecapablebeanfactory) {			// check the context			return (autowirecapablebeanfactory) context;		} else if (context instanceof configurableapplicationcontext) {			// try and grab the beanfactory			return ((configurableapplicationcontext) context).getbeanfactory();		} else if (context.getparent() != null) {			// and if all else fails, try again with the parent context			return findautowiringbeanfactory(context.getparent());		}		return null;	}	/**	 * sets the autowiring strategy	 * 	 * @see com.opensymphony.xwork2.spring.springobjectfactory#setautowirestrategy	 * @param autowirestrategy	 */	public void setautowirestrategy(int autowirestrategy) {		switch (autowirestrategy) {		case autowirecapablebeanfactory.autowire_autodetect:			logger.info("setting autowire strategy to autodetect");			this.autowirestrategy = autowirestrategy;			break;		case autowirecapablebeanfactory.autowire_by_name:			logger.info("setting autowire strategy to name");			this.autowirestrategy = autowirestrategy;			break;		case autowirecapablebeanfactory.autowire_by_type:			logger.info("setting autowire strategy to type");			this.autowirestrategy = autowirestrategy;			break;		case autowirecapablebeanfactory.autowire_constructor:			logger.info("setting autowire strategy to constructor");			this.autowirestrategy = autowirestrategy;			break;		default:			throw new illegalstateexception("invalid autowire type set");		}	}}

使用方式:
1.上面的rpcmethodhelper本身也应该配置到spring容器中
<bean id="rpcmethodhelper" class="com.niagara.rpc.rpcmethodhelper"></bean>

2.如果不想将上面的类配置为spring bean,那只要在工具类中得到applicationcontext的引用即可。
 

猜你喜欢

转载自tomfish88.iteye.com/blog/1144546