@Value总是在一个类中为空
我是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”一直为空。为什么这样?我错过了什么?提前致谢!
你可以尝试@PropertySource注解类,如下
@Configuration
@PropertySource(value="classpath:application.properties")
public class RestCallImpl implements RestCall {
@Value("${senderUrl}")
private String senderUrl;
}
感谢您的帮助!不,它不起作用(((( – user3742622
尝试使用以下:
public class RestCallImpl implements RestCall {
@Value("#{senderUrl}")
private String senderUrl;
...
基本上只是改变了 '$',以 '#'。
感谢您的帮助!不,它不起作用)InteliJ Idea说“无法解析变量”,当我尝试运行应用程序时出现错误 – user3742622
这两个变量都在这两个应用程序属性文件? –
是的,我可以通过InteliJ Idea的$ $来看到senderUrl的价值...我打断了我的想法,为什么它不起作用(((So sad((( – user3742622
'RestCall'是你的界面,它由Spring管理吗? –
你为什么给'application.properties'作为类路径和文件?两个文件都不同吗? –
@NiteshVirani是的,它是不同的文件。我正在使用类路径中的属性文件中的变量来调试和生产中的外部文件。 – user3742622