如何在我从数据库中获取的spring上下文中设置我的结果?

时间:2017-06-28 07:58:57

标签: spring

我从数据库中获取相同的数据但我想将它存储在弹簧上下文中它是如何实现的?

我有一个场景,我每次都会点击数据库并获取相同的结果,所以我如何在spring上下文中添加我的对象并每次从上下文中获取它?

1 个答案:

答案 0 :(得分:0)

你应该使用spring cache api。第一次调用Cacheable方法时,弹簧检查缓存中没有任何内容并执行load方法(并将结果放入缓存中),第二次spring检查是否存在方法调用的结果并从缓存返回resalt不执行该方法。 / p>

以下是示例example from spring web sitebetter example  并api

@Component
public class SimpleBookRepository implements BookRepository {

@Override
@Cacheable("books")
public Book getByIsbn(String isbn) {
    simulateSlowService();
    return new Book(isbn, "Some book");
}

// Don't do this at home
private void simulateSlowService() {
    try {
        long time = 3000L;
        Thread.sleep(time);
    } catch (InterruptedException e) {
        throw new IllegalStateException(e);
    }
}

}

根据您的代码:

@Component
public class MasterDataLoader implements InitializingBean {
    private Set<String> country = new HashSet<String>();

    @Override
    public void afterPropertiesSet() throws Exception {
        country = getknowledgebase();
    }

    @Cacheable("country")
    public Set<String> getknowledgebase() {
        Connection con = null;
        Set<String> country =new HashSet<>();
        try {
            con = ConnectionProvider.getConnection();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM knowledgebase");
            while (rs.next()) {
                if (rs.getString("country") != null) {
                    country.add(rs.getString("country").toLowerCase());
                }
            }
            rs.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return country;
    }
}

- 构造函数它不是你应该做一些逻辑的最佳位置,最好将逻辑移动到某个私有方法中,并从constuctor调用一个。

  • 另外,可能你不需要弹簧缓存这个,我认为设置就足够了。只返回从方法get ....设置coutries,该方法返回县。

更新2: 如果您可以获得所有数据并且他们不及时更改数据库

@Component
public class MasterDataLoader implements InitializingBean {
    private Set<String> country = new HashSet<String>();
    private Set<String> streets = new HashSet<String>();
    private Set<String> state = new HashSet<String>();

    public Set<String> getCountry() {
        return country;
    }

    public Set<String> getStreets() {
        return streets;
    }

    public Set<String> getState() {
        return state;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        loadCachedData();
    }


    private void  loadCachedData() {
        Connection con = null;
        try {
            con = ConnectionProvider.getConnection();
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM knowledgebase");
            while (rs.next()) {
                if (rs.getString("country") != null) {
                    country.add(rs.getString("country").toLowerCase());
                    streets.add(rs.getString("street").toLowerCase());
                    state.add(rs.getString("state").toLowerCase());
                }
            }
            rs.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

如果数据可能会改变

@Cacheable("country")
public Set<String> getCountry() {
    return method that load country from DB;
}

@Cacheable("street")
public Set<String> getStreets() {
    return method that load street from DB;
}

@Cacheable("state")
public Set<String> getState() {
    return method that load state from DB;
}