UserAccountServiceImpl service = applicationContext.getBean("UserAccountServiceImpl", UserAccountServiceImpl.class);
service =SpringContextListener.getBean("UserAccountServiceImpl", UserAccountServiceImpl.class);
我的代碼就像上面那樣。 我援引的getBean兩個ways.The第一種方式perfact跑,但第二個拋出這樣ApplicationContext beanfactory org.springframework.beans.factory.NoSuchBeanDefinitionException:
org.springframework.beans.factory.NoSuchBeanDefinitionException異常:無豆命名爲「UserAccountServiceImpl」被定義
applictionContext從implements ApplicationContextAware
SpringContextListener得到的是象下面這樣:
<bean id="springContextListener" class="com.xxxx.xxx.SpringContextListener"/>
public final class SpringContextListener implements BeanFactoryAware {
private static BeanFactory beanFactory;
public static BeanFactory getBeanFactory() {
return beanFactory;
}
public void setBeanFactory(BeanFactory beanFactory) {
if(SpringContextListener.beanFactory != null) {
throw new RuntimeException("beanFactory inited .............");
}
SpringContextListener.beanFactory = beanFactory;
}
public static <T> T getBean(String beanName, Class<T> clazs) {
return clazs.cast(beanFactory.getBean(beanName));
}
}
applicationContext和beanfactory有什麼區別?我的代碼的問題是什麼?
您的代碼是不完整的。你不會在任何地方調用'SpringContextListener.setBeanFactory'。沒有這個,這個問題就沒有意義了。 – skaffman
您是否在應用程序上下文中定義了UserAccountServiceImpl bean?或者它應該是'UserAccountService'? –