关于ApplicationContextAware 的问题

来源:互联网 发布:怎么用模拟器登录淘宝 编辑:程序博客网 时间:2024/05/20 07:52

加载Spring配置文件时,如果Spring配置文件中所定义的Bean类实现了ApplicationContextAware 接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的

class xxxx  implements ApplicationContextAware{

private static ApplicationContext applicationContext;
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        this.applicationContext = applicationContext;
    }

}

    <bean id="xxxxxx" class="xxxxxxx"></bean>

方法,获得ApplicationContext对象


public static Object getBean(String beanId) {
        return applicationContext.getBean(beanId);
    }

在spring 配置文件里面要配置要用getBean获取的id

<bean id="commentService"
        class="com.excellence.exoa.cms.service.impl.CommentServiceImpl">
    </bean>

0 0
原创粉丝点击