После небольшого исследования я пришел к решению, которое использует ServletContextListener. Я пропущу объяснение и покажу вам код.
@WebListener
public class DatabaseCreator implements ServletContextListener {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
@Resource(
name = "jdbc/rssDS",
type = javax.sql.DataSource.class,
authenticationType = Resource.AuthenticationType.CONTAINER
)
private DataSource dataSource;
@Override
public void contextInitialized(ServletContextEvent sce) {
if (dataSource == null) {
logger.error("Data source wasn't initialized");
}
String sql = readStreamToString(sce.getServletContext());
if (StringUtils.isBlank(sql)) {
logger.error("SQL script is empty");
}
try {
logger.debug("Executing SQL script");
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
statement.execute(sql);
} catch (SQLException e) {
logger.error("Unable to create database table structure", e);
}
}
private String readStreamToString(ServletContext ctx) {
//skipped, reads stream contents into String
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
https://confluence.atlassian.com/display/DOC/Configuring+an+Oracle+Datasource+in+Apache+Tomcat, это ссылка поможет вам? –
@YeWin нет, на самом деле у меня уже есть полностью функционирующее приложение. Я просто хочу автоматизировать процесс развертывания. В основном мне нужен способ выполнения SQL-скрипта во время развертывания или первого запуска. –
О, я вижу, вы тоже не хотите использовать Весну? –