0
0

Web 開発再入門 #13 ― 設定ファイル読込み処理

Last updated at Posted at 2024-04-28

Web 開発再入門 #13 ― 設定ファイル読込み処理

fmockup

はじめに

Web アプリケーションのサーバー・サイドを開発します。

Web アプリケーションの動作を設定します。サーバー・サイドのプログラムで、適時、この設定を読み込んでプログラムの動きを変えます。
これらの設定は、Windows サービス化において変更することもできます。

フォルダー・ファイル構成

D:\
└ Developments\
    └ Workspace\
         └ fmockup\
             ├ build\
             ├ sql\
             ├ src\
             │ └ main\
             │     ├ java\
             │     │ └ cn\
             │     │     └ com\
             │     │         └ xxxx\
             │     │             └ fmockup\
             │     │                  ├ action\
             │     │                  ├ controller\
             │     │                  ├ customizer\
             │     │                  ├ entity\
             │     │                  ├ mapper\
             │     │                  ├ response\
             │     │                  ├ service\
             │     │                  ├ util\
             │     │                  │ └ PropertyUtil.java ← コレ
             │     │                  ├ validator\
             │     │                  └ validator_order\
             │     └ resources\
             │          ├ mapper\
             │          ├ templates\
             │          ├ static\
             │          └ application.properties ← コレ
             ├ vue-vite\
             └ WinSW.NET-nnn\

ファイルの文字コード

基本的に Eclipse でファイルを作成するので、あまり意識したことがありません。
多分、Unix 改行(LF)なのだと思います。

ファイルの編集

  1. Eclipse で、ファイル “application.properties” を編集する。
    application.properties
    ・・・
    
    #
    # Application Constants
    #
    
    # program parameter
    property.login-retry-count=5
    property.login-expire-day=30
    property.crypto-key=Asdf1234Asdf1234
    property.idle-timeout=30
    
    # database
    property.db-host=localhost
    property.db-port=3306
    property.db-name=FMOCKUP
    property.db-user=FMOCKUP_USER
    property.db-password=Asdf1234
    
    # directory path
    property.home-directory-path=d:\\systems\\fmockup
    
    #
    # Tomcat
    #
    server.port=8080
    server.tomcat.max-http-header-size=81920
    server.error.whitelabel.enabled=false
    server.error.include-exception=false
    server.error.include-stacktrace=never
    
    # Strictly get the domain name and specify the its domain name.
    # property.host-fqdn=xx.xx.xx.xx:${server.port}
    # Conveniant (for test)
    property.host-fqdn=localhost:${server.port}
    
    #
    # Spring
    #
    spring.main.banner-mode=off
    server.error.path=/error
    server.servlet.context-path=/
    server.servlet.encoding.charset=UTF-8
    
    #
    # Cookie
    #
    server.servlet.session.cookie.path=/
    server.servlet.session.cookie.http-only=true
    server.servlet.session.cookie.secure=true
    
    #
    # Thymeleaf
    #
    spring.thymeleaf.encoding=UTF-8
    spring.thymeleaf.servlet.content-type=text/html
    
    #
    # MySQL
    #
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://${property.db-host}:${property.db-port}/${property.db-name}?allowMultiQueries=true
    spring.datasource.username=${property.db-user}
    spring.datasource.password=${property.db-password}
    
    #
    # Connection Pool
    #
    spring.datasource.validation-query=SELECT 1
    spring.datasource.test-on-borrow=true
    spring.datasource.type=com.zaxxer.hikari.HikariDataSource
    spring.datasource.hikari.pool-name=ConnectionPool
    spring.datasource.hikari.leak-detection-threshold=5000
    spring.datasource.hikari.connection-test-query=SELECT 1
    spring.datasource.hikari.minimum-idle=5
    spring.datasource.hikari.maximum-pool-size=10
    spring.datasource.hikari.auto-commit=false
    
    #
    # Mybatis
    #
    mybatis.configuration.map-underscore-to-camel-case=true
    mybatis.mapper-locations=classpath:mappers/*.xml
    
    #
    # Mybatis Log
    #
    # logging.level.jdbc.sqlonly=info
    logging.level.org.springframework=WARN
    logging.level.cn.com.xxxx.fmockup.mapper=DEBUG
    
    #
    # Logback
    #
    logging.config=classpath:logback-spring.xml
    logging.file.path=${property.home-directory-path}\\log
    spring.profiles.active=production
    

ファイルの作成

  1. Eclipse で、ファイル “PropertyUtil.java” を作成する。
    PropertyUtil.java
    ・・・
    
    package cn.com.xxxx.fmockup.util;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    /**
     * Property File "application.property" Utility
     *   It reads from property file parameters.
     */
    
    @Component
    public class PropertyUtil {
    
    	// Application Values
    
    	private static int idleTimeout;		// minutes
    	private static int loginRetryCount;	// number
    	private static int loginExpireDay;	// day
    	private static String hostFqdn;		//
    
    	private static String cryptoKey;		// must be 16 bytes
    
    	@Value("${property.idle-timeout:30}")
    	public void setIdleTimeout(int timeout) {
    		PropertyUtil.idleTimeout = timeout;
    	}
    
    	public static int getIdleTimeout() {
    		return idleTimeout;
    	}
    
    	@Value("${property.login-retry-count:5}")
    	public void setLoginRetryCount(int loginRetryCount) {
    		PropertyUtil.loginRetryCount = loginRetryCount;
    	}
    
    	public static int getLoginRetryCount() {
    		return loginRetryCount;
    	}
    
    	@Value("${property.login-expire-day:30}")
    	public void setLoginExpireDay(int loginExpireDay) {
    		PropertyUtil.loginExpireDay = loginExpireDay;
    	}
    
    	public static int getLoginExpireDay() {
    		return loginExpireDay;
    	}
    
    	@Value("${property.crypto-key:Asdf1234Asdf1234}")
    	public void setCryptoKey(String cryptokey) {
    		PropertyUtil.cryptoKey = cryptokey;
    	}
    
    	public static String getCryptoKey() {
    		return cryptoKey;
    	}
    
    	@Value("${property.host-fqdn:xx.xx.xx.xx:8080}")
    	public void setHostFqdn(String hostFqdn) {
    		PropertyUtil.hostFqdn = hostFqdn;
    	}
    
    	public static String getHostFqdn() {
    		return hostFqdn;
    	}
    }
    
    設定ファイルの setter と getter を設定します。setter ではパラメーターのデフォルト値を設定し、getter ではパラメーター値を取得します。

その他

設定ファイル読込み処理

設定ファイル読込み処理(中国語での説明、だけどソースコードが参考になる)

アプリケーション・プロパティ設定一覧

Tomcat

Hikari CP

Session(Cookie 含む)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0