Skip to content

Commit ece9287

Browse files
committed
Prefer Java configuration over XML.
Closes #1308
1 parent dac7eb2 commit ece9287

File tree

8 files changed

+161
-72
lines changed

8 files changed

+161
-72
lines changed

spring-data-cassandra-distribution/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@
112112
<enabled>false</enabled>
113113
</snapshots>
114114
</repository>
115+
<repository>
116+
<id>spring-release</id>
117+
<name>Spring Release</name>
118+
<url>https://repo.spring.io/release</url> <!-- For Releases -->
119+
<snapshots>
120+
<enabled>false</enabled>
121+
</snapshots>
122+
</repository>
115123
</repositories>
116124

117125
<pluginRepositories>
@@ -134,6 +142,14 @@
134142
<enabled>false</enabled>
135143
</snapshots>
136144
</pluginRepository>
145+
<pluginRepository>
146+
<id>spring-release</id>
147+
<name>Spring Release</name>
148+
<url>https://repo.spring.io/release</url> <!-- For Releases -->
149+
<snapshots>
150+
<enabled>false</enabled>
151+
</snapshots>
152+
</pluginRepository>
137153
</pluginRepositories>
138154

139155
</project>

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/example/KeyspacePopulatorConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.cassandra.example;
1717

1818
import org.springframework.context.annotation.Configuration;
19+
import org.springframework.core.io.ClassPathResource;
1920
import org.springframework.data.cassandra.config.AbstractCassandraConfiguration;
2021
import org.springframework.data.cassandra.core.cql.session.init.KeyspacePopulator;
2122
import org.springframework.data.cassandra.core.cql.session.init.ResourceKeyspacePopulator;
@@ -28,7 +29,8 @@ public class KeyspacePopulatorConfiguration extends AbstractCassandraConfigurati
2829
@Nullable
2930
@Override
3031
protected KeyspacePopulator keyspacePopulator() {
31-
return new ResourceKeyspacePopulator(scriptOf("CREATE TABLE my_table …"));
32+
return new ResourceKeyspacePopulator(new ClassPathResource("com/foo/cql/db-schema.cql"),
33+
new ClassPathResource("com/foo/cql/db-test-data.cql"));
3234
}
3335

3436
@Nullable
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2020-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.cassandra.example;
17+
18+
import org.springframework.context.annotation.Configuration;
19+
import org.springframework.core.io.ClassPathResource;
20+
import org.springframework.data.cassandra.config.AbstractCassandraConfiguration;
21+
import org.springframework.data.cassandra.core.cql.session.init.KeyspacePopulator;
22+
import org.springframework.data.cassandra.core.cql.session.init.ResourceKeyspacePopulator;
23+
import org.springframework.lang.Nullable;
24+
25+
// tag::class[]
26+
@Configuration
27+
public class KeyspacePopulatorFailureConfiguration extends AbstractCassandraConfiguration {
28+
29+
@Nullable
30+
@Override
31+
protected KeyspacePopulator keyspacePopulator() {
32+
33+
ResourceKeyspacePopulator populator = new ResourceKeyspacePopulator(
34+
new ClassPathResource("com/foo/cql/db-schema.cql"));
35+
36+
populator.setIgnoreFailedDrops(true);
37+
38+
return populator;
39+
}
40+
41+
// ...
42+
// end::class[]
43+
44+
@Override
45+
protected String getKeyspaceName() {
46+
return null;
47+
}
48+
// tag::class[]
49+
}
50+
// end::class[]

src/main/asciidoc/index.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ David Webb, Matthew Adams, John Blum, Mark Paluch, Jay Bryant
33
:revnumber: {version}
44
:revdate: {localdate}
55
ifdef::backend-epub3[:front-cover-image: image:epub-cover.png[Front Cover,1050,1600]]
6-
:spring-data-commons-docs: ../../../../../spring-data-commons/src/main/asciidoc
7-
:example-root: ../../../../spring-data-cassandra/src/test/java/org/springframework/data/cassandra/example
8-
:example-resources: ../../../../spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/example
6+
:spring-data-commons-docs: ../../../../spring-data-commons/src/main/asciidoc
7+
:example-root: ../../../spring-data-cassandra/src/test/java/org/springframework/data/cassandra/example
8+
:example-resources: ../../../spring-data-cassandra/src/test/resources/org/springframework/data/cassandra/example
99
:tabsize: 2
1010
:spring-framework-docs: https://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/
1111

src/main/asciidoc/preface.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ The two key tags to search for related answers to this project are https://stack
7878
[[get-started:help:professional]]
7979
Professional Support::
8080
Professional, from-the-source support, with guaranteed response time, is available from
81-
https://pivotal.io/[Pivotal Sofware, Inc.], the company behind Spring Data and Spring.
81+
https://pivotal.io/[Pivotal Software, Inc.], the company behind Spring Data and Spring.
8282

8383
[[get-started:up-to-date]]
8484
=== Following Development

src/main/asciidoc/reference/cassandra-auditing.adoc

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
[[cassandra.auditing]]
22
== General Auditing Configuration for Cassandra
33

4-
To activate auditing functionality, add the Spring Data for Apache Cassandra `auditing` namespace element to your configuration, as the following example shows:
4+
To activate auditing functionality, create a configuration as the following example shows:
55

66
.Activating auditing by using XML configuration
77
====
8-
[source,xml]
9-
----
10-
<cassandra:auditing mapping-context-ref="customMappingContext" auditor-aware-ref="yourAuditorAwareImpl"/>
11-
----
12-
====
13-
14-
Alternatively, auditing can be enabled by annotating a configuration class with the `@EnableCassandraAuditing` annotation, as the following example shows:
15-
16-
.Activating auditing using JavaConfig
17-
====
18-
[source,java]
8+
.Java
9+
[source,java,role="primary"]
1910
----
2011
@Configuration
2112
@EnableCassandraAuditing
@@ -27,6 +18,23 @@ class Config {
2718
}
2819
}
2920
----
21+
22+
.XML
23+
[source,xml,role="secondary"]
24+
----
25+
<?xml version="1.0" encoding="UTF-8"?>
26+
<beans xmlns="http://www.springframework.org/schema/beans"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28+
xmlns:cassandra="http://www.springframework.org/schema/data/cassandra"
29+
xsi:schemaLocation="
30+
http://www.springframework.org/schema/data/cassandra
31+
https://www.springframework.org/schema/data/cassandra/spring-cassandra.xsd
32+
http://www.springframework.org/schema/beans
33+
https://www.springframework.org/schema/beans/spring-beans.xsd">
34+
35+
<cassandra:auditing mapping-context-ref="customMappingContext" auditor-aware-ref="yourAuditorAwareImpl"/>
36+
</beans>
37+
----
3038
====
3139

3240
If you expose a bean of type `AuditorAware` to the `ApplicationContext`, the auditing infrastructure picks it up automatically and uses it to determine the current user to be set on domain types.

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ The annotation carries the same attributes as the namespace element.
5454
If no base package is configured, the infrastructure scans the package of the annotated configuration class.
5555
The following example shows how to use the `@EnableCassandraRepositories` annotation:
5656

57-
.Java configuration for repositories
57+
.Configuration for repositories
5858
====
59-
[source,java]
59+
.Java
60+
[source,java,role="primary"]
6061
----
6162
@Configuration
6263
@EnableCassandraRepositories
@@ -72,13 +73,9 @@ class ApplicationConfig extends AbstractCassandraConfiguration {
7273
}
7374
}
7475
----
75-
====
7676
77-
If you want to use XML configuration, then the following example shows a minimal configuration snippet:
78-
79-
.Cassandra repository Spring XML configuration
80-
====
81-
[source,xml]
77+
.XML
78+
[source,xml,role="secondary"]
8279
----
8380
<?xml version="1.0" encoding="UTF-8"?>
8481
<beans xmlns="http://www.springframework.org/schema/beans"

