I wrote the following code but I cannot make it work. I don't know where is the problem.
All classes are in the "hello" package.
public static void main(String args[]) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.scan("hello");
applicationContext.refresh();
ApplicationService app = new ApplicationService();
app.run();
applicationContext.close();
}
@Component
public class ApplicationService {
@Autowired
@Qualifier("main")
private SimpleDriverDataSource dataSourcee;
public void run(){
!!!!!!!! JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSourcee);
}
}
@Configuration
public class Config {
@Bean
@Qualifier("main")
public SimpleDriverDataSource getDataSource() {
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
dataSource.setDriverClass(org.h2.Driver.class);
dataSource.setUsername("sa");
dataSource.setUrl("jdbc:h2:mem");
dataSource.setPassword("");
return dataSource;
}
}
I tried in a lot of ways and combinations and I still get the following error. I tried without a qualifier, I changed the name of the method in Config, I tried to name the bean differently but I got no results. Where is the problem?
Exception in thread "main" java.lang.IllegalArgumentException: Property 'dataSource' is required
at org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(JdbcAccessor.java:135)
at org.springframework.jdbc.core.JdbcTemplate.<init>(JdbcTemplate.java:169)
at hello.ApplicationService.run(ApplicationService.java:28)
at hello.AppMain.main(AppMain.java:12)