在amazon AWS EC2实例中设置弹簧活动配置文件

时间:2017-10-14 04:12:19

标签: java spring hibernate amazon-web-services amazon-ec2

我已经设置了Amazon AWS EC2实例并安装了tomcat8和jdk 1.8。 我已经在tomcat的webapps目录中部署了基于Spring / Hibernate的web-app war文件,到目前为止一切正常。它的Spring Web MVC。

但我想用现有的配置实现 spring.active.profiles ,这种配置永远不会有效。

以下是我的JPAConfig文件的内容,该内容在本地服务器上运行良好,但不在 AWS EC2 上。我觉得我的EC2实例存在一些配置错误。

@Configuration
@EnableTransactionManagement
@PropertySources({ 
    @PropertySource("classpath:application.properties"),

       @PropertySource("classpath:application-${spring.profiles.active}.properties"),
        @PropertySource("classpath:api.properties"),
        @PropertySource("classpath:mailer.properties") })
    public class JPAConfiguration {

        @Autowired
        private Environment environment;

    //rest or codes    

如果我评论@PropertySource("classpath:application-${spring.profiles.active}.properties"),行,那么EC2上的工作正常,但出于显而易见的原因,它会选择application.properties个文件内容。当我取消注释它时,我的EC2 URL返回404 NOT not found。

请帮助在EC2实例上实现spring.active.profiles功能,以便我的EC2实例使用我的application-prod.properties文件

此外,请注意,由于某些未知原因,我无法在tomcat8的目录中看到除“webapps”之外的任何其他目录

1 个答案:

答案 0 :(得分:0)

在EC2实例中部署的工件上设置活动配置文件,即 在战争中打包的web.xml上:

web.xml

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>prod</param-value>
</context-param>

或使用注释等 https://www.mkyong.com/spring/spring-profiles-example/ 如果你的tomcat只提供这个应用程序(或prod个,你也可以将-Dspring.profiles.active=prod传递给jvm,即从CATALINA_OPTS env var我猜。

如果您的webapp是唯一一个托管在该tomcat上的,请考虑使用Spring Boot并将webapp打包为胖罐。

相关问题