java webapp配置的策略 [英] Strategies for java webapp configuration

查看:173
本文介绍了java webapp配置的策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的webapp的一部分涉及上传图像文件。在生产服务器上,文件将需要写入/ somepath_on_production_server / images。对于本地开发,我想将文件写入/ some_different_path / images。



处理这些配置差异的最佳方法是什么?



一个重要的要求是:我根本不想混淆生产服务器,我只是希望能够部署一个war文件并使其工作。所以我不想使用任何需要我混淆环境变量/ classpath / etc的技术。在生产机器上。我很乐意在本地机器上设置它们。



我想象了两种可能的一般方法:



<如果满足某些条件(环境变量/类路径/ etc),则在运行时加载特殊的dev配置文件

  • 在构建过程中翻转开关(maven profile maybe?)


  • 解决方案

    简单的东西,如一个 String 可以在 web.xml 中通过JNDI声明为环境条目。下面是一个名为imagePath的 env-entry 的示例。

     < env-entry> 
    < env-entry-name> imagePath< / env-entry-name>
    < env-entry-value> / somepath_on_production_server / images< / env-entry-value>
    < env-entry-type> java.lang.String< / env-entry-type>
    < / env-entry>要从Java代码访问属性,请执行JNDI查找:

    $ b $($)
    $ b

      //获取JNDI环境的句柄命名上下文
    上下文env =(Context)new InitialContext()。lookup(java:comp / ENV);

    //获取单个值
    String imagePath =(String)env.lookup(imagePath);

    这通常用旧的 ServiceLocator />>

    另一个选择是使用属性文件。



    < hr>

    而处理多个环境的maven方式通常涉及配置文件和过滤(属性文件,甚至是 web.xml )。



    资源< h3>


    Part of my webapp involves uploading image files. On the production server, the files will need to be written to /somepath_on_production_server/images. For local development, I want to write the files to /some_different_path/images.

    What's the best way to handle these configuration differences?

    One important requirement is this: I don't want to have to mess with the production server at all, I just want to be able to deploy a war file and have it work. So I don't want to use any technique which will require me to mess with the environment variables/classpath/etc. on the production machine. I'm fine with setting those on my local machine though.

    I'm imaginine two possible general approaches:

    1. loading a special "dev" configuration file at runtime if certain conditions are met (environment variable/classpath/etc)
    2. flipping a switch during the build process (maven profiles maybe?)

    解决方案

    Simple things like a String can be declared as environment entries in the web.xml and obtained via JNDI. Below, an example with an env-entry named "imagePath".

    <env-entry> 
        <env-entry-name>imagePath</env-entry-name> 
        <env-entry-value>/somepath_on_production_server/images</env-entry-value> 
        <env-entry-type>java.lang.String</env-entry-type> 
    </env-entry>
    

    To access the properties from your Java code, do a JNDI lookup:

    // Get a handle to the JNDI environment naming context
    Context env = (Context)new InitialContext().lookup("java:comp/env");
    
    // Get a single value
    String imagePath = (String)env.lookup("imagePath");
    

    This is typically done in an old fashioned ServiceLocator where you would cache the value for a given key.

    Another option would be to use a properties files.


    And the maven way to deal with multiple environments typically involves profiles and filtering (either of a properties file or even the web.xml).

    Resources

    这篇关于java webapp配置的策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