Skip to content

Commit a27481a

Browse files
committed
Prefer Java configuration over XML.
Closes #347
1 parent 7d48a69 commit a27481a

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

src/main/asciidoc/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Mattias Hellborg Arthursson; Ulrik Sandberg; Eric Dalquist; Keith Barlow; Rob Wi
55
ifdef::backend-epub3[:front-cover-image: image:epub-cover.png[Front Cover,1050,1600]]
66
:spring-data-commons-docs: ../../../../spring-data-commons/src/main/asciidoc
77
:spring-framework-docs: https://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/
8-
8+
:store: Ldap
99

1010
(C) 2008-2022 The original authors.
1111

src/main/asciidoc/reference/ldap-repositories.adoc

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,34 @@ Right now, this interface serves only typing purposes, but we can add additional
6868

6969
.General LDAP repository Spring configuration
7070
====
71-
[source,xml]
71+
.Java
72+
[source,java,role="primary"]
73+
----
74+
@Configuration
75+
@EnableLdapRepositories("com.acme.*.repositories")
76+
class MyConfig {
77+
78+
@Bean
79+
ContextSource contextSource() {
80+
81+
LdapContextSource ldapContextSource = new LdapContextSource();
82+
83+
ldapContextSource.setUserDn("cn=Admin");
84+
ldapContextSource.setPassword("secret");
85+
ldapContextSource.setUrl("ldap://127.0.0.1:389");
86+
87+
return ldapContextSource;
88+
}
89+
90+
@Bean
91+
LdapTemplate ldapTemplate(ContextSource contextSource) {
92+
return new LdapTemplate(contextSource);
93+
}
94+
}
95+
----
96+
97+
.XML
98+
[source,xml,role="secondary"]
7299
----
73100
74101
<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
94121
----
95122
====
96123

97-
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.
98-
99-
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:
124+
This configuration causes the base packages to be scanned for interfaces that extend `LdapRepository` and create Spring beans for each one found.
125+
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.
100126

101-
.Java configuration for repositories
102-
====
103-
[source,java]
104-
----
105-
@Configuration
106-
@EnableLdapRepositories
107-
class ApplicationConfig {
108-
109-
@Bean
110-
ContextSource contextSource() {
111-
112-
LdapContextSource ldapContextSource = new LdapContextSource();
113-
ldapContextSource.setUrl("ldap://127.0.0.1:389");
114-
115-
return ldapContextSource;
116-
}
117-
118-
@Bean
119-
LdapTemplate ldapTemplate(ContextSource contextSource) {
120-
return new LdapTemplate(contextSource);
121-
}
122-
}
123-
----
124-
====
127+
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.
125128

126129
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.
127130

@@ -131,14 +134,14 @@ We can add paging access to our repository, as follows:
131134
====
132135
[source,java]
133136
----
134-
@RunWith(SpringJUnit4ClassRunner.class)
137+
@ExtendWith({SpringExtension.class})
135138
@ContextConfiguration
136-
public class PersonRepositoryTests {
139+
class PersonRepositoryTests {
137140
138141
@Autowired PersonRepository repository;
139142
140143
@Test
141-
public void readAll() {
144+
void readAll() {
142145
143146
List<Person> persons = repository.findAll();
144147
assertThat(persons.isEmpty(), is(false));
@@ -158,7 +161,7 @@ Most of the data access operations you usually trigger on a repository result in
158161
====
159162
[source,java]
160163
----
161-
public interface PersonRepository extends PagingAndSortingRepository<Person, String> {
164+
interface PersonRepository extends PagingAndSortingRepository<Person, String> {
162165
163166
List<Person> findByLastname(String lastname); <1>
164167

src/test/java/org/springframework/data/ldap/repository/SimpleLdapRepositoryTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class SimpleLdapRepositoryTests {
6060
@BeforeEach
6161
void prepareTestedInstance() {
6262
tested = new SimpleLdapRepository<>(ldapOperationsMock, odmMock, Object.class);
63+
64+
6365
}
6466

6567
@Test

0 commit comments

Comments
 (0)