Ver Fonte

控制台输出连接信息

liujs há 3 anos atrás
pai
commit
f2a99efdf9

+ 64 - 0
src/main/java/com/parksong/basics/util/configurations/DataSourceConfig.java

@@ -0,0 +1,64 @@
+package com.parksong.basics.util.configurations;
+
+import com.zaxxer.hikari.HikariDataSource;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import javax.sql.DataSource;
+
+/**
+ * @author LiuJS
+ * @Time 2021/7/15 14:38
+ * @Desc
+ * @Version 1.0
+ */
+@Slf4j
+@Configuration
+public class DataSourceConfig {
+    @Value("${spring.datasource.url}")
+    private String url;
+    @Value("${spring.datasource.username}")
+    private String username;
+    @Value("${spring.datasource.password}")
+    private String password;
+    @Value("${spring.datasource.driver-class-name}")
+    private String driverClassName;
+    @Value("${spring.datasource.type}")
+    private String type;
+    @Value("${spring.datasource.max-idle}")
+    private Integer maxIdle;
+    @Value("${spring.datasource.min-idle}")
+    private Integer minIdle;
+    @Value("${spring.datasource.max-wait}")
+    private Integer maxWait;
+    @Value("${spring.datasource.initial-size}")
+    private Integer initialSize;
+
+    /**
+     * 配置连接池信息
+     * @return
+     */
+    @Bean
+    public DataSource dataSource(){
+        log.info("----------------------开始进行数据库连接------------------------");
+        HikariDataSource dataSource = new HikariDataSource();
+        //添加数据库数据
+        dataSource.setJdbcUrl(url);
+        dataSource.setUsername(username);
+        dataSource.setPassword(password);
+        dataSource.setDriverClassName(driverClassName);
+
+        dataSource.setMinimumIdle(minIdle);
+        try {
+            dataSource.getConnection();
+            log.info("-----------------------数据库连接成功---------------------------");
+        }catch (Exception e){
+            log.info("-----------------------数据库连接失败----------------------------");
+            e.printStackTrace();
+        }finally {
+            return dataSource;
+        }
+    }
+}

+ 13 - 5
src/main/java/com/parksong/basics/util/configurations/RedisConfig.java

@@ -40,9 +40,8 @@ public class RedisConfig {
     private boolean  blockWhenExhausted;
 
     @Bean
-    public JedisPool redisPoolFactory()  throws Exception{
-        log.info("JedisPool注入成功!!");
-        log.info("redis地址:" + host + ":" + port);
+    public JedisPool redisPoolFactory(){
+        log.info("----------------------开始进行Redis连接------------------------");
         JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
         jedisPoolConfig.setMaxIdle(maxIdle);
         jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
@@ -50,7 +49,16 @@ public class RedisConfig {
         jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
         // 是否启用pool的jmx管理功能, 默认true
         jedisPoolConfig.setJmxEnabled(true);
-        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
-        return jedisPool;
+        JedisPool jedisPool = null;
+        try {
+            jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
+            jedisPool.getResource();
+            log.info("----------------------Redis连接成功------------------------");
+        }catch (Exception e){
+            log.info("----------------------Redis连接失败------------------------");
+            e.printStackTrace();
+        }finally {
+            return jedisPool;
+        }
     }
 }