Java springutil:map注入
我创建了一个类似于的应用程序,但是我得到了一个由org.springframework.beans.factory.NoSuchBeanDefinitionException引起的java.lang.reflect.InvocationTargetException My.xml:Java springutil:map注入,java,spring,spring-mvc,Java,Spring,Spring Mvc,我创建了一个类似于的应用程序,但是我得到了一个由org.springframework.beans.factory.NoSuchBeanDefinitionException引起的java.lang.reflect.InvocationTargetException My.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"
<?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>
所以问题只是应用程序上下文参数不正确
classpath*:**/resources/application context.xml
我必须将其替换为application context.xml
,此xml文件应位于此处:
/项目目录|_/src
|_/main
|_/resources
|_application context.xml您能发布完整的堆栈跟踪吗?此
类路径*:**/resources/application context.xml
可能与您认为的不一样。您的上下文文件位于哪里?Sotirios:它位于我的项目文件夹/resources/application context.xml
。。。(“我的项目文件夹”也包含例如“src”和“target”-文件夹)您希望该文件最终位于类路径中。我不知道你是如何构建你的应用程序的,但它似乎并不存在。另外,堆栈跟踪还应该包括消息,该消息指定缺少哪个bean。好的,我发现我的上下文文件位置有问题(请参见编辑),但是我应该将我的.xml放在哪里?如果我将其放入“我的项目文件夹”并将classpath*:**/resources/application context.xml
更改为application context.xml
,那么我会得到一个FileNotFoundException-但我在命令行内的“我的项目文件夹”目录中?!
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();
}
}
package my.package;
@Service
public class Beans {
@Resource(name="multiWordTermModels")
private Map<String, String> multiWordTermModels;
public Map<String, String> getMultiWordTermModel(){
return multiWordTermModels;
}
}
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