1.新建类,并实现 org.springframework.context.ApplicationContextAware 接口.
package com.abc.framework.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * 在容器启动后,也可以通过 getBean(String name) 得到对象 * @author Administrator * <!-- 需要在spring.xml 里写 --> * <bean class="com.abc.framework.util.ApplicationContextHandle" lazy-init="false"/> */ public class ApplicationContextHandle implements ApplicationContextAware{ private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { ApplicationContextHandle.applicationContext = applicationContext; } /** * 获取对象 * 这里重写了bean方法,起主要作用 * @param name * @return Object 一个以所给名字注册的bean的实例 * @throws BeansException */ public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } }
2.在spring.xml内添加:
<bean class="com.abc.framework.util.ApplicationContextHandle" lazy-init="false"/>
3.应用:
private static final AttachmentService as = (AttachmentService)ApplicationContextHandle.getBean("attachmentService");
时间: 2024-10-12 11:02:50