1. 程式人生 > >SpringBoot Web專案雜記

SpringBoot Web專案雜記

一、整合多資料來源

1、pom檔案引入依賴

        <!-引入mybatis->
        <dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>

        <!-引入jpa->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-引入mysql->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-引入oracle->
        <dependency>
   			<groupId>com.oracle</groupId>
   			<artifactId>ojdbc6</artifactId>
   			 <version>11.2.0.3</version>
		</dependency>

2、配置properties檔案屬性

spring.datasource.mysql.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.mysql.jdbc-url=XXX
spring.datasource.mysql.username=XXX
spring.datasource.mysql.password=XXX
 
spring.datasource.oracle.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.oracle.jdbc-url=XXX
spring.datasource.oracle.username=XXX
spring.datasource.oracle.password=XXX

springboot2.0版本後,資料來源的url屬性名稱為jdbc-url,2.0前為url

3、配置DataSource、SqlSessionFactory、TransactionManager、SqlSessionTemplate


@Configuration
@MapperScan(basePackages = {"oracle資料來源掃描dao包的路徑"}, sqlSessionFactoryRef = "oracleSqlSessionFactory")
public class OracleDataSourceConfig {
 
    //建立資料來源bean物件
    @Bean(name = "oracleDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.oracle")//指定使用配置屬性的字首
    @Primary
    public DataSource oracleDataSource() {
        return DataSourceBuilder.create().build();
    }
    //建立SqlSessionFactory的bean物件
    @Bean(name = "oracleSqlSessionFactory")
    @Primary
    public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //mapper檔案掃描路徑
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper檔案路徑"));
        return bean.getObject();
    }
    //建立TransactionManager的bean物件
    @Bean(name = "oracleTransactionManager")
    @Primary
    public DataSourceTransactionManager oracleTransactionManager(@Qualifier("oracleDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
  //建立SqlSessionTemplate的bean物件
    @Bean(name = "oracleSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate oracleSqlSessionTemplate(@Qualifier("oracleSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

mysql資料來源配置方法同上

二、攔截器的使用

在@SpringBootApplication類下,新建過濾方法

@Bean
    public FilterRegistrationBean ParameterFilterRegister() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        //注入過濾器
        registration.setFilter(new ParameterFilter());
        //攔截規則
        registration.addUrlPatterns("/*");
        //過濾器名稱
        registration.setName("parameterFilter");
        //初始化引數
        Map<String,Object> initParameters = new HashMap<String,Object>();
        initParameters.put("xxx", "xxx");
        registration.setInitParameters(initParameters);
        //過濾器順序,按從小到大執行
        registration.setOrder(1);
        return registration;
    }

三、獲取ApplicationContext物件

編寫一個實體類實現ApplicationContextAware介面

package com.bdvo.SmartPush.base.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextUtil
    implements ApplicationContextAware
{
    /**
     * 上下文物件例項
     */
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * 獲取applicationContext
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 通過name獲取 Bean.
     * @param name
     * @return
     */
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    /**
     * 通過class獲取Bean.
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 通過name,以及Clazz返回指定的Bean
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}

四、其他配置

1、新增專案訪問字首

server.servlet.context-path=/XXX

2、開啟日誌

logging.level.XXX=DEBUG //xxx即需列印日誌的包 日誌級別從低到高 TRACE < DEBUG < INFO < WARN < ERROR < FATA

springboot預設的日誌是logback,若想使用其他日誌框架(如log4j)需引入pom依賴

更多的配置引數可以通過新建配置檔案來設定(當然也可以寫在application.properties裡)

Logback: logback-spring.xml, logback-spring.groovy, logback.xml, logback.groovy Log4j: log4j-spring.properties, log4j-spring.xml, log4j.properties, log4j.xml  

3、分割槽配置檔案

spring.profiles.active=dev 可以指定引用application-dev.properties,通過這個屬性可以區分測試、生產配置檔案