2 回答
TA贡献2039条经验 获得超7个赞
另一种解决方法是实施 CommandLineRunner。
@Component
public class ConfigurationInitRunner implements CommandLineRunner {
@Autowired
Resources resources; // resources are retrieved from database.
@Override
public void run(String... strings) throws Exception {
Config.environment = resources.getWsaaEnv();
Config.validation = resources.getWsaaEnvValidation();
}
}
TA贡献1785条经验 获得超4个赞
我设法找到了解决方法。正如最新更新所述,使用@SqlGroup 无法实现所需的行为。我不得不使用以下方法,使用@BeforeClass 和@AfterClass:
@BeforeClass
public static void setUpBeforeClass() throws Exception {
DriverManagerDataSource dataSource = getDataSource();
Connection conn = dataSource.getConnection();
ScriptUtils.executeSqlScript(conn, new ClassPathResource("db_config.sql"));
ScriptUtils.executeSqlScript(conn, new ClassPathResource("consultas/setup_schema"));
ScriptUtils.executeSqlScript(conn, new ClassPathResource("db_init.sql"));
JdbcUtils.closeConnection(conn);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
Connection conn = getDataSource().getConnection();
ScriptUtils.executeSqlScript(conn, new ClassPathResource(
"db_teardown.sql"));
JdbcUtils.closeConnection(conn);
}
private static DriverManagerDataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driver);
return dataSource;
}
删除了所有@SqlGroup 注释。这种方法非常干净,因为 ScriptUtils 不需要太多的依赖来运行。
添加回答
举报