Java / JPA类用于存储应用程序设置

huangapple go评论75阅读模式
英文:

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&lt;String, String&gt; 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(&quot;properties&quot;) 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 = &quot;properties&quot;, 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(&quot;properties&quot;) implicitly

@Repository
public class ApplicationSettings {

    private DatabaseService databaseService;

    public ApplicationSettings(DatabaseService databaseService) {
        this.databaseService = databaseService;
    }

    @Cacheable(&quot;properties&quot;)
    public Map&lt;String, String&gt; getAppProperties() {
        return databaseService.getAppProperties();
    }

    @CacheEvict(value = &quot;properties&quot;, 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&lt;String, String&gt; appProperties = applicationSettings.getAppProperties();

For Servlet based application

You can use EhCache or simply use Sevlet Listener for achieving the above scenario

in web.xml

&lt;web-app ....&gt;
  &lt;listener&gt;
    &lt;listener-class&gt;
    com.listernerpackage.ApplicationInitializationListener
    &lt;/listener-class&gt;
  &lt;/listener&gt;
:

Create a ServletContextListener

public class ApplicationInitializationListener implements ServletContextListener {    
    @Override
    public void contextInitialized(ServletContextEvent event) {
        ServletContext context = event.getServletContext();
        Map&lt;String, String&gt; properties = someDbManager.getAppProperties(); 
        context.setAttribute(&quot;properties&quot;, properties );           
    }
}

You can retrieve the properties in servlet by calling

Map&lt;String, String&gt; properties = (Map&lt;String, String&gt;) this.getContext().getAttribute(&quot;properties&quot;);

huangapple
  • 本文由 发表于 2020年7月26日 01:26:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63091363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定