我是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」一直爲空。爲什麼這樣?我錯過了什麼?提前致謝!
'RestCall'是你的界面,它由Spring管理嗎? –
你爲什麼給'application.properties'作爲類路徑和文件?兩個文件都不同嗎? –
@NiteshVirani是的,它是不同的文件。我正在使用類路徑中的屬性文件中的變量來調試和生產中的外部文件。 – user3742622