1.在初始化时保存ApplicationContext对象
2.通过Spring提供的utils类获取ApplicationContext对象
3.继承自抽象类ApplicationObjectSupport
4.继承自抽象类WebApplicationObjectSupport
5.实现接口ApplicationContextAware
6.通过Spring提供的ContextLoader
来说一下实现接口的方法:
@Component
public class example implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
example(){
}
public static TypeHandler createHandler(Type type) throws Exception{
if(type==null){
throw new Exception("handler type is null");
}
return (TypeHandler) context.getBean(type.getShortName()+"Handler");
}
}
实现接口之后,getBrean时要实现类型转换