Spring启动无法从配置文件特定的yaml文件中读取

时间:2017-09-24 00:41:39

标签: java spring-boot

我正在使用spring boot并尝试创建特定于配置文件的配置文件。我称之为application-local.yml

并补充道:

spring:
 profiles:
  active: local

 mysql:
  db:
   url: jdbc:mysql://localhost:3306/db?serverTimezone=UTC
   driverClassName: com.mysql.jdbc.Driver
   username: root
   password:

hibernate:
 dialect: org.hibernate.dialect.MySQL5Dialect
 show_sql: false

server:
 port: 8080

在我的DatabaseConfig.java文件中,我尝试从application-local.yml读取并配置数据库:

@Configuration
@EnableTransactionManagement
public class DatabaseConfig {

    @Value("${spring.mysql.db.url}")
    private String url;

    @Value("${spring.mysql.db.username}")
    private String userName;

    @Value("${spring.mysql.db.password}")
    private String password;

    @Value("${spring.mysql.db.driverClassName}")
    private String driverClassName;

    //hibernate
    @Value("${hibernate.dialect}")
    private String hibernateDialect;

    @Value("${hibernate.show_sql}")
    private String hibernateShowSql;

    @Bean
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean(name="entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
        LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean =
                new LocalContainerEntityManagerFactoryBean();
        localContainerEntityManagerFactoryBean.setDataSource(dataSource());
        localContainerEntityManagerFactoryBean.setPackagesToScan("xxxx");

        JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        localContainerEntityManagerFactoryBean.setJpaProperties(hibernateProperties());
        return localContainerEntityManagerFactoryBean;
    }

    @Bean
    public PlatformTransactionManager platformTransactionManager() {
        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        jpaTransactionManager.setEntityManagerFactory(entityManagerFactoryBean().getObject());
        return jpaTransactionManager;
    }

    private Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", hibernateDialect);
        properties.put("hibernate.show_sql", hibernateShowSql);
        return properties;
    }
}

现在我收到了无法

的错误
  

创建名为'databaseConfig'的bean时出错:注入自动连接的依赖项失败;嵌套异常是java.lang.IllegalArgumentException:无法解析值“$ {spring.mysql.db.url}”中的占位符'spring.mysql.db.url'

但是当我将属性文件重命名为application.yml时。它完美无缺。

我还尝试使用gradle命令从终端运行:./gradlew -Dspring.profiles.active=local bootRun但是我得到了同样的错误。它仅在我将YAML文件重构为application.yml时才有效。我究竟做错了什么?我打算有3个配置文件,如local,dev和prod。

2 个答案:

答案 0 :(得分:0)

在您的gradle文件中添加以下内容

./gradlew bootRun -Dspring.profiles.active=local

或尝试

{{1}}

答案 1 :(得分:0)

我有同样的问题。我通过添加

解决了这个问题
for value in issues_in_project:

   # standart field
   print(getattr(value.fields(), 'reporter'))
   print(getattr(value.fields(), 'summary'))

   # custom field
   print(getattr(value.fields(), 'customfield_15100'))

请确保其缩进正确。