Announcement Announcement Module
Collapse
No announcement yet.
Spring LDAP 2.0.2 - No Injection (or how to inject) - @Autowired Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring LDAP 2.0.2 - No Injection (or how to inject) - @Autowired

    Hi All,

    I am new to Spring LDAP (the version I am using is: 2.0.2) and I have some questions. I am querying Active Directory.

    I am going to post my code and my questions are in my main class - MainLdapTest:
    ldap_config.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"
            xmlns:ldap="http://www.springframework.org/schema/ldap"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                                http://www.springframework.org/schema/beans/spring-beans.xsd
                                   http://www.springframework.org/schema/ldap
                                   http://www.springframework.org/schema/ldap/spring-ldap.xsd">
    
        <!-- Information about this configuration file can be found:
                http://docs.spring.io/spring-ldap/docs/current/reference/#configuration
        -->
        <ldap:context-source id="contextSource"
                             url="ldap://<url>:<port>"
                             base="DC=comp,DC=com"
                             username="<username>"
                             password="<secret>" />
    
        <ldap:ldap-template id="ldapTemplate" context-source-ref="contextSource" />
    
        <bean id="LdapUserDAO" class="com.ldap.dao.LdapUserDAOImpl">
            <property name="ldapTemplate" ref="ldapTemplate" />
        </bean>
    </beans>

    LdapUser.java
    Code:
    @Entry(objectClasses = {"top", "person", "organizationalPerson", "user"})
    public final class LdapUser {
        @Id
        private Name distinguishedName;
        
        @Attribute(name="cn")
        private String cn;
        
        @Attribute(name="description")
        private String description;
        
        @Attribute(name="displayName")
        private String displayName;
    
        @Attribute(name="sAMAccountName")
        private String sAMAccountName;
        
        @Attribute(name="mail")
        private String mail;
        
        @Attribute(name="userPrincipalName")
        private String userPrincipalName;
    
        @Attribute(name="userAccountControl")
        private String userAccountControl;
        // ...
        // getters and setters
        // ...

    LdapUserDAO.java
    Code:
    public interface LdapUserDAO
    {
        public LdapUser create(LdapUser ldapUser);
        public void update(LdapUser ldapUser);
        public void delete(LdapUser ldapUser);
        public LdapUser findByUid(String uid);
        public List<LdapUser> findAll();
        public List<LdapUser> findByLastName(String lastName);
        public List<LdapUser> findBySAMAccountName(String sAMAccountName);
        public List<LdapUser> findDisabledUsers();
        public List<LdapUser> findDisabledUser(LdapUser ldapUser);
        public List<LdapUser> findByObjectClassPerson();
        public void setLdapTemplate(LdapTemplate ldapTemplate);
    }

    LdapUserDAOImpl.java
    Code:
    public class LdapUserDAOImpl implements LdapUserDAO
    {
        @Autowired
        private LdapTemplate localLdapTemplate;
        // ...
        public List<LdapUser> findBySAMAccountName(String sAMAccountName) {
            return localLdapTemplate.find(query().where("sAMAccountName").is(sAMAccountName), LdapUser.class);
        }
        // ...

    MainLdapTest.java
    Code:
    public class MainLdapTest
    {
        @Autowired
        private LdapUserDAO ldapUserDao;
    
        public MainLdapTest() {
            /**
             * The ldap_config.xml above is in my classpath, 
             * but how do I specify that the contents of that file must be used to inject ldapTemplate in this class, or any other class?
             *
             * Below are my tests and their result:
             *
             * Tests
             * 1. This test returns a NullPointerException because ldapTemplate is not set.
             *    Shouldn't it be injected by the "@Autowired" annotation?
             */
            List<LdapUser> queryListError = ldapUserDao.findByObjectClassPerson();
            System.out.println("[ERR] Query returns no values[queryListRetValues]: #[" + queryListError.size() + "]");
            
            /**
             * 2. As I set the LdapContextSource manually, it returns the data searched in the query.
             *
             *    sAMAccountName is the <username>
             */
            LdapContextSource ctxSrc = new LdapContextSource();
            ctxSrc.setUrl("<url>");
            ctxSrc.setBase("<base>");
            ctxSrc.setUserDn("<username>");
            ctxSrc.setPassword("<password>");
            ctxSrc.afterPropertiesSet();
    
            ldapUserDao = new LdapUserDAOImpl(ldapTemplate);
            List<LdapUser> queryListRetValues = ldapUserDao.findBySAMAccountName("<username>");
            System.out.println("[OK] Query returns no values[queryListRetValues]: #[" + queryListRetValues.size() + "]");
        }
    
        public static void main(String[] args)
        {
            MainLdapTest t = new MainLdapTest();
        }
    }
    In fact, my code is based on the one in Spring LDAP - ODM [1]. I have used the "@Autowired" annotation and as I understand the LdapTemplate should be injected into my LdapUserDAOImpl.

    I know there is a basic error here, I just can't find where.

    Links:
    [1] http://docs.spring.io/spring-ldap/do...reference/#odm

    Thanks for your attention and help.

    Regards!
    Last edited by VintuBr; May 29th, 2014, 01:16 PM.
Working...
X