How can an extension do a custom initialization on Magento start?

I use such initialization to load my custom mini-libraries with short popular global functions (rm_customer_logged_in() for example).

<type name='\Magento\Framework\AppInterface'>
	<plugin
		name='Df\Framework\AppInterfacePlugin'
		type='Df\Framework\AppInterfacePlugin'
	/>
</type>
<?php
namespace Df\Framework;
use \Magento\Framework\AppInterface;
class AppInterfacePlugin {
	/**
	 * @see \Magento\Framework\AppInterface::launch()
	 * @param AppInterface $subject
	 * @return array
	 */
	public function beforeLaunch(AppInterface $subject) {
		// Place your custom initialization code here.
		return [];
	}
}

This does not work for console applications (like setup:upgrade).
For console applications I use event handlers:

<event name='store_collection_load_before'>
	<observer
		name='Df\Core\Boot'
		instance='Df\Core\Boot'
		method='storeCollectionLoadBefore'
	/>
</event>
<?php
namespace Df\Core;
use \Magento\Framework\Event\Invoker\InvokerDefault;
use \Magento\Framework\Event\Observer as _O;
class Boot {
	/**
	 * @used-by InvokerDefault::_callObserverMethod()
	 * @param _O $o
	 * @return void
	 */
	public function storeCollectionLoadBefore(_O $o) {
		if (!self::$_done && 'cli' === PHP_SAPI) {
			self::run();
		}
	}
	// other code here...
}

Magento 2 event handling has been significantly changed last days: How to handle events

Now an extension can do a custom initialization just in its registration.php file.