package org.coderecord.ccms.web.action.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* Spring IOC上下文工具类
*
* @author Ryan Shaw
*
*/
public class SpringUtil implements ApplicationContextAware {
/**
* 当前IOC
*/
private static ApplicationContext applicationContext;
/**
* 设置当前上下文环境,此方法由spring自动装配
*/
@Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
applicationContext = arg0;
}
/**
* 从当前IOC获取bean
*
* @param id
* bean的id
* @return
*/
public static Object getObject(String id) {
Object object = null;
object = applicationContext.getBean(id);
return object;
}
}
上文的类就是我们要用的,而其中的setApplicationContext是接口中需要实现的,Spring会自动进行填充。我们在Spring的配置文件中注册一
这样就可以了,Spring把我们需要的东西给我们了
1 <bean id="springUtil" class="org.coderecord.ccms.web.action.util.SpringUtil" />
1 YouClass obj = (YouClass)SpringUtil.getObject("beanid");