How do I "register" a service in a Craft 3 plugin? In Craft 2, services were auto-loaded when using the craft()-&gt;plugin_serviceName-&gt;myMethodName() signature, but it seems like there's a catch in Craft 3, when using the new MyPlugin::getInstance()-&gt;myServiceName-&gt;myMethod().

I'm working on a business-logic plugin using a local path repository in my project's composer.json, and there are a rash of problems that come of this, mainly to do with creating + renaming services, as the service locator seems to cache them pretty aggressively.

How can I ensure these changes are picked up right away?
It turns out there's a handy method for manually registering your services, or in Yii2 parlance, just "components":

namespace example\myplugin;

use example\myplugin\services\Example as ExampleService;

use Craft;
use craft\base\Plugin as BasePlugin;

class MyPlugin extends BasePlugin
{
    public static $plugin;

    public function init()
    {
        parent::init();
        self::$plugin = $this;

        $this-&gt;setComponents([
            'example' =&gt; ExampleService::class
        ]);
    }
}


Then, somewhere else in your plugin, your service is immediately available as:

MyPlugin::$plugin-&gt;example-&gt;myMethod();


…where example is the key in the hash of classes you passed to $this-&gt;setComponents().