Last updated Mar 28, 2018

How do I get a reference to a component?

Confluence component system is powered by Spring, but we've done a lot of nice things to make it easier for developers to get their hands on a component at any time.

Autowired Objects

If your object is being autowired (for example, another plugin module or a Struts action), the easiest way to access a component is to use constructor injection.

For example, if you need a SpaceManager, you can inject it as follows:

  1. Mark class with @javax.inject.Named or @org.springframework.stereotype.Component annotation and place @Inject or @Autowired respectively;
  2. Mark your field with @ConfluenceImport annotation to let the scanner know that it requires to import it from Confluence;
1 2 3 4 5 6 7 8 9 10 11 @Named public class ExampleClass { @ConfluenceImport private SpaceManager spaceManager; @Inject public void setSpaceManager(SpaceManager spaceManager){ this.spaceManager = spaceManager; } }

You can do above points in any order.

This approach uses an Atlassian Spring Scanner.

Non-Autowired Objects

If your object is not being autowired, you may need to retrieve the component explicitly. This is done using the ContainerManager. For example:

1
2
SpaceManager spaceManager = (SpaceManager) ContainerManager.getComponent("spaceManager");

Rate this page: