2017-09-24 41 views
0

我正在玩彈簧啓動並試圖創建配置文件特定的配置文件。我稱之爲application-local.yml春季開機無法讀取配置文件特定yaml文件

並補充說:

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個配置文件,如本地,開發和產品。

+0

如果使用的是春天開機,那麼你爲什麼不使用數據源自動配置? –

回答

0

要麼添加在您的gradle這個文件

bootRun { 
    profile: local 
} 

或以下嘗試

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