स्ट्रिंग मान में प्लेसहोल्डर को हल नहीं किया जा सका


86

मैं एक .propertiesफ़ाइल से गुणों का उपयोग करने की कोशिश कर रहा हूं , लेकिन यह काम नहीं करता है।

यहाँ मेरा कोड है:

@Service("ServiceFTP")
@Transactional
public class ServiceFTPImpl implements ServiceFTP {

@Value("${project.ftp.adresse}")
private String adresse;

@Value("${project.ftp.login}")
private String compte;

@Value("${project.ftp.password}")
private String motDePasse;

@Value("${project.ftp.root}")
private String ROOT;

[...]

}

यह वर्ग @Valueगुण प्राप्त करने के लिए एनोटेशन का उपयोग करता है । इसे स्प्रिंग सेवा के रूप में भी घोषित किया गया है और यह मेरी infraContext.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.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:property-placeholder location="classpath:context-core.properties"/>

[...]

</beans>

का उपयोग करते हुए context:property-placeholder, मैं इस फ़ाइल को अपनी context-core.propertiesफ़ाइल से लिंक करता हूं :

project.ftp.adresse = localhost
project.ftp.login = anonymous
project.ftp.password =
project.ftp.root = /anonymous/

यह समझ में आता है, है ना?

लेकिन जब मैं अपना प्रोजेक्ट लॉन्च करने की कोशिश करता हूं, तो टॉमकैट इस अपवाद को फेंक देता है:

    ERROR [context.ContextLoader.initWebApplicationContext()] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ServiceFTP': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String project.sins.service.impl.ServiceFTPImpl.adresse; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'project.ftp.adresse' in string value "${project.ftp.adresse}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:388)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1636)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String project.sins.service.impl.ServiceFTPImpl.adresse; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'project.ftp.adresse' in string value "${project.ftp.adresse}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    ... 27 more
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'project.ftp.adresse' in string value "${project.ftp.adresse}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:173)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:125)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:151)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:142)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:169)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:748)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:740)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)
    ... 29 more

या, संक्षेप में: java.lang.IllegalArgumentException: Could not resolve placeholder 'project.ftp.adresse' in string value "${project.ftp.adresse}"

संपादित करें:

यहाँ मेरा web.xml है:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="sins" version="2.5">

    <display-name>Project</display-name>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <context-param>
        <param-name>log4jExposeWebAppRoot</param-name>
        <param-value>false</param-value>
    </context-param>

    <filter>
        <filter-name>ExpiresFilter</filter-name>
        <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
        <init-param>
            <param-name>ExpiresByType text/html</param-name>
            <param-value>now plus 0 seconds</param-value>
        </init-param>
        <init-param>
            <param-name>ExpiresByType application/json</param-name>
            <param-value>now plus 0 seconds</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>ExpiresFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <filter-class>
            org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener>

    <context-param>
        <param-name>
            org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG
        </param-name>
        <param-value>
            /WEB-INF/tiles/user.xml
        </param-value>
    </context-param>

    <resource-ref>
        <res-ref-name>jdbc/si_nsg</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>

</web-app>

मेरा infraContext.xml एक और .xml फ़ाइल में आयात किया गया है जिसका नाम ApplicationContext.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:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">

    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="environment">
            <bean class="org.springframework.web.context.support.StandardServletEnvironment"/>
        </property>
    </bean>

    <import resource="classpath:securityContext.xml"/>

    [...]
    <import resource="classpath:project/sins/persistenceContext.xml"/>

    <import resource="classpath:project/sins/infraContext.xml"/>

</beans>

मैं स्पष्ट रूप से कुछ याद कर रहा हूं, लेकिन मैं यह पता नहीं लगा सकता कि क्या।

कृपया मुझे बताएं कि क्या आपको अधिक जानकारी की आवश्यकता है, क्योंकि यह मेरा पहला सवाल है, मैं जल्द से जल्द जवाब देने की कोशिश करूंगा :)।


1
आपका infraContext.xmlलोड कहाँ हो रहा है? स्टैकट्रेस से देखते हुए यह द्वारा लोड किए गए कॉन्फ़िगरेशन में शामिल नहीं है ContextLoaderListener। कृपया अपना web.xml पोस्ट करें।
एम। डीनुम

2
समस्या यह है कि आपके पास PropertySourcesPlaceholderConfigurerएक स्पष्ट रूप से परिभाषित (क्यों?) और एक नामस्थान के कारण कई उदाहरण हैं । आप में applicationContext.xmlसे कोई भी कुछ नहीं कर रहा है और न ही कुछ जोड़ रहा है। इसे हटा दो।
एम। डाइनम

जवाबों:


73

