javaspring-bootsonarqubesimplejdbccall

Multiple SimpleJDBCCall beans causing Sonar issue in Java


My application is springboot and I am having DatabaseConfig where multiple SimpleJDBCCall beans are configured to execute stored proc and am having DAO where am injecting beans using constructor. This is working well, however this is failing on sonar because I have more than 7 beans. We cannot tune Sonar settings as many teams use it. So please suggest here, Below is how my DatabaseConfig looks

@Configuration
public class DatabaseConfig {

@Autowired
public JDBCTemplate jdbcT;

@Bean(spOne)
public SimpleJDBCCall spOne(){
   return new SimpleJDBCCall(jdbcT).withSchema(x).....;
}

@Bean(spTwo)
public SimpleJDBCCall spTwo(){
   return new SimpleJDBCCall(jdbcT).withSchema(x).....;
}

@Bean(spTen)
public SimpleJDBCCall spTen(){
   return new SimpleJDBCCall(jdbcT).withSchema(x).....;
}}

Below is how my DAO class looks like

@Repository
public class Dao{
private JDBCTemplate jdbcT;
private SimpleJDBCCall spOne; private SimpleJDBCCall spTwo; private SimpleJDBCCall spTen;

  public Dao(JDBCTemplate jdbcT, SimpleJDBCCall spOne; SimpleJDBCCall spTwo; SimpleJDBCCall spTen){
         this.jdbcT = jdbcT;
         this.spOne = spOne;
         this.spOne = spTwo; // More settings of storedProc beans.
         this.spOne = spTen;
}}

Solution

  • You could define a Map as constructor parameter, like this:

    public Dao(JDBCTemplate template, Map<String, SimpleJDBCCall> jdbcCalls){
        
    }
    

    Spring will then add all bean instances of type SimpleJDBCCall into the map. The key of the map would be the bean names (in your case e.g. "spOne", "spTwo" etc.), and the value is the instance of the bean. If you still can do your usecase with this, it might be the easiest solution.

    If you don't access specific beans in your code, but e.g. just iterate over all templates to execute a call, a List instead of the map could also be sufficient then.