Неверное определение bean-компонента: не удалось разрешить заполнитель - PullRequest
0 голосов
/ 24 февраля 2019

Я использую springframework в первый раз.Поэтому написал небольшую программу и протестировал значение переменной Instance, сгенерированной в IoC.Но я получаю сообщение об ошибке ниже:

Feb 24, 2019 10:40:13 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'loadingObject' defined in class path resource [Spring-Config.xml]: Could not resolve placeholder 'log4j.configuration' in value "${log4j.configuration}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'log4j.configuration' in value "${log4j.configuration}"
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'loadingObject' defined in class path resource [Spring-Config.xml]: Could not resolve placeholder 'log4j.configuration' in value "${log4j.configuration}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'log4j.configuration' in value "${log4j.configuration}"
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:228)
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:213)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:86)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:286)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:166)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:691)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:528)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
    at com.jcg.spring.log4j.Mainclass.main(Mainclass.java:12)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'log4j.configuration' in value "${log4j.configuration}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:232)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:296)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:217)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:147)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:85)
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:225)
    ... 9 more

Я поместил мой application.properties файл и Spring-Config.xml файл метаданных в это местоположение ...\src\main\resources

application.properties

log4j.configuration=C:\Softwares\ConfigFiles\log4j.properties

Spring-Config.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <bean id="propertiesToBeTaken" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:application.properties</value>
                <value>classpath*:*keys.properties</value>
            </list>
        </property>
    </bean>

    <bean id = "loadingObject" class = "com.jcg.spring.log4j.TestController">
        <property name="log4jConfig" value="${log4j.configuration}" />
    </bean> 

</beans>

Фрагмент кода

public class TestController  {

    public String log4jConfig;

    public void setlog4j(String log4jConfig){

        this.log4jConfig = log4jConfig;

    }

    public  String getlog4j(){

        return this.log4jConfig;
    }

}

public class Mainclass {

    public static void main(String[] args){


        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Config.xml");
        TestController obj = (TestController) context.getBean("TestController");
        System.out.println(obj.log4jCongif);



    }

}

Кажется, все в порядке, но не уверен, почему возникает эта ошибка.Застрял в этом на некоторое время.Может кто-нибудь, пожалуйста, посмотрите?Чего мне не хватает?

Спасибо

Ответы [ 2 ]

0 голосов
/ 24 февраля 2019

Кажется, контейнер Spring пытается создать экземпляр компонента TestController перед PropertyPlaceholderConfigurer, поэтому свойство не разрешается, поэтому возникает ошибка.

Вы можете попробовать поместить <property name="ignoreUnresolvablePlaceholders" value="true"/> в Spring-Config.xml.сказать весне игнорировать неразрешенные свойства.Как только PropertyPlaceholderConfigurer будет создан, возможно, свойство будет разрешено.

Попробуйте это

<bean id="propertiesToBeTaken" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:application.properties</value>
            <value>classpath:keys.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>

Плюс еще несколько изменений:

  • TestController obj = (TestController) context.getBean("loadingObject");
  • Имя метода установки: setLog4jConfig
  • Имя метода получения: getLog4jConfig
0 голосов
/ 24 февраля 2019

Попробуйте заменить bean-компонент PropertyPlaceHolder следующим текстом:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

        <property name="location">
            <value>application.properties</value>
        </property>
    </bean>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...