package com.zdc;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class BeanGetContain implements ApplicationContextAware {
private ApplicationContext ctx;
private String msg;
public void getSource() {
Dog1 dog1 = (Dog1) ctx.getBean("dog1");
dog1.getname();
System.out.println(msg);
}
public void setCtx(ApplicationContext ctx) {
this.ctx = ctx;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
// TODO Auto-generated method stub
ctx = arg0;
}
}
1、首先该类要实现ApplicationContextAware 的接口
2、重写setApplicationContext方法;
3、定义ApplicationContext 对象;并添加set方法
4、配置bean:
5、在初始化该xml的时候我们会发现
会去加载该set方法,将arg0参数及spring容器赋值给ctx.