There are three approaches i have used till now for accessing the alfresco services ( Searchservice, NodeService etc) in a class.
1. Accessing through application context.
As mentioned in alfresco documentation and in examples too, we can get the application context object from alfresco utility class 'ApplicationContextHelper'
// Do not forget to include alfresco jars into your project
ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
Now from application context, get the service registry bean having all the registered services for alfresco, like this
ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
And from this service registry object you can get any alfresco services, for e.g like this
SearchService searchService = serviceRegistry.getSearchService();
NodeService nodeService = serviceRegistry.getNodeService();
OR
You can get it by following these steps also
ApplicationContext appContext = new ClassPathXmlApplicationContext("alfresco/application-context.xml");
ServiceRegistry registry = (ServiceRegistry)appContext.getBean(ServiceRegistry.SERVICE_REGISTRY);
2. Getting services from repository directly
SearchService searchService = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getSearchService();
3. Setting services into properties in faces context and accessing it directly just by declaring getters and setters
a. Register your bean into faces-config-custom.xml
<managed-bean>
<description>{Bean Description You want to Provide}</description>
<managed-bean-name>{Bean Name}</managed-bean-name>
<managed-bean-class>
{Package Name.Bean Name}
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>searchService</property-name>
<value>#{SearchService}</value>
</managed-property>
</managed-bean>
b. Declare and write the getters and setters for the properties you have declared and want to use in your class
public class TestClass
{
private SearchService searchService;
/** Your code */
public SearchService getSearchService() {
return searchService;
}
public void setSearchService(SearchService searchService) {
this.searchService = searchService;
}
}