英文:
Java/JPA class for storing application settings
问题
如何在我的Java Servlet应用程序中使用JPA来创建一个名为Settings
的类?我在考虑创建一个静态类,其中包含一个用于存储应用程序设置(如电子邮件服务器地址等)的键值对映射。类似于以下内容:
public class ApplicationSettings {
private static Map<String, String> settings;
注意:由于您要求只返回翻译好的部分,我已经按照您的要求进行了处理。如有其他需要,请随时提问。
英文:
How can I use JPA to create a Settings
class for my Java Servlet application? I'm thinking of something like a static class with a map of key/value pairs for storing the application settings like e-mail server address etc. Something like this:
public class ApplicationSettings {
private static Map<String, String> settings;
答案1
得分: 1
对于基于Spring的应用程序
您可以使用缓存来存储从数据库中获取的属性。您可以使用 @Cacheable("properties")
来调用数据库服务并将其加载到缓存中。如果您想要更新、删除或添加新的属性,您可以使用 @CacheEvict(value = "properties", allEntries = true)
,其中您可以调用数据库服务来执行实际操作。@CacheEvict
将清除所有与 properties
键关联的现有缓存,并通过隐式调用 @Cacheable("properties")
来加载新的属性。
@Repository
public class ApplicationSettings {
private DatabaseService databaseService;
public ApplicationSettings(DatabaseService databaseService) {
this.databaseService = databaseService;
}
@Cacheable("properties")
public Map<String, String> getAppProperties() {
return databaseService.getAppProperties();
}
@CacheEvict(value = "properties", allEntries = true)
public void updateAppProperties(String key, String value) throws IOException {
databaseService.updateAppProperties(key, value);
}
}
现在,您可以在需要使用属性的地方使用 ApplicationSettings
,例如:
@Autowired
private ApplicationSettings applicationSettings;
// ...
Map<String, String> appProperties = applicationSettings.getAppProperties();
对于基于Servlet的应用程序
您可以使用 EhCache 或者简单地使用 Servlet 监听器来实现上述情景。
在 web.xml
中:
<web-app ....>
<listener>
<listener-class>
com.listernerpackage.ApplicationInitializationListener
</listener-class>
</listener>
:
</web-app>
创建一个 ServletContextListener
:
public class ApplicationInitializationListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
Map<String, String> properties = someDbManager.getAppProperties();
context.setAttribute("properties", properties );
}
}
您可以通过调用以下方式在 Servlet 中检索属性:
Map<String, String> properties = (Map<String, String>) this.getContext().getAttribute("properties");
英文:
For Spring based application
You can use Caching for storing the properties from database. You can use @Cacheable("properties")
for calling the database service and load it in the cache. If you want to update, delete or add new property you can use @CacheEvict(value = "properties", allEntries = true)
where you can call the database service to use do the actual operation. @CacheEvict
will clear all the existing cache mapped for properties
key and loads the new properties by calling @Cacheable("properties")
implicitly
@Repository
public class ApplicationSettings {
private DatabaseService databaseService;
public ApplicationSettings(DatabaseService databaseService) {
this.databaseService = databaseService;
}
@Cacheable("properties")
public Map<String, String> getAppProperties() {
return databaseService.getAppProperties();
}
@CacheEvict(value = "properties", allEntries = true)
public void updateAppProperties(String key, String value) throws IOException {
databaseService.updateAppProperties(key, value);
}
}
You can now use ApplicationSettings
wherever want can the properties like
@Autowired
private ApplicationSettings applicationSettings;
:
:
Map<String, String> appProperties = applicationSettings.getAppProperties();
For Servlet based application
You can use EhCache or simply use Sevlet Listener for achieving the above scenario
in web.xml
<web-app ....>
<listener>
<listener-class>
com.listernerpackage.ApplicationInitializationListener
</listener-class>
</listener>
:
Create a ServletContextListener
public class ApplicationInitializationListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
Map<String, String> properties = someDbManager.getAppProperties();
context.setAttribute("properties", properties );
}
}
You can retrieve the properties in servlet by calling
Map<String, String> properties = (Map<String, String>) this.getContext().getAttribute("properties");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论