Parameter 0 of constructor in com.myapp.MyBean required a bean of type 'org.springframework.jdbc.core.JdbcTemplate' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
I am starting a spring boot application. I am trying to communicate with my sqlite3 database (which lives in my resources dir as mydb.db) but can't get past this error when starting up the app.
This is my Main class:
@RestController
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
public class Main {
@Autowired
private MyBean bean;
@RequestMapping("/")
String home() {
return "Hello World!";
}
public void getNames() {
bean.getNames();
}
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
MyBean class:
@Component
public class MyBean {
private final JdbcTemplate jdbcTemplate;
public MyBean(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void getNames() {
List<Name> names= jdbcTemplate.query("SELECT * FROM names",
(rs, rowNum) -> new Name(rs.getInt("keyid"), rs.getString("name")));
for (Name name : names) {
System.out.println(name.getKeyid() + " - " + name.getName());
}
}
}
My applications.properties:
spring.datasource.url=jdbc:sqlite:file:mydb.db
spring.datasource.driver-class-name=org.sqlite.JDBC
And my dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.36.0.3</version>
</dependency>
</dependencies>
From my understanding, Spring Boot's auto-configuration should take care of what the error is saying but I can't figure it out
If you are facing issues when using @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) at the class level of your controller, it might be causing problems because it disables the default auto-configuration for the data source.
Here are a few things you can check and consider:
Example of a basic DataSource bean configuration:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("your.driver.class.name");
dataSource.setUrl("your.jdbc.url");
dataSource.setUsername("your.username");
dataSource.setPassword("your.password");
return dataSource;
}
}
@RestController
public class Main {
@Autowired
private MyBean bean;
@RequestMapping("/")
String home() {
return "Hello World!";
}
public void getNames() {
bean.getNames();
}
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}