These two codes should do exactly the same thing, but the first one works and the second one doesnt work. Can anyone review the code and give the details about why the code failed during second approach.
The first code :
@Component public class AdminSqlUtil implements SqlUtil { @Autowired private ApplicationContext context; DataSource dataSource =(DataSource) context.getBean("adminDataSource"); public void runSqlFile(String SQLFileName) { Resource resource = context.getResource(SQLFileName); EncodedResource encodedResource = new EncodedResource(resource, Charset.forName("UTF-8")); try { ScriptUtils.executeSqlScript(dataSource.getConnection(), encodedResource); } catch (SQLException ex) { throw new RuntimeException(ex); } } The second code :
@Component public class AdminSqlUtil implements SqlUtil { @Autowired private ApplicationContext context; public void runSqlFile(String SQLFileName) { Resource resource = context.getResource(SQLFileName); EncodedResource encodedResource = new EncodedResource(resource, Charset.forName("UTF-8")); try { ScriptUtils.executeSqlScript((DataSource)context.getBean("adminDataSource").getConnection(), encodedResource); } catch (SQLException ex) { throw new RuntimeException(ex); } }