Im new to spring and Jdbc, JdbcTemplate works when I auto wire it in the main class, but when I auto wire it in any other class it is null.
note: I comment out one of the JdbcTemplate when I test the other, I thought it might make a conflict.
Test class (Main)
@SpringBootApplication
public class Test implements CommandLineRunner {
@Autowired
private JdbcTemplate jdbcTemplate; // this works
public static void main(String[] args) {
SpringApplication.run(Test.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("main class jdbc template = "+jdbcTemplate);
}
}
StudentDaoImpl class
public class StudentDaoImpl {
@Autowired
private JdbcTemplate jdbcTemplate; //this is null
}
Application.properties
spring.datasource.url=jdbc:mysql://localhost/db
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
I really have no clue why is this happening, any help will be appreciated.