如何在 windows 上正确使用 Spring 个人资料

How to correctly using Spring profile on windows

我正在尝试为开发和生产维护不同的 Spring 配置文件,为此我在我的桌面上用我的 Spring 启动项目创建了一个文件夹(网络框架),应用程序-dev.properties 和应用程序-prod.properties.

但是,我无法将配置文件导入到我的项目中。我用来将其导入到我的项目中的代码如下。

@Configuration
@Profile("dev")
@PropertySource("file:///${user.home}/web skeleton/application-dev.properties")
public class DevelopmentConfig {
@Bean
public  EmailService emailService(){
    return new MockEmailService();
}

谁能告诉我这是否是在 Spring 中使用 PropertySource 的正确方法。

您可以选择定义我们存储这些属性的自定义源,否则会查找默认位置(class路径:application.properties)。所以我们现在将上面的注解添加到已有的属性class:

@Configuration
@PropertySource("classpath:configprops.properties")
@ConfigurationProperties(prefix = "dev")
public class ConfigProperties {
    // previous code
}

现在,属性 文件中定义的具有前缀 dev 且与其中一个属性同名的任何属性都会自动分配给此对象。

#Simple properties
dev.host=mailer@mail.com
dev.port=9000

勾选this

我也做过这种配置

只需在您的配置中添加以下代码class

@PropertySource("classpath:application-${spring.profiles.active}.properties")

而这个 属性 在 application.properties

spring.profiles.active=dev

您可以根据需要将其更改为 prod 和 cert。