2015-08-08 143 views
0

我是Spring中的新手,現在我正試圖處理@Value註釋。我有兩個班。一個有註解:@Value總是在一個類中爲空

public class RssHandler { 
    @Value("${theTopic}") 
    private String theTopic; 
    ... 

而另外一個:

public class RestCallImpl implements RestCall { 
    @Value("${senderUrl}") 
    private String senderUrl; 
    ... 

我的屬性文件是:

theTopic=mytopic 
senderUrl=http://localhost:8080/ 

我的豆XML有,我發現這裏同樣的問題像所有的東西propertyConfigurer和beans聲明(據我所知):

<?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:int="http://www.springframework.org/schema/integration" 
     xmlns:feed="http://www.springframework.org/schema/integration/feed" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd 
          http://www.springframework.org/schema/integration 
          http://www.springframework.org/schema/integration/spring-integration.xsd 
          http://www.springframework.org/schema/integration/feed 
          http://www.springframework.org/schema/integration/feed/spring-integration-feed.xsd"> 

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
      <value>classpath:application.properties</value> 
      <value>file:/Users/Projects/Java/TestNotifier/resources/application.properties</value> 
      </list> 
     </property> 
     <property name="ignoreUnresolvablePlaceholders" value="true"/> 
     <property name="ignoreResourceNotFound" value="true"/> 
    </bean> 

    <!-- RSS Stuff --> 
    <int:channel id="inputRssFeedChannel"/> 

    <feed:inbound-channel-adapter id="news" 
            channel="inputRssFeedChannel" 
            url="http://feeds.feedburner.com/Techcrunch"> 
     <int:poller fixed-rate=5000 max-messages-per-poll=100/> 
    </feed:inbound-channel-adapter> 

    <int:service-activator input-channel="inputRssFeedChannel" 
          ref="rssPrintOutService" 
          method="printRss"/> 

    <bean id="rssPrintOutService" class="TestNotifier.RssHandler"/> 
    <bean id="SnsRestCall" class="TestNotifier.RestCallImpl"/> 
</beans> 

當我運行我的應用程序時,我完全得到「theTopic」,但「senderUrl」一直爲空。爲什麼這樣?我錯過了什麼?提前致謝!

+0

'RestCall'是你的界面,它由Spring管理嗎? –

+0

你爲什麼給'application.properties'作爲類路徑和文件?兩個文件都不同嗎? –

+0

@NiteshVirani是的,它是不同的文件。我正在使用類路徑中的屬性文件中的變量來調試和生產中的外部文件。 – user3742622

回答

0

你可以嘗試@PropertySource註解類,如下

@Configuration 
@PropertySource(value="classpath:application.properties") 
public class RestCallImpl implements RestCall { 

    @Value("${senderUrl}") 
    private String senderUrl; 

} 
+0

感謝您的幫助!不,它不起作用(((( – user3742622

0

嘗試使用以下:

public class RestCallImpl implements RestCall { 

    @Value("#{senderUrl}") 
    private String senderUrl; 
    ... 

基本上只是改變了 '$',以 '#'。

+0

感謝您的幫助!不,它不起作用)InteliJ Idea說「無法解析變量」,當我嘗試運行應用程序時出現錯誤 – user3742622

+0

這兩個變量都在這兩個應用程序屬性文件? –

+0

是的,我可以通過InteliJ Idea的$ $來看到senderUrl的價值...我打斷了我的想法,爲什麼它不起作用(((So sad((( – user3742622

相關問題