src/main/asciidoc/reference/cassandra.adoc

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -291,30 +291,38 @@ Declaring a keyspace with a specification allows creating and dropping of the Ke
291291
It derives CQL from the specification so that you need not write CQL yourself.
292292
The following example specifies a Cassadra keyspace by using XML:
293293

294-
.Specifying a Cassandra keyspace by using XML
294+
.Specifying a Cassandra keyspace
295295
====
296-
[source,xml]
296+
.Java
297+
[source,java,role="primary"]
298+
----
299+
include::../{example-root}/CreateKeyspaceConfiguration.java[tags=class]
297300
----
298-
<cassandra:session>
299-
300-
<cassandra:keyspace action="CREATE_DROP" durable-writes="true" name="my_keyspace">
301-
<cassandra:replication class="NETWORK_TOPOLOGY_STRATEGY">
302-
<cassandra:data-center name="foo" replication-factor="1" />
303-
<cassandra:data-center name="bar" replication-factor="2" />
304-
</cassandra:replication>
305-
</cassandra:keyspace>
306301
307-
</cassandra:session>
302+
.XML
303+
[source,xml,role="secondary"]
308304
----
309-
====
305+
<?xml version="1.0" encoding="UTF-8"?>
306+
<beans xmlns="http://www.springframework.org/schema/beans"
307+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
308+
xmlns:cassandra="http://www.springframework.org/schema/data/cassandra"
309+
xsi:schemaLocation="
310+
http://www.springframework.org/schema/data/cassandra
311+
https://www.springframework.org/schema/data/cassandra/spring-cassandra.xsd
312+
http://www.springframework.org/schema/beans
313+
https://www.springframework.org/schema/beans/spring-beans.xsd">
310314
311-
You can also specify a Cassandra keyspace by using Java configuration, as the following example shows:
315+
<cassandra:session>
312316
313-
.Specifying a Cassandra keyspace by using Java configuration
314-
====
315-
[source,java]
316-
----
317-
include::../{example-root}/CreateKeyspaceConfiguration.java[tags=class]
317+
<cassandra:keyspace action="CREATE_DROP" durable-writes="true" name="my_keyspace">
318+
<cassandra:replication class="NETWORK_TOPOLOGY_STRATEGY">
319+
<cassandra:data-center name="foo" replication-factor="1" />
320+
<cassandra:data-center name="bar" replication-factor="2" />
321+
</cassandra:replication>
322+
</cassandra:keyspace>
323+
324+
</cassandra:session>
325+
</beans>
318326
----
319327
====
320328

@@ -334,16 +342,14 @@ You may sometimes need to initialize a keyspace that runs on a server somewhere.
334342
You can provide arbitrary CQL that is executed on `CqlSession` initialization and shutdown in the configured keyspace, as the following Java configuration example shows:
335343

336344
====
337-
[source,java]
345+
.Java
346+
[source,java,role="primary"]
338347
----
339348
include::../{example-root}/KeyspacePopulatorConfiguration.java[tags=class]
340349
----
341-
====
342-
343-
If you want to initialize a database using XML configuration and you can provide a reference to a `SessionFactory` bean, you can use the `initialize-keyspace` tag in the `cassandra` namespace:
344350
345-
====
346-
[source,xml,indent=0,subs="verbatim,quotes"]
351+
.XML
352+
[source,xml,indent=0,subs="verbatim,quotes",role="secondary"]
347353
----
348354
<cassandra:initialize-keyspace session-factory-ref="cassandraSessionFactory">
349355
<cassandra:script location="classpath:com/foo/cql/db-schema.cql"/>
@@ -382,7 +388,14 @@ The second option to control what happens with existing data is to be more toler
382388
To this end, you can control the ability of the initializer to ignore certain errors in the CQL it executes from the scripts, as the following example shows:
383389

