在网上找到了很多方法,都不对。
只能看源码,如下代码,注入了自己的实例对象。
public static void registerBean(String name, Object instance, ApplicationContext context) {
if(context instanceof ConfigurableApplicationContext) {
ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext)context;
name = instance.getClass().getName() + "-" + name;
if (applicationContext.getBeanFactory().getSingleton(name) == null) {
applicationContext.getBeanFactory().registerSingleton(name, instance);
}
}
}