新项目打算用下springboot,使用druid数据源,根据网上的配置,直接配置。
spring:
datasource:
name: datasource
url:
username:
password:
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: oracle.jdbc.driver.OracleDriver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 1 from dual
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
发现启动后总是输出log
2017-01-19 14:16:56.321 [main] ERROR c.alibaba.druid.pool.DruidDataSource - testWhileIdle is true, validationQuery not set
2017-01-19 14:16:56.360 [main] INFO c.alibaba.druid.pool.DruidDataSource - {dataSource-1} inited
发现validationQuery没有自动注入,查资料发现,springboot1.4取消了spring.datasource.type参数。 需要手动声明datasource bean。
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class DruidConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(DruidConfiguration.class);
private String name;
private String url;
private String username;
private String password;
private String type;
private String driverClassName;
private String filters;
private int maxActive;
private int initialSize;
private int maxWait;
private int minIdle;
private int timeBetweenEvictionRunsMillis;
private int minEvictableIdleTimeMillis;
private boolean testWhileIdle;
private String validationQuery;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean poolPreparedStatements;
private int maxOpenPreparedStatements;
@Bean
public DruidDataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
try {
dataSource.setFilters(filters);
} catch (SQLException e) {
e.printStackTrace();
}
dataSource.setMaxActive(maxActive);
dataSource.setInitialSize(initialSize);
dataSource.setMaxWait(maxWait);
dataSource.setMinIdle(minIdle);
dataSource.setTimeBetweenConnectErrorMillis(timeBetweenEvictionRunsMillis);
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
dataSource.setTestWhileIdle(testWhileIdle);
dataSource.setValidationQuery(validationQuery);
dataSource.setTestOnBorrow(testOnBorrow);
dataSource.setTestOnReturn(testOnReturn);
dataSource.setPoolPreparedStatements(poolPreparedStatements);
dataSource.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
// dataSource.setFilters("mergeStat,wall,log4j");//mergeStat代替stat表示sql合并,wall表示防御SQL注入攻击
return dataSource;
}
@Bean
@Profile({"localdev", "remotetest"})
public ServletRegistrationBean druidServlet() {
LOGGER.info("\n********************************************************\n\n加载druid " +
"servlet\n\n********************************************************");
return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
}
@Bean
@Profile({"localdev", "remotetest"})
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}