亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

spring boot:如何從應用程序屬性配置數據源

spring boot:如何從應用程序屬性配置數據源

蠱毒傳說 2021-11-17 14:46:33
我想下面的代碼值:DriverClassName,Url,Username,Password從讀取application.properties文件,該怎么做?我正在使用 Spring Boot、Mysql、Hibernate 和 Spring Rest。數據源配置文件    //This is working fine with inline value specified as below (DriverClassName, Url,Username,Password    @Configuration    @EnableTransactionManagement    @EnableJpaRepositories(basePackages = "com.nouhoun.springboot.jwt.integration.repository")    public class DatasourceConfig {        @Bean        public DataSource datasource() throws PropertyVetoException {               final DriverManagerDataSource dataSource = new DriverManagerDataSource();               dataSource.setDriverClassName("com.mysql.jdbc.Driver");               dataSource.setUrl("jdbc:mysql://localhost:3306/fdb?createDatabaseIfNotExist=true");               dataSource.setUsername("root");               dataSource.setPassword("");               return dataSource;    }   ....   ....   ....
查看完整描述

2 回答

?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

一旦你定義數據源屬性application.properties中@SpringBootApplication它會自動配置datasource,這樣你就可以刪除DataSource configuration。但是如果你想自定義你的數據源配置,那么下面應該Environment可以讓你訪問屬性:


@Configuration

@PropertySource(value= {"classpath:application.properties"})

public class DatasourceConfig {


    @Autowired

    Environment environment;


    @Bean

    public DataSource datasource() throws PropertyVetoException {

        final DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(environment.getProperty("spring.datasource.driver-class-name"));

        dataSource.setUrl(environment.getProperty("spring.datasource.url"));

        dataSource.setUsername(environment.getProperty("spring.datasource.username"));

        dataSource.setPassword(environment.getProperty("spring.datasource.password"));

        return dataSource;

    }

}

或者,如果您不想通過 訪問屬性Environment,則可以通過訪問@Value


  @Value("${spring.datasource.driver-class-name}")

    private String driverName;


    @Value("${spring.datasource.url}")

    private String url;


    @Value("${spring.datasource.username}")

    private String userName;


    @Value("${spring.datasource.password}")

    private String password;


    @Bean

    public DataSource datasource() throws PropertyVetoException {

        final DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(driverName);

        dataSource.setUrl(url);

        dataSource.setUsername(userName);

        dataSource.setPassword(password);

        return dataSource;

    }


查看完整回答
反對 回復 2021-11-17
?
浮云間

TA貢獻1829條經驗 獲得超4個贊

似乎您忘記在 pom.xml 或 build.gradle 中添加依賴項,或者如果您已經添加,則您的構建沒有該依賴項(運行mvn clean install)


<dependency>

    <groupId>mysql</groupId>

    <artifactId>mysql-connector-java</artifactId>

    <version>6.0.6</version>

</dependency>

請添加并重試


查看完整回答
反對 回復 2021-11-17
  • 2 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號