Prefer Java configuration over XML.

Closes #347
This commit is contained in:
Mark Paluch
2022-09-28 09:35:52 +02:00
parent 7d48a69fcc
commit a27481af0d
3 changed files with 38 additions and 33 deletions

View File

@@ -5,7 +5,7 @@ Mattias Hellborg Arthursson; Ulrik Sandberg; Eric Dalquist; Keith Barlow; Rob Wi
ifdef::backend-epub3[:front-cover-image: image:epub-cover.png[Front Cover,1050,1600]]
:spring-data-commons-docs: ../../../../spring-data-commons/src/main/asciidoc
:spring-framework-docs: https://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/
:store: Ldap
(C) 2008-2022 The original authors.

View File

@@ -68,7 +68,34 @@ Right now, this interface serves only typing purposes, but we can add additional
.General LDAP repository Spring configuration
====
[source,xml]
.Java
[source,java,role="primary"]
----
@Configuration
@EnableLdapRepositories("com.acme.*.repositories")
class MyConfig {
@Bean
ContextSource contextSource() {
LdapContextSource ldapContextSource = new LdapContextSource();
ldapContextSource.setUserDn("cn=Admin");
ldapContextSource.setPassword("secret");
ldapContextSource.setUrl("ldap://127.0.0.1:389");
return ldapContextSource;
}
@Bean
LdapTemplate ldapTemplate(ContextSource contextSource) {
return new LdapTemplate(contextSource);
}
}
----
.XML
[source,xml,role="secondary"]
----
<beans xmlns="http://www.springframework.org/schema/beans"
@@ -94,34 +121,10 @@ Right now, this interface serves only typing purposes, but we can add additional
----
====
This namespace element causes the base packages to be scanned for interfaces that extend `LdapRepository` and create Spring beans for each one found. By default the repositories get an autowired `LdapTemplate` Spring bean that is called `ldapTemplate`, so you only need to configure `ldap-template-ref` explicitly if you deviate from this convention.
This configuration causes the base packages to be scanned for interfaces that extend `LdapRepository` and create Spring beans for each one found.
By default, the repositories get an autowired `LdapTemplate` Spring bean that is called `ldapTemplate`, so you only need to configure `ldap-template-ref` explicitly if you deviate from this convention.
If you want to go with Java configuration, use the `@EnableLdapRepositories` annotation. The annotation carries the same attributes as the namespace element. If no base package is configured, the infrastructure scans the package of the annotated configuration class. The following example shows how to set up Java configuration:
.Java configuration for repositories
====
[source,java]
----
@Configuration
@EnableLdapRepositories
class ApplicationConfig {
@Bean
ContextSource contextSource() {
LdapContextSource ldapContextSource = new LdapContextSource();
ldapContextSource.setUrl("ldap://127.0.0.1:389");
return ldapContextSource;
}
@Bean
LdapTemplate ldapTemplate(ContextSource contextSource) {
return new LdapTemplate(contextSource);
}
}
----
====
If you want to go with Java configuration, use the `@EnableLdapRepositories` annotation. The annotation carries the same attributes as the namespace element. If no base package is configured, the infrastructure scans the package of the annotated configuration class.
Because our domain repository extends `CrudRepository`, it provides you with CRUD operations as well as methods for access to the entities. Working with the repository instance is a matter of dependency injecting it into a client.
@@ -131,14 +134,14 @@ We can add paging access to our repository, as follows:
====
[source,java]
----
@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith({SpringExtension.class})
@ContextConfiguration
public class PersonRepositoryTests {
class PersonRepositoryTests {
@Autowired PersonRepository repository;
@Test
public void readAll() {
void readAll() {
List<Person> persons = repository.findAll();
assertThat(persons.isEmpty(), is(false));
@@ -158,7 +161,7 @@ Most of the data access operations you usually trigger on a repository result in
====
[source,java]
----
public interface PersonRepository extends PagingAndSortingRepository<Person, String> {
interface PersonRepository extends PagingAndSortingRepository<Person, String> {
List<Person> findByLastname(String lastname); <1>

View File

@@ -60,6 +60,8 @@ class SimpleLdapRepositoryTests {
@BeforeEach
void prepareTestedInstance() {
tested = new SimpleLdapRepository<>(ldapOperationsMock, odmMock, Object.class);
}
@Test