【问题标题】:How to Autowire LdapRepository in Spring?如何在 Spring 中自动装配 LdapRepository?
【发布时间】:2018-10-29 07:21:17
【问题描述】:

我正在尝试使用 Spring 的自动配置来启动嵌入式 LDAP 服务器并使用 spring-data-ldap 访问它。但是,自动装配的字段、存储库(LdapRepository 的实例)和 ldapTemplate(LdapTemplate 的实例)为空。

例如,

spring.ldap.model.UserTest > testSaveUser FAILED
    java.lang.NullPointerException at UserTest.java:32

我错过了什么?

build.gradle

plugins {
  id 'org.springframework.boot' version '2.0.6.RELEASE' apply false
}

apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'

repositories {
  jcenter()
}

dependencyManagement {
  imports {
    mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
  }
}

dependencies {
  compile 'org.springframework.data:spring-data-ldap'
  compile 'com.unboundid:unboundid-ldapsdk'
  testCompile 'org.springframework.boot:spring-boot-starter-data-ldap'
  testCompile 'org.springframework.boot:spring-boot-starter-test'
}

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

src/main/java/spring/ldap/model下:

Config.java

包spring.ldap.model;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.ldap.repository.config.EnableLdapRepositories;

@Configuration
@EnableLdapRepositories
public class Config { }

用户.java

package spring.ldap.model;

import javax.naming.Name;

import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.DnAttribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;

@Entry(objectClasses = { "person", "top" }, base="dc=spring,dc=ldap,dc=model" )
public class User {
  @Id private Name dn;
  @Attribute(name = "cn") @DnAttribute(value = "cn", index = 0) private String userName;
  @Attribute(name = "sn") private String fullName;

  public Name getDn() { return dn; }
  public void setDn(Name dn) { this.dn = dn; }

  public String getUsername() { return userName; }
  public void setUsername(String userName) { this.userName = userName; }

  public String getFullName() { return fullName; }
  public void setFullName(String fullName) { this.fullName = fullName; }
}

UserRepository.java

package spring.ldap.model;

import org.springframework.data.ldap.repository.LdapRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends LdapRepository<User> { }

src/test/java/spring/ldap/model下:

UserTest.java

package spring.ldap.model;

import static org.junit.Assert.assertNotNull;

import javax.naming.Name;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.ldap.DataLdapTest;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.support.LdapNameBuilder;
import org.springframework.test.context.ContextConfiguration;

@DataLdapTest()
@ContextConfiguration(classes = Config.class)
public class UserTest {
  @Autowired UserRepository repository;
  @Autowired LdapTemplate ldapTemplate;

  @Before
  public void SetUp() {
    LdapNameBuilder builder = LdapNameBuilder.newInstance();
    builder.add("dc", "model");
    builder.add("dc", "ldap");
    builder.add("dc", "spring");
    Name dn = builder.build();
    DirContextAdapter context = new DirContextAdapter(dn);
    context.setAttributeValues("objectclass", new String[] { "top", "domain", "extensibleObject" });
    context.setAttributeValue("dc", "spring");
    ldapTemplate.bind(context);
  }

  @Test public void testSaveUser() {
    User user = new User();
    user.setUsername("ncornish");
    user.setFullName("Nicholas Cornish");

    repository.save(user);

    assertNotNull("Id was not generated!", user.getDn());
  }
}

在 src/test/resources 下:

application.properties

spring.ldap.embedded.base-dn=dc=spring,dc=ldap,dc=model
spring.ldap.embedded.credential.username=uid=admin
spring.ldap.embedded.credential.password=secret

【问题讨论】:

  • 您的测试没有运行到 Spring 上下文中,因为它没有使用 @RunWith(SpringRunner.class) 进行注释。所以没有什么东西永远不会自动装配里面的任何东西。

标签: spring spring-data spring-ldap


【解决方案1】:

必须将这些注释放在测试类上下文中:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { UserRepository.class, LdapTemplate.class })
@ContextConfiguration(classes = Config.class)
@EnableConfigurationProperties

亲切的问候。

【讨论】:

    【解决方案2】:

    感谢 JB Nezit,您的评论

    您的测试没有运行到 Spring 上下文中,因为它没有注释 与@RunWith(SpringRunner.class)。所以没有什么会自动接线 里面的任何东西。

    解决了这个问题。 IE。将以下内容添加到测试类中

    @RunWith(SpringRunner.class)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-18
      • 2017-10-09
      • 1970-01-01
      • 2022-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      相关资源
      最近更新 更多