Project configs for Java projects

How to store configs for project in Java. In normally case i stored it in ‘src/main/resources/’ with *.properties. How best approach for storing projects config for development, local, production environments?

I tried use *.prop with follow structure

resources
|_ config
   |_local
   |_development
   |_production
   |_environment.properties

I want avoid it structure.

0

In my humble opinion, the best approach here is to have an internal and external configuration. The internal configuration contains all your defaults and will be contained in your resource folder. The external configuration is a properties file deployed with your program that gets read afterwards and overrides any defaults.

For testing, it is nice since you already have everything you need to run the program without any additional configuration. In production, you can override one or more values as you need to without rewriting the program. See below for an example on how to load the configuration:

private static Properties getConfiguration() throws IOException {
    final Properties configProperties = new Properties();

    // Attempt to read internal configuration
    InputStream configStream = null;
    try {
        configStream = Main.class.getResourceAsStream("/configuration.properties");
        if(configStream != null) {
            configProperties.load(configStream);
        }
    } finally {
        if(configStream != null) {
            configStream.close();
        }
        configStream = null;
    }

    // Attempt to read external configuration (overriding as necessary)
    try {
        final File configFile = new File("configuration.properties");
        if(configFile.isFile()) {
            configStream = new FileInputStream(configFile);

            configProperties.load(configStream);
        }
    } finally {
        if(configStream != null) {
            configStream.close();
        }
    }
    return configProperties;
}

Be sure to adjust the paths as necessary, though I personally prefer to keep the main configuration in an obvious spot both in my resources class path as well as externally.

Note that depending on the information contained in the configuration, you may want to purposefully exclude some information from your internal configuration. An example might be information to connect to a database. In such a case, when testing, you provide also the external configuration yourself that has all the missing database connection info. When you deploy, you omit the external configuration and create a new one for your client (best practice dictates that you would check for the existence of these parameters for good measure, and throw an appropriate exception if missing).

Hope that helps!

2

Based on my experience, it’s a normal way to keep several configuration/application properties files in the repo, but remove redundant ones during building a deliverable.

Also I would like to add that it’s better to separate those two things: application properties and application configuration.

The first one can be used for keeping a deeply internal application setup, e.g.: number of available threads, number of retries etc. The application properties file is necessary part of application. Normally it’s located in the directory <project_name>/src/main/resources.

The application configuration is more client-side thing. Usually it’s used to keep configuration which could/should be changed by customer/user. In other words, that file usually contains configuration, which related to business logic of the application. Almost always that file is required, but some applications can use an internal default configuration in order to provide high availability and not rely on a configuration file only. It’s OK to put a configuration file neither in the project root folder or into a special directory: <project_name>/conf.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật