'Spring @Autowire annotation failing for conditional beans

I have JPA repository which is a conditional Bean. It will be instantiated only if we have a bean named mySqlEntityManager .

@ConditionalOnBean(name="mySqlEntityManager")
public interface TransactionBlobRepo extends JpaRepository<TransactionBlobData, TransactionBlobId> {    

}

In Service class Iam injecting this repository class using @Autowire .There its failing, throwing exceptions. So i tried with required =false also , but it will throw NullPointerException since bean is not injected . I need to inject this bean conditionally like i did for jpa repository . Please suggest a solution.

@Service
public class TransactionDaoServiceImpl {

    @Autowired
    TransactionBlobRepo transactionBlobRepo;
    
    
    public void insertTransactionData(TransData data) throws SystemException{
        try {
            transactionBlobRepo.save(data); // transactionBlobRepo will be Null here if required =false
        }catch(Exception e) {
            throw new SystemException(e.getCause());
        }
    }
    
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source