आपके कॉन्फ़िगरेशन में आपके पास 2 PropertySourcesPlaceholderConfigurerइंस्टेंस हैं।

applicationContext.xml

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="environment">
        <bean class="org.springframework.web.context.support.StandardServletEnvironment"/>
    </property>
</bean>

infraContext.xml

<context:property-placeholder location="classpath:context-core.properties"/>

डिफ़ॉल्ट रूप से एक PlaceholderConfigurerविफल-तेजी से हो रहा है, इसलिए यदि एक प्लेसहोल्डर को हल नहीं किया जा सकता है तो यह अपवाद फेंक देगा। applicationContext.xmlफ़ाइल के उदाहरण में कोई गुण नहीं है और जैसा कि सभी प्लेसहोल्डर्स पर विफल होगा।

समाधान: से निकालें applicationContext.xml यह कुछ भी यह केवल बातें टूट जाता है नहीं जोड़ता है के रूप में।


बहुत बढ़िया, धन्यवाद, इसलिए मैं इसे वसंत में ले जाता हूं कि केवल एक ही उदाहरण हो सकता है PlaceholderConfigurer?
मुश्ताक जमील

11
यदि कोई प्लेसहोल्डर नहीं मिला है तो आप डिफ़ॉल्ट रूप से तेजी से विफल हो सकते हैं। आप सेट करके ऐसा निष्क्रिय कर सकते हैं ignore-unresolved-placeholdersकरने के लिए true
एम। डाइनम

3
मैं एनोटेशन के साथ स्प्रिंग बूट में इसे कैसे संभाल सकता हूं?
rezKesh

5

मुझे भी यही समस्या थी, इसे जोड़कर हल किया

<filtering>true</filtering> 

pom.xml में:

पहले (काम नहीं किया):

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>               
        </resource>
    </resources>
</build>

के बाद (यह काम किया):

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

उसके बाद आप सिर्फ mvan क्लीन इंस्टॉल और एप्लिकेशन को चलाएं ।


धन्यवाद, इससे मेरी समस्या हल हो गई। मैं उत्सुक हूं कि क्यों, क्यों / कैसे मावेन रिसोर्स प्लगइन वसंत प्लेसहोल्डर्स को प्रभावित कर रहा है ... मैंने कभी अनुमान नहीं लगाया कि वे संबंधित थे!
पिम हजेब्रोक

3

यह समस्या तब होती है जब अनुप्रयोग कुछ_file_name.properties फ़ाइल तक पहुँचने में असमर्थ है। सुनिश्चित करें कि गुण फ़ाइल वसंत में संसाधन फ़ोल्डर में रखी गई है।

शूटिंग स्टेप्स की परेशानी

1: संसाधन फ़ोल्डर के अंतर्गत गुण फ़ाइल जोड़ें।

2: यदि आपके पास संसाधन फ़ोल्डर नहीं है। प्रोजेक्ट नया> स्रोत फ़ोल्डर पर राइट क्लिक करके नया बनाकर एक बनाएँ, इसे संसाधन के रूप में नाम दें और इसके अंतर्गत अपनी गुण फ़ाइल रखें।

एनोटेशन आधारित कार्यान्वयन के लिए

@PropertySource(ignoreResourceNotFound = true, value = "classpath:some_file_name.properties")जगह धारक का उपयोग करने से पहले इसे जोड़ें / जोड़ें

उदाहरण:

Assignment1Controller.Java

@PropertySource(ignoreResourceNotFound = true, value = "classpath:assignment1.properties")
@RestController  
public class Assignment1Controller {

//  @Autowired
//  Assignment1Services assignment1Services;
    @Value("${app.title}")
    private String appTitle;
     @RequestMapping(value = "/hello")  
        public String getValues() {

          return appTitle;

        }  

}

assignment1.properties

app.title=Learning Spring

1
क्या यह स्प्रिंग 4 और बाद में लागू होता है?
मल्कोकोग्लू

हां @ मल्कोकोग्लू
गनी

3

मेरे मामले में, मैं application.yml फ़ाइल को मर्ज करते समय लापरवाह था, और मैंने अपने गुणों को सही करने के लिए अनावश्यक रूप से प्रेरित किया है।

मैंने इसे इस तरह प्रेरित किया है:

spring:
    application:
       name: applicationName
............................
    myProperties:
       property1: property1value

जबकि कोड यह उम्मीद करता था कि यह इस तरह होगा:

spring:
    application:
        name: applicationName
.............................
myProperties:
    property1: property1value

3

आप डिफ़ॉल्ट मान भी आज़मा सकते हैं। वसंत-मान-एनोटेशन

