spring-boot项目中datasource可选

2020-03-03  本文已影响0人  alex102400

/**

* 是否启用数据库的配置控制。主要是控制是否启用druid

*/

@Configuration

@AutoConfigureBefore(DruidDataSourceAutoConfigure.class)

public class DbEnableConfiguration {

private static final Loggerlog = LogManager.getLogger();

    @Bean

    @ConditionalOnProperty(name ="app.datasource.enable", havingValue ="false")

public DataSourcedataSource() {

log.info("Init NoopDataSource");

        return new NoopDataSource();

    }

/**

* 空数据源实现

*/

    public static class NoopDataSourceimplements javax.sql.DataSource {

@Override

        public ConnectiongetConnection()throws SQLException {

return null;

        }

@Override

        public ConnectiongetConnection(String username, String password)throws SQLException {

return null;

        }

@Override

        public T unwrap(Class iface)throws SQLException {

return null;

        }

@Override

        public boolean isWrapperFor(Class iface)throws SQLException {

return false;

        }

@Override

        public PrintWritergetLogWriter()throws SQLException {

return null;

        }

@Override

        public void setLogWriter(PrintWriter out)throws SQLException {

}

@Override

        public void setLoginTimeout(int seconds)throws SQLException {

}

@Override

        public int getLoginTimeout()throws SQLException {

return 0;

        }

@Override

        public java.util.logging.LoggergetParentLogger()throws SQLFeatureNotSupportedException {

return null;

        }

}

}

上一篇下一篇

猜你喜欢

热点阅读