384390
====
385-
[source,xml,indent=0,subs="verbatim,quotes"]
391+
.Java
392+
[source,java,role="primary"]
393+
----
394+
include::../{example-root}/KeyspacePopulatorFailureConfiguration.java[tags=class]
395+
----
396+
397+
.XML
398+
[source,xml,indent=0,subs="verbatim,quotes",role="secondary"]
386399
----
387400
<cassandra:initialize-keyspace session-factory-ref="cassandraSessionFactory" ignore-failures="DROPS">
388401
<cassandra:script location="..."/>
@@ -401,27 +414,24 @@ Each statement should be separated by `;` or a new line if the `;` character is
401414
You can control that globally or script by script, as the following example shows:
402415

403416
====
404-
[source,java]
417+
.Java
418+
[source,java,role="primary"]
405419
----
406420
include::../{example-root}/SessionFactoryInitializerConfiguration.java[tags=class]
407421
----
408-
====
409-
410-
Alternatively, you can use XML to configure the `SessionFactoryInitializer`:
411422
412-
====
413-
[source,xml,indent=0,subs="verbatim,quotes"]
423+
.XML
424+
[source,xml,indent=0,subs="verbatim,quotes",role="secondary"]
414425
----
415-
<cassandra:initialize-keyspace session-factory-ref="cassandraSessionFactory" separator="@@"> <1>
416-
<cassandra:script location="classpath:com/myapp/cql/db-schema.cql" separator=";"/> <2>
426+
<cassandra:initialize-keyspace session-factory-ref="cassandraSessionFactory" separator="@@">
427+
<cassandra:script location="classpath:com/myapp/cql/db-schema.cql" separator=";"/>
417428
<cassandra:script location="classpath:com/myapp/cql/db-test-data-1.cql"/>
418429
<cassandra:script location="classpath:com/myapp/cql/db-test-data-2.cql"/>
419430
</cassandra:initialize-keyspace>
420431
----
421-
<1> Set the separator scripts to `@@`.
422-
<2> Set the separator for `db-schema.cql` to `;`.
423432
====
424433

434+
425435
In this example, the two `test-data` scripts use `@@` as statement separator and only the `db-schema.cql` uses `;`.
426436
This configuration specifies that the default separator is `@@` and overrides that default for the `db-schema` script.
427437

@@ -490,23 +500,29 @@ to specify the desired column type.
490500

491501
The following example shows how to specify entity base packages in XML configuration:
492502

493-
.Specifying entity base packages with XML configuration
503+
.Specifying entity base packages
494504
====
495-
[source,xml]
505+
.Java
506+
[source,java,role="primary"]
496507
----
497-
498-
<cassandra:mapping entity-base-packages="com.foo,com.bar"/>
499-
508+
include::../{example-root}/EntityBasePackagesConfiguration.java[tags=class]
500509
----
501-
====
502-
503-
The following example shows how to specify entity base packages in Java configuration:
504510
505-
.Specifying entity base packages with Java configuration
506-
====
507-
[source,java]
511+
.XML
512+
[source,xml,role="secondary"]
508513
----
509-
include::../{example-root}/EntityBasePackagesConfiguration.java[tags=class]
514+
<?xml version="1.0" encoding="UTF-8"?>
515+
<beans xmlns="http://www.springframework.org/schema/beans"
516+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
517+
xmlns:cassandra="http://www.springframework.org/schema/data/cassandra"
518+
xsi:schemaLocation="
519+
http://www.springframework.org/schema/data/cassandra
520+
https://www.springframework.org/schema/data/cassandra/spring-cassandra.xsd
521+
http://www.springframework.org/schema/beans
522+
https://www.springframework.org/schema/beans/spring-beans.xsd">
523+
524+
<cassandra:mapping entity-base-packages="com.foo,com.bar"/>
525+
</beans>
510526
----
511527
====
512528

0 commit comments

Comments
 (0)