Results 1 to 10 of 10

Thread: ldaptemplate returns null

  1. #1
    Join Date
    Feb 2011
    Location
    Leeuwarden, Netherlands
    Posts
    8

    Default ldaptemplate returns null

    Hi, I'm new to Spring LDAP, and having a bit of configuration problems;

    First I tried to setup SpringLDAP using Autowire
    applicationContect
    Code:
    <?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"
    	xmlns:p="http://www.springframework.org/schema/p" 
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	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
    			http://www.springframework.org/schema/mvc 
    			http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    	
    	<context:annotation-config />
    	<context:component-scan base-package="countrycerts"/>
    	
    	<mvc:annotation-driven /> 
    	
    	<context:property-placeholder location="classpath:config.properties" />
    	
        <bean id="messageSource" 
        	class="org.springframework.context.support.ResourceBundleMessageSource"
        	p:basename="messages"/>
    	    
        <import resource="persistence.xml"/>
        <import resource="ldap.xml"/>
    </beans>
    ldap.xml
    Code:
    <?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.xsd">
    
    	<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
            <property name="contextSource" ref="contextSource" />
        </bean>
    		   
        <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
            <property name="url" value="ldap://localhost:10389" />
            <property name="userDn" value="uid=admin,ou=system" />
            <property name="password" value="d3v3l0p3r" />
            <property name="pooled" value="false" />
        </bean>
        
        <bean id="ldapBean" class="countrycerts.beans.LDAPBean">
        	<property name="ldapTemplate" ref="ldapTemplate"/>
        </bean>
    </beans>
    But the DI failed, or what i'm currently thinking that its value was reset. I tried the "old way" of hardwiring the dependencies, and i can see the dependency being set,

    Code:
    set LDAP template: org.springframework.ldap.core.LdapTemplate@5010f992
    but after initialisation the call fails on a null ldaptemplate

    Code:
    ldapTemplate:null
    LDAPBean.java
    Code:
    package countrycerts.beans;
    
    import java.util.HashSet;
    import java.util.Set;
    
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import javax.naming.directory.Attributes;
    import javax.naming.directory.BasicAttribute;
    import javax.naming.directory.BasicAttributes;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.ldap.core.DistinguishedName;
    import org.springframework.ldap.core.LdapTemplate;
    import org.springframework.ldap.filter.AndFilter;
    import org.springframework.ldap.filter.EqualsFilter;
    import org.springframework.stereotype.Service;
    
    import countrycerts.ldap.User;
    import countrycerts.ldap.UserAttributesMapper;
    
    @ManagedBean
    @SessionScoped
    @Service
    public class LDAPBean {
    	
    	//@Autowired
    	private LdapTemplate ldapTemplate;
    	
    	public void setLdapTemplate(LdapTemplate template) {
    		System.out.println("set LDAP template: " + template);
    		this.ldapTemplate = template;
    	}
    	
    	public void getUsers() {
    		System.out.println("ldapTemplate:" + this.ldapTemplate);
    		
    		add("New User", "User", "12345");
    		
    		Set<User> users = getAllUsers("David");
    		for (User user: users){
    			System.out.println(user.getCommonName() + "-" + user.getTelephone());
    		}
    	}
    	
    	@SuppressWarnings("unchecked")
    	public Set<User> getAllUsers(String surName){
    				
    		UserAttributesMapper mapper = new UserAttributesMapper();
    		
    		AndFilter filterObject = new AndFilter();
    		filterObject.and(new EqualsFilter("objectClass", "person"));
    		filterObject.and(new EqualsFilter("sn", surName));
    		
    		return new HashSet<User>(
    			this.ldapTemplate.search("ou=users,ou=system", filterObject.encode(), mapper));
    	}
    	
    	public void add(String commonName, String surName, String telephone){
    		
    		String baseDn = "ou=users,ou=system";
    		DistinguishedName distinguisedName = new DistinguishedName(baseDn);
    		distinguisedName.add("cn", commonName);
    			
    		Attributes userAttributes = new BasicAttributes();
    		userAttributes.put("sn", surName);
    		userAttributes.put("telephoneNumber", telephone);
    		
    		BasicAttribute classAttribute = new BasicAttribute("objectclass");
    		classAttribute.add("top");
    		classAttribute.add("person");		
    		userAttributes.put(classAttribute);
    		
    		this.ldapTemplate.bind(distinguisedName, null, userAttributes);
    	}
    }
    Am I missing something in my configuration or class?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Judging by this you are trying to use it as both a spring bean and JSF bean... The jsf bean isn't a spring bean and as such is a new instance without anything injected. If you want to use JSF with Spring I suggest a read of the reference guide about the JSF integration.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Feb 2011
    Location
    Leeuwarden, Netherlands
    Posts
    8

    Default

    Quote Originally Posted by Marten Deinum View Post
    Judging by this you are trying to use it as both a spring bean and JSF bean... The jsf bean isn't a spring bean and as such is a new instance without anything injected. If you want to use JSF with Spring I suggest a read of the reference guide about the JSF integration.
    Hi Marten,

    By using the @Service and @Component annotations I can use Spring managed beans as JSF Beans. I do this all the time. If you look at the previous post you can see from the console output that the ldapTemplate is assigned, however when a call from JSF is being handled the ldapTemplate is null, despite the @SessionScoped tag

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    By using the @Service and @Component annotations I can use Spring managed beans as JSF Beans.
    No you cannot.. JSF will create a new instance and not reuse the one spring has defined you will end up with 2 instances, one JSF managed and one Spring managed.

    As stated read the reference guide if you want to use Spring manged bean as or in jsf managed beans.

    you can see from the console output that the ldapTemplate is assigned
    Which is the spring managed one NOT the jsf managed one.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Feb 2011
    Location
    Leeuwarden, Netherlands
    Posts
    8

    Default

    I don't understand.. I have a similar DAO approach in my datamodel in which Hibernate also has an Autowired session factory. This works like a charm. Are you sure the beans really get duplicated for Spring / JSF use? I've googled around a bit, but I couldn't find anything useful, do you have any links?

    http://stackoverflow.com/questions/4...-managed-beans

    Code:
    @ManagedBean
    @SessionScoped
    @Component
    public class CountryBean {
    	
    	@Autowired
    	private WorldpartsDAO worldpartDAO;
    
            public List<Worldparts> getWorldParts() {
    		return worldpartDAO.findAll();
    	}
    Code:
    package countrycerts.model.dao;
    
    import countrycerts.model.domain.Worldparts;
    
    public interface WorldpartsDAO extends GenericDAO<Worldparts, Integer> {
    
    }
    Code:
    @Repository
    public class WorldPartsDAOImpl extends GenericDAOImpl<Worldparts, Integer>
    		implements WorldpartsDAO {

    Code:
    @Repository
    public abstract class GenericDAOImpl<T, ID extends Serializable> implements
    		GenericDAO<T, ID> {
    
    	@Autowired
    	private SessionFactory factory;

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    I'm sure... As I mentioned read the reference guide that has a section on how to integrate spring with jsf.

    If you don't believe me print-out the hashcode for the LDAPBean in your system.out if they are the same the hashcode is equal.
    Last edited by Marten Deinum; Jun 8th, 2012 at 06:35 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Feb 2011
    Location
    Leeuwarden, Netherlands
    Posts
    8

    Default

    sorry, but you're not helping in any way.. my configuration is just fine

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    No it isn't... JSF doesn't know anything about spring and as such will not do anything with @Autowired or whatever as it simply doesn't know it.

    I pointed you to the section in the reference guide which has the information on how to integrate JSF with Spring, that should help you.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Feb 2011
    Location
    Leeuwarden, Netherlands
    Posts
    8

    Default

    I found my problem... and no it wasnt my jsf / spring managed bean

    You know what really was the reason for the problem above?

    The classname LDAPBean !! Refactored it to LdapBean and now @autowiring works like a charm

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Hmm not sure how that can be but if it works it works. I still think you have multiple instances of your bean but that is just me and my JSF experience with Spring (without a ELResolver JSF doesn't resolve spring beans).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •