How to make different class for DB connections

Viewed 108

I am developing a spring boot application and i need a common class to provide me Database Connections for all the controllers So I create a sepeate class As below:

@RestController
public class DataBaseConnector{


     @Autowired
     @Qualifier("dataSource")
     public static DataSource dataSource;

        @Bean
        @Primary
        @ConfigurationProperties(prefix = "spring.ds")
        public DataSource DataSourcePGStreet() {
            return DataSourceBuilder.create().build();
        }

       @Autowired
       public Connection giveConnection() throws SQLException{
           return dataSource.getConnection();
       }


}

Then In another Controller i call the connection as below:

@Autowired
    @Qualifier("dbc")
    private static DataBaseConnector obj;
@Autowired
 private Connection connectionDatabase;

.../// Rest Code
    @RequestMapping(value="/path",produces={MediaType.APPLICATION_JSON_VALUE},method=RequestMethod.GET)
     public ResponseEntity<?> getStreetScore(){


     obj=new DataBaseConnector();
     connectionDatabase=obj.giveConnection();



}

But this throws me an error of

Error creating of name with DataBaseConnector.Any help is appreciated

Full Stack TraceStacktrace

Updated Stack trace

Description:

Field dataSource in com.dmmltasmu.controller.DataBaseConnector required a bean of type 'javax.sql.DataSource' that could not be found.
    - Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'
    - Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required classes 'javax.transaction.TransactionManager', 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'
3 Answers
Related