डिफ़ॉल्ट मान उन गुणों के लिए प्रदान किए जा सकते हैं जिन्हें परिभाषित नहीं किया जा सकता है। इस उदाहरण में "कुछ डिफ़ॉल्ट" मान इंजेक्ट किया जाएगा:

@Value("${unknown.param:some default}")
private String someDefault;

यदि समान संपत्ति को सिस्टम गुण के रूप में और गुण फ़ाइल में परिभाषित किया गया है, तो सिस्टम गुण लागू किया जाएगा।


ठीक है ... इससे मुझे मदद मिली। वास्तव में मेरे ऐप में कई एप्लिकेशन हैं- <प्रोफाइल> .yml फाइलें, और मुख्य कॉन्फ़िगरेशन में परिभाषित एक नई संपत्ति के कारण मुझे एक त्रुटि प्राप्त हुई थी। केवल फ़ाइल। ईमानदारी से मैं मुख्य कॉन्फ़िगरेशन में केवल एक बार मान को परिभाषित करने की उम्मीद कर रहा था, और यदि मामला किसी प्रोफ़ाइल-विशिष्ट कॉन्फ़िगरेशन में मान को ओवरराइड करता है। ऐसा लगता है कि यह उस तरह काम नहीं करता है, और एक डिफ़ॉल्ट मूल्य हमेशा प्रदान किया जाना चाहिए। अब मुझे समझ में आया कि मेरे जावा कॉन्फ़िगरेशन क्यों विफल हो रहे थे, जब मैंने डिफ़ॉल्ट मान के साथ गुणों को इनिशियलाइज़ नहीं किया था! क्या यह व्यवहार बदला जा सकता है? धन्यवाद :-)
funder7

2

मुझे मेरी माइक्रो-सेवा में वही त्रुटि मिली, जब भी आप प्रोग्राम में @Value एनोटेशन घोषित करते हैं, @Value ("$ {project.api.key}")

सुनिश्चित करें कि आपके एप्लिकेशन.प्रोपरेटी फाइल एक ही मान के साथ रिक्त प्रोजेक्ट नहीं होनी चाहिए ।api.key = कुछ मान जोड़ें

MostIMP : अन्यथा यह "" सर्विसफ़टीपी नाम के साथ बीन बनाने में त्रुटि "त्रुटि निर्भर निर्भरता का इंजेक्शन" फेंक देगा


0

स्प्रिंग बूट के साथ:

Pom.xml में

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <addResources>true</addResources>
            </configuration>
        </plugin>
    </plugins>
</build>

उदाहरण जावा में

@Configuration
@Slf4j
public class MyAppConfig {
    @Value("${foo}")
    private String foo;
    @Value("${bar}")
    private String bar;
    @Bean("foo")
    public String foo() {
        log.info("foo={}", foo);
        return foo;
    }
    @Bean("bar")
    public String bar() {
        log.info("bar={}", bar);
        return bar;
    }
    [ ... ]

गुण फ़ाइलों में:

src / मुख्य / संसाधन / application.properties

foo=all-env-foo

src / मुख्य / संसाधन / application-rec.properties

bar=rec-bar

src / मुख्य / संसाधन / application-prod.properties

bar=prod-bar

VM में Application.java के तर्क

-Dspring.profiles.active=[rec|prod]

गुणों को संशोधित करने के बाद mvan कमांड चलाना न भूलें!

mvn clean package -Dmaven.test.skip=true

लॉग फ़ाइल के लिए -Dspring.profiles.active = rec:

The following profiles are active: rec
foo=all-env-foo
bar=rec-bar

लॉग फ़ाइल के लिए -spring.profiles.active = ठेस:

The following profiles are active: prod
foo=all-env-foo
bar=prod-bar

लॉग फ़ाइल के लिए -Dspring.profiles.active = स्थानीय:

Could not resolve placeholder 'bar' in value "${bar}"

Oups, मैं application-local.properties बनाना भूल जाता हूं।


0

मुझे अपने माइक्रोसेवा प्रोजेक्ट में भी यही त्रुटि मिली। संपत्ति मेरी yml फ़ाइल में ही छूट गई थी। इसलिए मैंने इस समस्या का समाधान करने के लिए संपत्ति का नाम और मूल्य जोड़ा


0

यह त्रुटि दिखाई देती है क्योंकि स्प्रिंग प्रोजेक्ट फ़ाइल गुण ( bootstrap.yml या application.yml ) को नहीं पढ़ता है । इसे हल करने के लिए, आपको अपने pom.xml में निर्भरता को जोड़ना होगा

   <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-context</artifactId>
    </dependency>


-3

मेरा समाधान $ और {के बीच एक स्थान जोड़ना था।

उदाहरण के लिए:

@Value("${project.ftp.adresse}")

हो जाता है

@Value("$ {project.ftp.adresse}")
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.