I can see that I can do component scan and restrict what packages are scanned using XML file.
<beans>
<context:component-scan base-package="com.mycompany"/>
</beans>
How can I do the same in YML file?
YML is just to load properties, refer to this Spring documentation.
24.6 Using YAML instead of Properties YAML is a superset of JSON, and as such is a very convenient format for specifying hierarchical configuration data. The SpringApplication class will automatically support YAML as an alternative to properties whenever you have the SnakeYAML library on your classpath.
This is also explained here.
You can also use YAML ('.yml') files as an alternative to '.properties'.
So you cannot define/configure beans using YML.