java - ApplicationContext beanfactory org.springframework.beans.factory.NoSuchBeanDefinitionException: -


 useraccountserviceimpl service = applicationcontext.getbean("useraccountserviceimpl", useraccountserviceimpl.class);  service =springcontextlistener.getbean("useraccountserviceimpl", useraccountserviceimpl.class); 

my code above. invoke getbean in 2 ways.the first way ran perfact,but second 1 throw exception

org.springframework.beans.factory.nosuchbeandefinitionexception: no bean named 'useraccountserviceimpl' defined

applictioncontext got implements applicationcontextaware springcontextlistener below:

 <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));       }   } 

what difference between applicationcontext , beanfactory?what code's problem ?

i have added useraccountserviceimpl bean in application-context.xml

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); 

result:

useraccountserviceimpl [tostring()=com.vl.test.useraccountserviceimpl@3834d63f] useraccountserviceimpl [tostring()=com.vl.test.useraccountserviceimpl@3834d63f] 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -