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.
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:
@javax.inject.Named
or @org.springframework.stereotype.Component
annotation and place @Inject
or @Autowired
respectively;@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.
This approach uses an Atlassian Spring Scanner.
If your object is not being autowired, you may need to retrieve the component explicitly. This is done using the ContainerManager
.
For example:
1 2SpaceManager spaceManager = (SpaceManager) ContainerManager.getComponent("spaceManager");
Rate this page: