Magento 2- How to call an event for observer

Magento 2- How to call an event for observer-

Step 1:Create events.xml file, and use this code-

Path– app\code\Vendor\Modulename\etc


<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_catalog_product_save_entity_after">
<observer name="Sample_Demo_Observer" instance="Sample\Demo\Model\Observer" />
</event>
</config>

Step 2:Create observer.php file, and use this code-


<?php 
namespace Vendor\Namespace\Model; 
use Magento\Framework\Event\ObserverInterface; 
class Observer implements ObserverInterface 
{ 
/** * @var ObjectManagerInterface */
protected $_objectManager; 
protected $scopeConfig; 
/** * @param \Magento\Framework\ObjectManagerInterface $objectManager * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */ 

public function __construct( 
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, 
\Magento\Framework\ObjectManagerInterface $objectManager 
) 
{ 
$this->_objectManager = $objectManager;
$this->_scopeConfig = $scopeConfig;

/* parent::__construct(
$scopeConfig
);*/
}

/**
* customer register event handler
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{

$product = $observer->getEvent()->getProduct();

echo "
<pre>++";

print_r($product->getData());

echo "</pre>

"

exit();

}

}