I created an App similar to this , but I get a java.lang.reflect.InvocationTargetException caused by org.springframework.beans.factory.NoSuchBeanDefinitionException.
My .xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<context:component-scan base-package="my.package" />
<util:map id="multiWordTermModels">
<entry key="termList_wiki" value="resources/multiWordTerms.lst"/>
</util:map>
</beans>
And my java apps:
package my.package;
public class ModelLoader {
private static Map<String, String> multiWordTermModels;
public static void loadModels() throws IOException, ClassNotFoundException{
ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath*:**/resources/application-context.xml");
Beans beans = appContext.getBean(Beans.class);
multiWordTermModels = beans.getMultiWordTermModel();
}
}
Beans class:
package my.package;
@Service
public class Beans {
@Resource(name="multiWordTermModels")
private Map<String, String> multiWordTermModels;
public Map<String, String> getMultiWordTermModel(){
return multiWordTermModels;
}
}
It looks the same as in the example so I don't know what's the problem.
Edit: complete stacktrace:
16:36:13.087 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 0 bean definitions from location pattern [classpath*:**/resources/application-context.xml
]
16:36:13.087 [main] DEBUG o.s.c.s.ClassPathXmlApplicationContext - Bean factory for org.springframework.context.support.ClassPathXmlApplicationContext@3c630382:
org.springframework.beans.factory.support.DefaultListableBeanFactory@38d5fb42: defining beans []; root of factory hierarchy
16:36:13.134 [main] DEBUG o.s.c.s.ClassPathXmlApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.
context.support.DelegatingMessageSource@20fd3abe]
16:36:13.134 [main] DEBUG o.s.c.s.ClassPathXmlApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using d
efault [org.springframework.context.event.SimpleApplicationEventMulticaster@6088fdaa]
16:36:13.134 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFa
ctory@38d5fb42: defining beans []; root of factory hierarchy
16:36:13.134 [main] DEBUG o.s.c.s.ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.spring
framework.context.support.DefaultLifecycleProcessor@5156668a]
16:36:13.134 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
16:36:13.150 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemProperties]
16:36:13.150 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemEnvironment]
16:36:13.150 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source. Returning [nul
l]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner
.java:53)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No q
ualifying bean of type [my.package.Beans] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.
getBean(DefaultListableBeanFactory.java:371)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.
getBean(DefaultListableBeanFactory.java:331)
at org.springframework.context.support.AbstractApplicationContext.getBea
n(AbstractApplicationContext.java:968)
at my.package.ModelLoader.loadModels(ModelLoader.java:32)
at my.package.SqeRestApiApplication.main(SqeRestApiApplication.
java:15)
... 6 more
So the problem was just the incorrect Application-Context parameter classpath*:**/resources/application-context.xml
which I had to replace with application-context.xml
and this xml-file should be located here:
/project-dir
|_ /src
|_ /main
|_ /resources
|_application-context.xml
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.