スプリングソース拡張ポイント1:initPropertySourcesメソッド

Springの力は、Java開発者に大きな利便性を提供するだけでなく、そのオープンアーキテクチャにより、ユーザーがSpringを拡張する最大の能力を持つことができることです。

protected void initPropertySources() {
		// For subclasses: do nothing by default.
	}

AbstractApplicationContextクラスには、拡張するサブクラス用に予約されているinitPropertySourcesメソッドがあります。これはrefresh()の最初のメソッドprepareRefresh();で呼び出されます。

protected void prepareRefresh() {
    
    
		// Switch to active.
		// 设置容器启动的时间
		this.startupDate = System.currentTimeMillis();
		// 容器的关闭标志位
		this.closed.set(false);
		// 容器的激活标志位
		this.active.set(true);

		// 记录日志
		if (logger.isDebugEnabled()) {
    
    
			if (logger.isTraceEnabled()) {
    
    
				logger.trace("Refreshing " + this);
			}
			else {
    
    
				logger.debug("Refreshing " + getDisplayName());
			}
		}

		// Initialize any placeholder property sources in the context environment.
		// 留给子类覆盖,初始化属性资源
		initPropertySources();

		// Validate that all properties marked as required are resolvable:
		// see ConfigurablePropertyResolver#setRequiredProperties
		// 创建并获取环境对象,验证需要的属性文件是否都已经放入环境中
		getEnvironment().validateRequiredProperties();

		// Store pre-refresh ApplicationListeners...
		// 判断刷新前的应用程序监听器集合是否为空,如果为空,则将监听器添加到此集合中
		if (this.earlyApplicationListeners == null) {
    
    
			this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
		}
		else {
    
    
			// Reset local application listeners to pre-refresh state.
			// 如果不等于空,则清空集合元素对象
			this.applicationListeners.clear();
			this.applicationListeners.addAll(this.earlyApplicationListeners);
		}

		// Allow for the collection of early ApplicationEvents,
		// to be published once the multicaster is available...
		// 创建刷新前的监听事件集合
		this.earlyApplicationEvents = new LinkedHashSet<>();
	}

したがって、このクラスまたはそのサブクラスを継承して、initPropertySourcesメソッドをオーバーライドし、いくつかの拡張機能を実装できます。

public class MyClassPathXmlApplicationContext extends ClassPathXmlApplicationContext {


    public MyClassPathXmlApplicationContext(String... configLocations){
        super(configLocations);
    }

    @Override
    protected void initPropertySources() {
        System.out.println("扩展initPropertySource");
        //这里添加了一个name属性到Environment里面,以方便我们在后面用到
        getEnvironment().getSystemProperties().put("name","bobo");
        //这里要求Environment中必须包含username属性,如果不包含,则抛出异常
        getEnvironment().setRequiredProperties("username");
    }
}

ここでは、2つの拡張を行いました。
最初に、環境に属性値を追加します。
2番目:必要なシステム属性usernameを設定します。ユーザー名属性が環境に含まれていない場合、システムは例外をスローします。

テストカテゴリ:

public class Test {

    public static void main(String[] args) {
        MyClassPathXmlApplicationContext ac = new MyClassPathXmlApplicationContext("applicationContext.xml");

//        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-${username}.xml");
    }
}

もちろん、他の拡張機能も実行できます。これは単なる例です。

おすすめ

転載: blog.csdn.net/u013277209/article/details/109177452