java – Eine Frage in Druid-Spring-Boot-Starter
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-07-05 10:26:11
0
1
1065

Veröffentlichen Sie zuerst den Code

@Configuration
@ConditionalOnClass(com.alibaba.druid.pool.DruidDataSource.class)
@EnableConfigurationProperties(DruidStatProperties.class)
@Import({DruidSpringAopConfiguration.class,
         DruidStatViewServletConfiguration.class,
         DruidWebStatFilterConfiguration.class})
public class DruidDataSourceAutoConfigure {

    @Bean
    @ConfigurationProperties("spring.datasource.druid")
    @ConditionalOnMissingBean
    public DataSource dataSource(Environment env) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();

        //if not found prefix 'spring.datasource.druid' settings,'spring.datasource' prefix settings will be used.
        if (dataSource.getUsername() == null) {
            dataSource.setUsername(env.getProperty("spring.datasource.username"));
        }
        if (dataSource.getPassword() == null) {
            dataSource.setPassword(env.getProperty("spring.datasource.password"));
        }
        if (dataSource.getUrl() == null) {
            dataSource.setUrl(env.getProperty("spring.datasource.url"));
        }
        if (dataSource.getDriverClassName() == null) {
            dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        }
        // set filters default value on StatViewServlet enabled.
        if (! "false".equals(env.getProperty("spring.datasource.druid.StatViewServlet.enabled"))) {
            try {
                dataSource.setFilters("stat");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return dataSource;
    }
}
public class DruidDataSourceBuilder {

    private Map<String, String> properties = new HashMap<String, String>();

    public static DruidDataSourceBuilder create() {
        return new DruidDataSourceBuilder();
    }

    public DruidDataSource build() {
        DruidDataSource dataSource = new DruidDataSource();
        maybeGetDriverClassName();
        bind(dataSource);
        return dataSource;
    }

    //use spring boot relaxed binding by reflection config druid . detail see Spring Boot Reference Relaxed binding section.
    private void bind(DruidDataSource result) {
        MutablePropertyValues properties = new MutablePropertyValues(this.properties);
        new RelaxedDataBinder(result)
                .withAlias("url", "jdbcUrl")
                .withAlias("username", "user")
                .bind(properties);
    }

    private void maybeGetDriverClassName() {
        if (!this.properties.containsKey("driverClassName")
                && this.properties.containsKey("url")) {
            String url = this.properties.get("url");
            String driverClass = DatabaseDriver.fromJdbcUrl(url).getDriverClassName();
            this.properties.put("driverClassName", driverClass);
        }
    }
}

Was ich nicht verstehe, ist die MaybeGetDriverClassName-Methode von DruidDataSourceBuilder. Wenn die DataSource-Methode in DruidDataSourceBuilder verwendet wird, haben die Eigenschaften in DruidDataSourceBuilder offensichtlich keine Bedeutung. Ist die MaybeGetDriverClassName-Methode in diesem Fall nicht bedeutungslos?
Ein weiterer Punkt ist die if-Beurteilung in der dataSource-Methode von DruidDataSourceAutoConfigure. Die dataSource sollte keine Attribute in die Methode eingefügt haben. Ist es nicht sinnlos, so zu urteilen?

曾经蜡笔没有小新
曾经蜡笔没有小新

Antworte allen(1)
过去多啦不再A梦

可能是为以后 properties 里面添加其他属性留余地吧。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!