Spring util: инъекция карты

1

Я создал приложение, подобное этому, но я получаю java.lang.reflect.InvocationTargetException, вызванное org.springframework.beans.factory.NoSuchBeanDefinitionException.

Мой.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
  • 0
    Можете ли вы опубликовать полную трассировку стека?
  • 0
    Этот classpath*:**/resources/application-context.xml вероятно, не делает то, что вы думаете, что он делает. Где находится ваш контекстный файл?
Показать ещё 4 комментария
Теги:
spring
spring-mvc

1 ответ

0

Таким образом, проблема была просто неправильным параметром Application-Context classpath*:**/resources/application-context.xml который мне пришлось заменить с помощью application-context.xml и этот XML файл должен быть расположен здесь:

/Проект-реж
| _/src
| _/main
| _/resources
| _application-context.xml

Ещё вопросы

Сообщество Overcoder
Наверх
Меню