Search code examples
springalfrescocomponent-scan

Alfresco personService.createPerson error when using spring component-scan


Some weeks ago, I developed an AMP based on Alfresco 3.4.14. In that module, I created some customized services. One of the services was used to manage users, with a method to create new users:

public NodeRef createUser(<my_parameters>) {
    ..........................
    ..........................
    HashMap<QName, Serializable> properties = new HashMap<QName, Serializable>();

    properties.put(ContentModel.PROP_USERNAME, username);
    properties.put(ContentModel.PROP_PASSWORD, password);
    ..........................
    ..........................
    NodeRef person = personService.createPerson(properties);
    return person;
}

All services were defined in alfresco context files:

<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="customUserService" class="com.mycustom.service.impl.CustomUsersServiceImpl">
        <property name="personService" ref="PersonService"/>
        <property name="nodeService" ref="NodeService"/>
    </bean>
    <bean id="customProjectsService" class="com.mycustom.service.impl.CustomProjectsServiceImpl">
        <property name="searchService" ref="SearchService"/>
        <property name="nodeService" ref="NodeService"/>
    </bean>
</beans>

Everything was working properly. I was able to create users, execute my other customized services...until here PERFECT!!!!!!!! The application was working properly.


Few days ago, instead of define all the beans in the context, we decided to start to use spring annotations:

package com.mycustom.service.impl;

@Service("customUsersService")
public class CustomUsersServiceImpl implements CustomUsersService {

    @Override
    public NodeRef createUser(<my_parameters>) {
    }
}

And in the context file, to use the spring component-scan:

<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="com.mycustom.service" />
</beans>

All the services were working properly, the application looked to work properly too. We were happy because we could decouple our application, using annotations.

BUTTTTTTTTTTTT, when I tried to create a user, through the explorer or trough the application I got the same error: Failed to create Person due to error: xxxxxxx beforeCreateNode: use PersonService to create person

enter image description here


Solution

  • Debugging PersonServiceImpl class, I detected the error was in the line beforeCreateNodeValidationBehaviour.enable();

    public NodeRef createPerson(Map<QName, Serializable> properties, Set<String> zones) {
        .....................
        .....................
        NodeRef personRef = null;
            try
            {
                beforeCreateNodeValidationBehaviour.disable();
    
                personRef = nodeService.createNode(
                        getPeopleContainer(),
                        ContentModel.ASSOC_CHILDREN,
                        getChildNameLower(userName), // Lowercase:
                        ContentModel.TYPE_PERSON, properties).getChildRef();
            }
            finally
            {
                //ERROR!!!!!!!!!!!!!!!!
                beforeCreateNodeValidationBehaviour.enable();
            }
        .....................
        .....................
    }
    

    After some investigations, when we reverted the code, and remove the spring component-scan, the application was working properly again.

    We opened a ticket to Alfresco support, and they told us, this issue is fixed in alfresco 4.1.4

    Then people who are using a prior version of Alfresco like us, be careful with this and remove the component-scan and replace it with manual beans wiring.