dubbo.spring.javaconfig.DatabaseConfig.java Source code

Java tutorial

Introduction

Here is the source code for dubbo.spring.javaconfig.DatabaseConfig.java

Source

/*
 * Copyright 2016-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dubbo.spring.javaconfig;

import com.wiiyaya.framework.provider.repository.BaseDaoFactoryBean;
import com.wiiyaya.framework.provider.tools.hibernate.DriverType;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.io.IOException;

/**
 * <p>??</p>
 * <p>
 * <p>??????jpa?</p>
 * <p>
 * <p></p>
 *
 * @author wiiyaya
 */
@Configuration
@ComponentScan(basePackages = { "com.wiiyaya.provider.*.service.impl" })
@EnableJpaRepositories(basePackages = {
        "com.wiiyaya.provider.*.repository" }, repositoryFactoryBeanClass = BaseDaoFactoryBean.class)
@EnableJpaAuditing(auditorAwareRef = "auditorProvider", dateTimeProviderRef = "dateTimeProvider")
@EnableTransactionManagement
//@PropertySource("classpath:database.properties")//properties?
public class DatabaseConfig {

    //===========properties?  ===============
    //@PropertySource?EnvironmentEnvironment?jvm???systemjndi
    /*@Autowired
    private Environment env;//?env.getProperty()???
        
    //EL?? #{} environment@PropertySourcebean??
    @Value("#{environment['db.validate']}")
    private String dbValidate;
        
    @Value("#{environment['db.driverType']}")
    private String dbDriverType;
        
    @Value("#{environment['db.url']}")
    private String dbUrl;
        
    @Value("#{environment['db.username']}")
    private String dbUsername;
        
    @Value("#{environment['db.password']}")
    private String dbPassword;*/
    //===========properties?  ?===============

    //===========properties?  ===============
    //??? ${}?PropertySourcesPlaceholderConfigurer??
    //????${jdbc.username.${phase}}
    //???EL??#{'${server.name}'.split(',')}
    //?????${key:default}
    /*@Value("${db.driverType}")
    private String dbDriverType;
        
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
       return new PropertySourcesPlaceholderConfigurer();
    }*/
    //===========properties?  ?===============

    //===========properties?  ===============
    /*@Autowired
    @Qualifier("dbConfig")
    private Properties dbConfig;*/

    @Value("#{dbConfig['db.validate']}")
    private String dbValidate;

    @Value("#{dbConfig['db.conn.driverType']}")
    private String dbConnDriverType;

    @Value("#{dbConfig['db.conn.url']}")
    private String dbConnUrl;

    @Value("#{dbConfig['db.conn.username']}")
    private String dbConnUsername;

    @Value("#{dbConfig['db.conn.password']}")
    private String dbConnPassword;

    @Value("#{dbConfig['db.pool.timeBetweenEvictionRunsMillis']}")
    private int dbPoolTimeBetweenEvictionRunsMillis;

    @Value("#{dbConfig['db.pool.validationQueryTimeout']}")
    private int dbPoolValidationQueryTimeout;

    @Value("#{dbConfig['db.pool.maxActive']}")
    private int dbPoolMaxActive;

    @Value("#{dbConfig['db.pool.maxWait']}")
    private int dbPoolMaxWait;

    @Value("#{dbConfig['db.pool.initialSize']}")
    private int dbPoolInitialSize;

    @Value("#{dbConfig['db.pool.maxIdle']}")
    private int dbPoolMaxIdle;

    @Value("#{dbConfig['db.pool.minIdle']}")
    private int dbPoolMinIdle;

    @Value("#{dbConfig['db.pool.minEvictableIdleTimeMillis']}")
    private int dbPoolMinEvictableIdleTimeMillis;

    @Value("#{dbConfig['db.pool.removeAbandonedTimeout']}")
    private int dbPoolRemoveAbandonedTimeout;

    /**
     * ??
     * @return
     */
    @Bean
    public PropertiesFactoryBean dbConfig() {
        PropertiesFactoryBean dbConfig = new PropertiesFactoryBean();
        dbConfig.setLocation(new ClassPathResource("database.properties"));
        return dbConfig;
    }
    //===========properties?  ?===============

    /**
     * hibernate ? envers ?
     * @return
     */
    @Bean
    public PropertiesFactoryBean hibernateConfig() {
        PropertiesFactoryBean dbConfig = new PropertiesFactoryBean();
        dbConfig.setLocation(new ClassPathResource("hibernate.properties"));
        return dbConfig;
    }

    /**
     * JPA?
     */
    @Bean
    public PlatformTransactionManager transactionManager() throws IOException {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
        return transactionManager;
    }

    /**
     * ?
     */
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws IOException {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(dataSource());
        entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter());
        entityManagerFactory.setJpaProperties(hibernateConfig().getObject());
        entityManagerFactory.setPackagesToScan("com.wiiyaya.provider.*.entity");
        return entityManagerFactory;
    }

    /**
     * jpa
     */
    private JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setDatabasePlatform(DriverType.valueOf(dbConnDriverType).getPlatform());
        return jpaVendorAdapter;
    }

    /**
     * ??
     */
    @Bean
    public DataSource dataSource() {
        DataSource source = new DataSource();
        source.setDriverClassName(DriverType.valueOf(dbConnDriverType).getClassName());
        source.setUrl(dbConnUrl);
        source.setUsername(dbConnUsername);
        source.setPassword(dbConnPassword);

        //      source.setTestOnConnect(true);//false?(InitSQLvalidate?)?
        //      source.setTestOnBorrow(true);//false??
        //      source.setTestOnReturn(true);//false
        //      source.setValidationInterval(600000);//?30000
        source.setTestWhileIdle(true);//false??
        source.setTimeBetweenEvictionRunsMillis(dbPoolTimeBetweenEvictionRunsMillis);//10005000
        //      source.setInitSQL("SELECT 1");
        source.setValidationQuery("SELECT 1");//?
        //      source.setValidator();//
        //      source.setValidatorClassName();//??
        source.setValidationQueryTimeout(dbPoolValidationQueryTimeout);//-1?

        source.setMaxActive(dbPoolMaxActive);//??0?
        source.setMaxWait(dbPoolMaxWait);//??30000
        source.setInitialSize(dbPoolInitialSize);//????
        source.setMaxIdle(dbPoolMaxIdle);//
        source.setMinIdle(dbPoolMinIdle);//?
        source.setMinEvictableIdleTimeMillis(dbPoolMinEvictableIdleTimeMillis);//?60000

        source.setRemoveAbandoned(true);//
        source.setRemoveAbandonedTimeout(dbPoolRemoveAbandonedTimeout);//60
        source.setLogAbandoned(true);//??

        return source;
    }
}