How to access ComponentRepository


Hello ,

I went through opengamma - really nice product.
I did some code digging and went through the previous posts in the forum.
I have a basic newbie question on it.
In this documentation -
I understand that all the components are initialized and registered to a ComponentManager.
But then i am not able to figure out how to access ComponentManager so that i can use the already registered Component instances.



A quick reply as I’m currently away from the office…

The design of the configuration system was not intended such that the ComponentManager is used as a central lookup object during runtime. Instead, most of the time, you should only use the component manager to register things with during setup.

So, during initialisation, you create a component Foo and register it. Then, when you want to create component Bar that depends on Foo, you inject it in using the Joda-Bean injection method and the INI file ::foo reference. ie, you do not go to the manager and look it up. Note that this is different to how you work with the context in Spring!

This way, the principal usage of the manager is during setup - only low level framework tools (like the RESTful code) use the manager after setup. This results in less coupling between components and forces you as a developer to think more clearly about your components and their dependencies, and also externalises that knowledge into the INI file (rather than being hard coded into a Java based manager lookup).

Having said all that, it should be possible to use a static method on ComponentManager to access the manager at any point in the application. We just don’t recommend that approach.


Okies , so i have to create my own component and add it in the ini file. And during the init() method i need to grab all the instances i need from Component Repo. Got it … Thanks.



Thats the idea, but see other component factories for a detailed example to follow. For example, UserFinanicalPositionSourceComponentFactory.

That component factory pulls in 5 other components:

  private CacheManager _cacheManager;
  @PropertyDefinition(validate = "notNull")
  private PortfolioMaster _underlyingPortfolioMaster;
  @PropertyDefinition(validate = "notNull")
  private PositionMaster _underlyingPositionMaster;
  private PortfolioMaster _userPortfolioMaster;
  private PositionMaster _userPositionMaster;

These are populated by the component system because the factory is a Joda-Bean. That way you never have to manually query the repository, because the components you depend on are injected into the fields.

As you can see in the init method, the repo is never queried for components. Thats because the dependent components were injected.

Saying all that, it is perfectly acceptable to query the repo in the init method. Its just not what we choose to do at OpenGamma - we prefer using the Joda-Bean injection.


Thanks Stephen. I was also looking for the same. Joda-bean is something new to me but then let me start looking into it.