Spring bean生命周期
- - 开源软件 - ITeye博客Spring bean生命周期. 在传统的Java应用中,Bean的生命周期非常简单. Java的关键词new用来实例化Bean(或许他是非序列化的). 相反,Bean的生命周期在Spring容器中更加细致. 理解Spring Bean的生命周期非常重要,因为你或许要利用Spring提供的机会来订制Bean的创建过程.
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * SpringBean的工具类<br> * 非注入方式取得spring注入bean的util类实现 * @author langkai * */ public final class SpringBeanUtil implements ApplicationContextAware { private static ApplicationContext ctx; /** * 通过spring配置文件中配置的bean id取得bean对象 * @param id spring bean ID值 * @return spring bean对象 */ public static Object getBean(String id) { if (ctx == null) { throw new NullPointerException("ApplicationContext is null"); } return ctx.getBean(id); } @Override public void setApplicationContext(ApplicationContext applicationcontext) throws BeansException { ctx = applicationcontext; } }
<bean class="*.SpringBeanUtil"/>
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("springConfig_bus.xml"); IMsgBusService client = (IMsgBusService) context.getBean("client");