2015-02-03 14 views
15

İlkbaharda oldukça yeni yaşıyorum, bu aptal bir soru ise beni affedin. Bir programı başlatmaya çalıştığımda aşağıdaki hatayı alıyorum: java.lang.IllegalArgumentException: Could not resolve placeholder 'appclient' in string value [${appclient}]. Aşağıdaki kod çalıştırıldığında hata atılır:Spring, yer tutucuyu çözemedi

package ca.virology.lib2.common.config.spring.properties; 
import ca.virology.lib2.config.spring.PropertiesConfig; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.Import; 
import org.springframework.context.annotation.PropertySource; 

@Configuration 
@Import({PropertiesConfig.class}) 
@PropertySource("${appclient}") 
public class AppClientProperties { 
private static final Logger log = LoggerFactory.getLogger(AppClientProperties.class); 
{ 
    //this initializer block will execute when an instance of this class is created by Spring 
    log.info("Loading AppClientProperties"); 
} 
@Value("${appclient.port:}") 
private int appClientPort; 

@Value("${appclient.host:}") 
private String appClientHost; 

public int getAppClientPort() { 
    return appClientPort; 
} 

public String getAppClientHost() { 
    return appClientHost; 
} 
} 

Bir özellik dosyası appclient.properties ana bilgisayar ve bağlantı bilgileriyle kaynaklar klasöründe bulunmaktadır çağırdı. Hiç değilse "${appclient}"'un nerede tanımlandığından emin değilim. Belki de tanımlanmamıştır ve bu da soruna neden olmaktadır. "${appclient}"'u "{classpath:/appclient.properties}" gibi bir şeye değiştirmem gerekiyor mu yoksa başka bir şey mi eksik?

+0

PropertySource sınıf yolunu kullanarak başvuruda bulunabilirsiniz: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html –

+0

Aslında çalışmayı deniyordum Bu program, 'A' olarak adlandırılan farklı bir program içinde 'B' olarak adlandırın. Görünüşe göre, B programını ayrı ayrı yürüttüğünüzde, program argümanı olarak $ {appclient} 'değerini alır; Ancak, bu A'dan B'yi çalıştırmaya çalıştığımda ve elde ettiğim hataya neden olmazdı. – Brkk

cevap

12

Özellikler dosyasını doğru okumadınız. PropertySource parametresini aşağıdaki gibi iletmelidir: file:appclient.properties veya classpath:appclient.properties. Ek açıklamayı değiştirin: Ayrıca ithal ediyoruz olarak

@PropertySource(value={"classpath:appclient.properties"}) 

Ancak ben senin PropertiesConfig dosya içeriyor bilmiyorum. İdeal olarak, @PropertySource ek açıklamasının burada saklanmış olması gerekir. Eğer böyle bir şey kullanabilirsiniz

8

Yukarıdaki Bahar 3.1 ve kullanıyorsanız, ... Önceki versiyonlarda

@Configuration 
@PropertySource("classpath:foo.properties") 
public class PropertiesWithJavaConfig { 

@Bean 
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
    return new PropertySourcesPlaceholderConfigurer(); 
} 
} 
Ayrıca xml yapılandırma gibi gidebilirsiniz

...

<?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:foo.properties" /> 

    </beans> 

.

İlgili konular