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
Post a Comment