Java 如何在首次部署应用程序时运行查询,然后在剩余生命周期中保留数据

Java 如何在首次部署应用程序时运行查询,然后在剩余生命周期中保留数据,java,spring,spring-mvc,spring-jdbc,Java,Spring,Spring Mvc,Spring Jdbc,我有一个查询,从数据库中获取列名。这些列名不会因请求或会话而更改。因此,为了避免在每个请求上运行无用的查询,我希望在部署应用程序时运行此查询一次,然后在应用程序的剩余生命周期中将结果保留在列表中,直到再次部署应用程序 如何实现这一点,以便在服务层中使用最终列表 如果有更好的方法,而不是在部署时运行查询,那么请建议其他方法 目前,我有: public List<String> fetchColNames (String tableName) { String query = "

我有一个查询,从数据库中获取列名。这些列名不会因请求或会话而更改。因此,为了避免在每个请求上运行无用的查询,我希望在部署应用程序时运行此查询一次,然后在应用程序的剩余生命周期中将结果保留在列表中,直到再次部署应用程序

如何实现这一点,以便在服务层中使用最终列表

如果有更好的方法,而不是在部署时运行查询,那么请建议其他方法

目前,我有:

public List<String> fetchColNames (String tableName) {
    String query = "SELECT Upper(column_name) FROM information_schema.columns WHERE table_name = ?";
        List<String> cols = getJdbcTemplate().query(query, new Object[] {tableName}, new RowMapper<String>() {
           public String mapRow(ResultSet rs, int rowNum) throws SQLException {
               return rs.getString(1);
           } 
        });
    return cols;
} 
您可以使用以下方法:

当然,您还必须缓存注释:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    <cache:annotation-driven />
</beans>

您可以在可配置bean的方法上使用postconstruct注释,它将在部署应用程序时执行

可能是将列表添加到应用程序上下文?这可能会起作用,但可能是我设置不正确,或者由于我遇到以下错误而缺少某些内容:创建名为“org.springframework.cache.interceptor.CacheInterceptor0”的bean时出错:无法解析对bean“cacheManager”的引用
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    <cache:annotation-driven />
</beans>