Spring util:地图注入

时间:2015-03-26 15:03:33

标签: java spring spring-mvc

我创建了一个类似于this的应用程序,但是我得到了一个由org.springframework.beans.factory.NoSuchBeanDefinitionException引起的java.lang.reflect.InvocationTargetException。

我的.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>

我的java应用程序:

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

1 个答案:

答案 0 :(得分:0)

所以问题只是不正确的Application-Context参数 classpath*:**/resources/application-context.xml 我必须用application-context.xml替换,这个xml文件应位于此处:

/项目-DIR
| _ / src
| _ /主
| _ /资源
| _Application-context.xml中