I'm a Spring novice user.

I have a database table which is static in nature and contains only a few records.I want a Map-like structure(id - name) that holds two columns of this table. This Map must be loaded/initialized when the web application is started and must be applicable throughout the application's context, independent of the users sessions and must be read-only. This way, I can save a lot of DB queries as the different operations will simply read from this Map.

While I'm aware of ServletContextListener etc. of Java EE, I don't know how to achieve the same in Spring. Is a Spring Service bean the right place/way to initialize and store such a Map?

Please guide me about the same.

有帮助吗?

解决方案

You can create a regular spring bean exposing a method which loads the data you require from the database and stores it in your map. Annotate this method with @PostConstruct and spring will ensure that it is called when your application context starts, hence loading your map.

You could use springs JdbcTemplate to load your data within this method

See Spring PostConstruct doco for information on the @PostConstruct annotation

See JdbcTemplate doco for information on JdbcTemplate

其他提示

You can configure lists, sets and maps in a Spring XML configuration. See here for more examples.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top