Reputation: 2750
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
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'
Upvotes: 3
Views: 429
Reputation: 124431
Don't try to inject a Connection
that will lead to errors and hard to debug issues. Just use a JdbcTemplate
to do JDBC access don't make your life harder.
Next to that @Autowired
on static
fields won't work you can only inject into non static
fields.
Assuming you have configured your spring.datasource
properties accordingly (if not rename your spring.ds
properties to spring.datasource
) that way Spring will automatically configure a DataSource
and JdbcTemplate
.
Judging from your code just delete your DataBaseConnector
.
And to use the JdbcTemplate
just inject 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 was.
Upvotes: 1
Reputation: 57381
You use static
@Autowired
@Qualifier("dataSource")
public static DataSource dataSource;
Spring cannot autowire static
. Remove the static
You can try to autowire by method and asign to thee static but this is awful approach.
The same in the
@Autowired
@Qualifier("dbc")
private static DataBaseConnector obj;
Remove static.
BTW java naming convention supposes method names start from lower case. Correct the
DataSourcePGStreet
Upvotes: 2
Reputation: 732
You can't autowire the static fields. Ugly 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;
}}
Upvotes: 4