Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ApplicationContext beanfactory org.springframework.beans.factory.noSuchBean定义异常:_Java_Spring - Fatal编程技术网

Java ApplicationContext beanfactory org.springframework.beans.factory.noSuchBean定义异常:

Java ApplicationContext beanfactory org.springframework.beans.factory.noSuchBean定义异常:,java,spring,Java,Spring,我的代码如上所示。 我用两种方式调用getBean,第一种方式运行perfact,但第二种方式抛出如下异常 org.springframework.beans.factory.noSuchBean定义异常:未定义名为“UserAccountServiceImpl”的bean applictionContext是从实现ApplicationContextAware SpringContextListener如下所示: UserAccountServiceImpl service = applic

我的代码如上所示。 我用两种方式调用getBean,第一种方式运行perfact,但第二种方式抛出如下异常

org.springframework.beans.factory.noSuchBean定义异常:未定义名为“UserAccountServiceImpl”的bean

applictionContext是从
实现ApplicationContextAware
SpringContextListener如下所示:

 UserAccountServiceImpl service = applicationContext.getBean("UserAccountServiceImpl", UserAccountServiceImpl.class);
 service =SpringContextListener.getBean("UserAccountServiceImpl", UserAccountServiceImpl.class);


公共最终类SpringContextListener实现BeanFactoryAware{
私人静态Bean工厂Bean工厂;
公共静态BeanFactory getBeanFactory(){
还豆厂;
}
公共工厂(BeanFactory BeanFactory){
if(SpringContextListener.beanFactory!=null){
抛出新的运行时异常(“beanFactory初始化………”;
}
SpringContextListener.beanFactory=beanFactory;
}
公共静态T getBean(字符串beanName,类clazs){
return clazs.cast(beanFactory.getBean(beanName));
}  
}

applicationContext和beanfactory之间有什么区别?我的代码有什么问题?

我在application-context.xml中添加了
useraccountserviceinpl
作为bean

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 applicationContext =
    newClassPathXmlApplicationContext("application-context.xml");

UserAccountServiceImpl service = applicationContext.getBean("UserAccountServiceImpl",
    UserAccountServiceImpl.class);

System.out.println(service);

service=SpringContextListener.getBean("UserAccountServiceImpl",UserAccountServImpl.class);

System.out.println(service);

您的代码不完整。您没有在任何地方调用
SpringContextListener.setBeanFactory
。如果没有这一点,这个问题就没有意义了。您已经在应用程序上下文中定义了
useraccountserviceinpl
bean了吗?或者应该是
UserAccountService
?添加到application-context.xml中,如下所示:谢谢!你的回答对解决我的问题是正确的。但不是我想要的确切结果。最后,我知道了原因。此标记应在UserAccountServiceImpl获取beanfactory之前添加!
ApplicationContext applicationContext =
    newClassPathXmlApplicationContext("application-context.xml");

UserAccountServiceImpl service = applicationContext.getBean("UserAccountServiceImpl",
    UserAccountServiceImpl.class);

System.out.println(service);

service=SpringContextListener.getBean("UserAccountServiceImpl",UserAccountServImpl.class);

System.out.println(service);
UserAccountServiceImpl [toString()=com.vl.test.UserAccountServiceImpl@3834d63f]
UserAccountServiceImpl [toString()=com.vl.test.UserAccountServiceImpl@3834d63f]