从POM文件设置Spring applicationContext属性

时间:2017-03-09 04:48:58

标签: spring maven

我在applicationContext.xml文件中有以下配置:

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:application.properties</value>             
                <value>classpath:database.properties</value>
            </list>
        </property>
    </bean>

<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${smtp.host}" />
    </bean>

在我的POM文件中设置了smtp.host,如下所示:

<build>
    <defaultGoal>install</defaultGoal>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    ...
</build>

<profile>
    <id>local</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <application.env>local</application.env>
        <profile.scope>compile</profile.scope>
        <skip.test>true</skip.test>
        <smtp.host>my.smtp.server</smtp.host>
    </properties>
</profile>

在部署我的应用程序时,我遇到一条错误消息,指出Spring无法解析smtp.host。我将以下映射添加到我的application.properties文件中:

smtp.host=${smtp.host}

但Spring开始抱怨我在该属性上有一个循环占位符引用。有什么我想念的吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

你在这里混合了两件事。 lblDetail.text = [NSString stringWithFormat:@"Received card info. Number: %@, expiry: %02lu/%lu, cvv: %@.", info.cardNumber, (unsigned long)info.expiryMonth, (unsigned long)info.expiryYear, info.cvv]; 用于构建应用程序。您定义的属性通常与您的应用程序属性无关。 Maven配置文件与Spring配置文件无关。他们只被命名为equaly。

您应该配置Spring应用程序,因为您可以阅读here。你可以 - 我不建议 - 用你的pom作为财产来源。

通常的方法是从外部化配置中读取它。我不知道你是否使用Spring Boot,你可以查看Spring Boot Way并调整它,如果你使用Spring而不使用Boot。

所以将pom.xml文件添加到src / mein / resources中,如

apllication.properties

如果你使用boot,你就完成了,否则你必须添加一个

smtp.host=my.smtp.server

@PropertySource("classpath:/application.properties")

答案 1 :(得分:1)

在这里,您将混合应用程序的构建和运行时阶段,它们是互斥的。

内行&#39;角色结束一旦构建完成,因此使用的任何属性都会随之消失。此外,应用程序启动与用于构建它的工具/进程无关,因此它们之间没有共享任何信息。因此,使用pom.xml中指定的属性的想法是不可行的。

关于循环引用,语句smtp.host=${smtp.host}与java代码int i = i;非常相似,因为i已定义并分配给自身,因此基本上没有效果。

相关问题