java - Spring 实用程序 :map injection

标签 java spring spring-mvc

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

我的.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();
    }
}

Bean类:

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

最佳答案

所以问题只是应用程序上下文参数不正确 类路径*:**/resources/application-context.xml 我必须将其替换为 application-context.xml 并且此 xml 文件应位于此处:

/项目目录
|_/src
|_/main
|_/资源
|_application-context.xml

关于java - Spring 实用程序 :map injection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29281870/

相关文章:

java - NoSuchBeanDefinitionException : No qualifying bean of type 'javax.servlet.Filter' available

JAVA_HOME 设置为无效目录 :

java - 在 Spring boot 中动态配置 DataSourceBuilder url

java - 创建EntityManager以及持久化

java - 将 webjar 作为根资源提供服务

java - 关于子串和递归的问题

spring - 提供静态内容会导致请求方法 'GET' 不受支持

mysql - hibernate中带注释的查询

spring-mvc - Spring mvc <表单:input> tags with some values passed from the calling controller in modelAttribute

java - 如何使用 Java 和 EPSON TM-T88III 打印凭证