How to make a different class for DB connections
I am developing a spring boot application and I need a generic class to provide me with database connections for all controllers. So I am creating a sepeate class. As shown 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 like 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 generates an error
Error creating name with DataBaseConnector. Any help is appreciated
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'
source to share
You cannot autowire static fields. Terrible but if you want to try below one
@Component
public class AnotherController {
private static DataBaseConnectionProvider obj;
@Autowired
public void setDataBaseConnectionProvider(DataBaseConnectionProvider obj) {
AnotherController.obj = obj;
}}
source to share
You are using static
@Autowired
@Qualifier("dataSource")
public static DataSource dataSource;
Spring cannot autwire static
. Deletestatic
You can try the autowire method and asign to the static, but this is a terrible approach.
The same in
@Autowired
@Qualifier("dbc")
private static DataBaseConnector obj;
Remove static.
The Java BTW naming convention assumes that method names begin with lowercase. Fix
DataSourcePGStreet
source to share
Don't try to type Connection
, which will lead to errors and difficult to debug problems. Just use JdbcTemplate
to access JDBC so it doesn't make your life harder.
Next to @Autowired
on fields static
won't work, you can only type in tags static
.
Suppose you have configured your properties appropriately spring.datasource
(unless you rename your properties spring.ds
to spring.datasource
) in this way Spring will automatically configure DataSource
and JdbcTemplate
.
Judging by your code, just remove DataBaseConnector
.
And to use JdbcTemplate
just enter it.
@Autowired
private JdbcTemplate jdbc;
@RequestMapping(value="/path",produces={MediaType.APPLICATION_JSON_VALUE},method=RequestMethod.GET)
public ResponseEntity<?> getStreetScore(){
Obj result = jdbc.queryForObject(<your-query>, new RowMapper() { ... });
return ResponseEntity.ok(result);
}
Or whatever your implementation.
source to share