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.
- Trunk javadocs:
- Trunk source location:Â https://source.sakaiproject.org/svn/kernel/trunk/component-manager/
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
- Import the cover
import org.sakaiproject.component.cover.ComponentManager;
- 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
- 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)
- 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>
- 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; }
- No extra Maven dependencies or imports are needed when using this method