Using the Sakai ComponentManager to access Sakai Services

Information

This explains basic usage of the Sakai ComponentManager. This service is used to get access to other Sakai service. You can typically get access to other Sakai services using Spring Framework semantics directly as well. This is because the Sakai ComponentManager is basically just a thin wrapper around Spring.

Accessing the ComponentManager

NOTE You must use the cover to get the component manager service

  • Note: This is not the recommended method, you should be using Spring to inject Sakai services, but in some cases this is not possible
  1. Import the cover
    import org.sakaiproject.component.cover.ComponentManager;
    
  2. Get access to a Sakai service using the ComponentManager cover
    userDirectoryService = (UserDirectoryService) ComponentManager.get(UserDirectoryService.class);
    
    • You could also access a service bean by name if you know the name but in general it is safer to use the interface for the service as shown
  3. You will need to add a Maven dependency (like this Maven 2 dependency) to your project:
        <dependency>
          <groupId>org.sakaiproject.kernel</groupId>
          <artifactId>sakai-component-manager</artifactId>
          <version>${sakai.version}</version>
          <scope>provided</scope>
        </dependency>
    

Accessing services via Spring

  • You can use the Spring Framework to inject Sakai services into your own services or beans
    Using Spring to get the service for your class (e.g. YourAppClass) (recommended)
  1. Add the UserDirectoryService bean to the bean for YourAppClass
    <bean id="org.sakaiproject.yourapp.logic.YourAppClass"
    		class="org.sakaiproject.yourapp.logic.impl.YourAppClassImpl">
    	<property name="userDirectoryService"
    		ref="org.sakaiproject.user.api.UserDirectoryService" />
    </bean>
    
  2. Add a variable and setter to YourAppClass to use the service in like so:
    private UserDirectoryService userDirectoryService;
    public void setUserDirectoryService(UserDirectoryService userDirectoryService) {
    	this.userDirectoryService = userDirectoryService;
    }
    
  3. No extra Maven dependencies or imports are needed when using this method