]]>Spring和W(xu)ebworkl合的方?/title>http://www.aygfsteel.com/lilan1983/archive/2005/11/23/21119.html李岚李岚Wed, 23 Nov 2005 05:52:00 GMThttp://www.aygfsteel.com/lilan1983/archive/2005/11/23/21119.htmlhttp://www.aygfsteel.com/lilan1983/comments/21119.htmlhttp://www.aygfsteel.com/lilan1983/archive/2005/11/23/21119.html#Feedback0http://www.aygfsteel.com/lilan1983/comments/commentRss/21119.htmlhttp://www.aygfsteel.com/lilan1983/services/trackbacks/21119.html前两U方式webwork的wiki里有Q就不多说了(jin),点这里可以查?/A>?BR>而以下的l出的思\是webwork mail list的讨论的一U方案,应该是最好的一U方案?BR> 代码1Q?/FONT>
publicclass SpringContainer implements Container { private ApplicationContext applicationContext; public SpringContainer(ServletContext servletContext) { this.applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext); } public SpringContainer(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } /** *//** * @param key * component class type or component name * @return@throws * ComponentNotFoundException */ public Object getComponent(Object key) throws ComponentNotFoundException { if (applicationContext ==null) thrownew IllegalStateException("Spring Application context has not been set"); if (key ==null) thrownew ComponentNotFoundException("The component key can not be null"); if (key instanceof Class) { Map beans = applicationContext.getBeansOfType((Class) key); if (beans ==null) thrownew ComponentNotFoundException("The container is unable to resolve single instance of "+ ((Class) key).getName() +", none instances found"); if (beans.size() ==0|| beans.size() >1) thrownew ComponentNotFoundException("The container is unable to resolve single instance of "+ ((Class) key).getName() +", number of instances found was: "+ beans.size()); key = beans.keySet().iterator().next(); } return applicationContext.getBean(key.toString()); } publicvoid reload() { close(); ((AbstractApplicationContext) applicationContext).refresh(); } publicvoid autowireComponent(Object bean) { ((AbstractApplicationContext) applicationContext).getBeanFactory().autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false); } publicvoid close() { ((AbstractApplicationContext) applicationContext).close(); } }