Disable Spring Boot Hikari Datasource with aspectj

There is database which will shutdown in uncertain future and application wants to be restarted and still working after that. In spring boot every datasource bean depends to some other bean, if we use @ConditionalOnProperty which actually not creates the bean after that other beans fails to initialize and app stop booting.

If need to disable a datasource without deleting all the spring beans or without using @ConditionalOnProperty

When @ConditionalOnProperty comes to scene it is like deleting or changing all other spring beans which is not really required if you just want to disable a datasource.

Use a dummy datasource with SimpleDriverDataSource

public DataSource dataSource(){
    SimpleDriverDataSource simpleDriverDataSource = new SimpleDriverDataSource();
    return simpleDriverDataSource;
    return new HikariDataSource(hikariConfig());

this way if any calls goes to that db it will throw some exception but application will still boot up. Second step use aop to disable all db calls and return empty data.

import org.apache.ibatis.binding.MapperProxy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.Proxy;

public class MyBatisAop {

	@Around("execution(* org.test.common.dao.BaseDAO.*(..))")
	public Object invoke(ProceedingJoinPoint proceedingJoinPoint)
			throws Throwable {
		return new Object();