JAva静的コードブロックやspring@valueなどの注記注入順序


今日ymlプロファイルを参照するときは、継承された静的コードブロックが使用されているためです.クラスとクラスが継承関係にある静的コードブロックは、親静的コードブロックを先に実行してからサブクラス静的コードブロックを実行する問題は言うまでもない.
今日探求するのはspringに関する実行順序です
プロジェクトが開始されると、サブクラスの静的コードブロックで使用する必要があるプロファイルの一部をロードする必要があります.プロジェクトを実行するとき、サブクラスは親の属性を参照して空のポインタを常に報告します.
親コード:
package com.pinyu.system.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;

import com.pinyu.system.web.controller.AppInfoController;

/** 
* @author ypp
*     :2018 12 11    10:12:11 
* @Description: TODO(            ) 
*/
@Component
@SpringBootConfiguration
@PropertySource("classpath:application.yml")
public class YmlPropertiesUtils {
	
	private static Logger logger = LogManager.getLogger(YmlPropertiesUtils.class);

	@Value("${spring.profiles.active}")
	protected static String profilesActive;

	public static String getProfilesActive() {
		return profilesActive;
	}
	
}

サブクラスコード:
package com.pinyu.system.global.config.quartz;

import java.io.IOException;
import java.util.Properties;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.pinyu.system.global.GlobalConstants;
import com.pinyu.system.utils.YmlPropertiesUtils;

/**
 * @author ypp     :2018 12 11    10:03:52
 * @Description: TODO(            )
 */
public class QuartzPropertiesUtils extends YmlPropertiesUtils {

	private static Properties quartzProperties;
	
	private static ClassPathResource quartzClassPathResource;

	static {
		try {
			if (profilesActive.equals(GlobalConstants.PROFILE_ACTIVE_DEV)) {
				quartzClassPathResource = new ClassPathResource("/quartz.properties");
			} else if(profilesActive.equals(GlobalConstants.PROFILE_ACTIVE_TEST)){
				quartzClassPathResource = new ClassPathResource("/test/quartz-test.properties");
			}
			quartzProperties = PropertiesLoaderUtils.loadProperties(quartzClassPathResource);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static Properties getQuartzProperties() {
		return quartzProperties;
	}

	public static ClassPathResource getQuartzClassPathResource() {
		return quartzClassPathResource;
	}
}

profilesActiveというものを使っている間は、ずっとnullでした.そこで,関連実行の順序を検討した.
java static静的コードブロックは最初に実行され、springになります. 相関注入.このように上記の考え方ではprofilesActiveを取得する価値はありません.静的コードブロックが先にロードされると、プロジェクトの起動がエラーになります.
そこで親クラスも静的コードブロックで取得し,子クラスは親静的コードブロックの後に実行され,自然に取得できる.問題が解決する.
コードは次のとおりです.
package com.pinyu.system.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;

import com.pinyu.system.web.controller.AppInfoController;

/** 
* @author ypp
*     :2018 12 11    10:12:11 
* @Description: TODO(            ) 
*/
//@Component
//@SpringBootConfiguration
//@PropertySource("classpath:application.yml")
public class YmlPropertiesUtils {
	
	private static Logger logger = LogManager.getLogger(YmlPropertiesUtils.class);

//	@Value("${spring.profiles.active}")
	protected static String profilesActive;

	public static String getProfilesActive() {
		return profilesActive;
	}

	static{
		Yaml yaml = new Yaml();
        URL url = YmlPropertiesUtils.class.getClassLoader().getResource("application.yml");
        if (url != null) {
            //  application.yaml        ,     obj,
			try {
//				Object obj= yaml.load(new FileInputStream(url.getFile()));
				Map map =(Map)yaml.load(new FileInputStream(url.getFile()));
				Map spring =(Map)map.get("spring");
				Map profiles =(Map)spring.get("profiles");
				profilesActive =(String) profiles.get("active");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				logger.info("       ");
			}
        }
	}
}

主に知識点、java静的コードブロック、springを記録します. 注記の実行順序