Last updated Apr 2, 2024

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
@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.

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: