Skip to content

Commit b485888

Browse files
committed
Merge pull request #8202 from jxblum:spring-boot-sample-cache-ux-improvements
* pr/8202: Polish contribution Improve spring-boot-sample-cache UX by using Maven profiles
2 parents 87b8ce6 + 4d7b03b commit b485888

File tree

2 files changed

+155
-104
lines changed

2 files changed

+155
-104
lines changed

spring-boot-samples/spring-boot-sample-cache/README.adoc

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ abstraction is supported by many caching libraries, including:
1515
* Generic provider based on `org.springframework.Cache` bean definition(s)
1616
1717
The sample defines a simple `CountryService` that caches countries by ISO code. When
18-
the application starts a client invokes the service with a random code every 500ms. You
19-
can look at the `/metrics` endpoint to review the cache statistics if your chosen
18+
the application starts a client invokes the service with a random code every 500ms.
19+
You can look at the `/metrics` endpoint to review the cache statistics if your chosen
2020
caching provider is supported.
2121

2222

@@ -26,30 +26,31 @@ The sample uses Spring's cache annotation. If you want to use the JSR-107 annota
2626
instead, simply add the `javax.cache:cache-api` dependency to the project. No further
2727
configuration is necessary.
2828

29-
NOTE: You can use the JSR-107 annotations with _any_ cache provider; a JSR-107 compliant
30-
cache provider is not necessary.
29+
NOTE: You can use the JSR-107 annotations with _any_ cache provider; a JSR-107
30+
compliant cache provider is not necessary.
3131

3232

3333

3434
== Using a different cache provider
3535
Initially, the project does not define any caching library so the abstraction works
36-
on simple `ConcurrentHashMap`-based caches. You can try out your favorite caching library
37-
as explained below.
36+
on simple `ConcurrentHashMap`-based caches. You can try out your favorite caching
37+
library as explained below.
3838

3939

4040

4141
=== JCache (JSR-107)
42-
If you want to configure your cache infrastructure via the standard, you need a compliant
43-
implementation and the JSR-107 api. You first need to add `javax.cache:cache-api` to your
44-
project. Then you could try the following:
42+
If you want to configure your cache infrastructure via the standard, you need a
43+
compliant implementation and the JSR-107 api. You first need to add
44+
`javax.cache:cache-api` to your project. Then you could try the following:
4545

4646
* `EhCache 3`: add `org.ehcache:ehcache`
4747
* `Hazelcast`: add `com.hazelcast:hazelcast`
4848
* `Infinispan`: add `org.infinispan:infinispan-jcache`
4949

50-
TIP: Certain cache providers do not create a default cache on-the-fly if it does not exist
51-
so you might need to update the sample to create the caches on startup or specify the
52-
location to the provider-specific file via the `spring.cache.jcache.config` property.
50+
TIP: Certain cache providers do not create a default cache on-the-fly if it does not
51+
exist so you might need to update the sample to create the caches on startup or
52+
specify the location to the provider-specific file via the
53+
`spring.cache.jcache.config` property.
5354

5455
NOTE: Any other JSR-107 compliant provider is also supported but Spring Boot may not
5556
offer a dependency management entry for it. You will have to add it with the version
@@ -59,39 +60,55 @@ of the library that you want to use.
5960

6061
=== EhCache 2.x
6162
Simply add the `net.sf.ehcache:ehcache` dependency to the project. Since there is a
62-
default `ehcache.xml` configuration file at the root of the classpath, it is automatically
63-
used to configure the underlying `CacheManager`. Note that EhCache 3 uses a different
64-
format and doesn't default to `ehcache.xml` anymore. Check
65-
http://www.ehcache.org/documentation/3.0/xml.html[the documentation] for more details.
63+
default `ehcache.xml` configuration file at the root of the classpath,
64+
it is automatically used to configure the underlying `CacheManager`.
65+
Note that EhCache 3 uses a different format and doesn't default to `ehcache.xml`
66+
anymore. Check http://www.ehcache.org/documentation/3.0/xml.html[the documentation]
67+
for more details.
68+
69+
TIP: Run sample cache application using EhCache with
70+
`$mvn spring-boot:run -Pehcache2`.
6671

6772

6873

6974
=== Hazelcast
70-
Both `com.hazelcast:hazelcast` and `com.hazelcast:hazelcast-spring` should be added to
71-
the project to enable support for Hazelcast. Since there is a default `hazelcast.xml`
72-
configuration file at the root of the classpath, it is used to automatically configure
73-
the underlying `HazelcastInstance`.
75+
Both `com.hazelcast:hazelcast` and `com.hazelcast:hazelcast-spring` should be added
76+
to the project to enable support for Hazelcast. Since there is a default
77+
`hazelcast.xml` configuration file at the root of the classpath, it is used to
78+
automatically configure the underlying `HazelcastInstance`.
79+
80+
TIP: Run sample cache application using Hazelcast with
81+
`$mvn spring-boot:run -Phazelcast`.
7482

7583

7684

7785
=== Infinispan
7886
Add the `org.infinispan:infinispan-spring4-embedded` dependency to enable support for
79-
Infinispan. There is no default location that Infinispan uses to look for a config file
80-
so if you don't specify anything it will bootstrap on a hardcoded default. You can set
81-
the `spring.cache.infinispan.config` property to use the provided `infinispan.xml`
82-
configuration instead.
87+
Infinispan. There is no default location that Infinispan uses to look for a config
88+
file so if you don't specify anything it will bootstrap on a hardcoded default. You
89+
can set the `spring.cache.infinispan.config` property to use the provided
90+
`infinispan.xml` configuration instead.
91+
92+
TIP: Run sample cache application using Hazelcast with
93+
`$mvn spring-boot:run -Pinfinispan`.
8394

8495

8596

8697
=== Couchbase
87-
Add the `java-client` and `couchbase-spring-cache` dependencies and make sure that you
88-
have setup at least a `spring.couchbase.bootstrap-hosts` property.
98+
Add the `java-client` and `couchbase-spring-cache` dependencies and make sure that
99+
you have setup at least a `spring.couchbase.bootstrap-hosts` property.
100+
101+
TIP: Run sample cache application using Hazelcast with
102+
`$mvn spring-boot:run -Pcouchbase`.
89103

90104

91105

92106
=== Redis
93-
Add the `spring-boot-starter-data-redis` and make sure it is configured properly (by default,
94-
a redis instance with the default settings is expected on your local box).
107+
Add the `spring-boot-starter-data-redis` and make sure it is configured properly (by
108+
default, a redis instance with the default settings is expected on your local box).
109+
110+
TIP: Run sample cache application using Hazelcast with
111+
`$mvn spring-boot:run -Predis`.
95112

96113

97114

@@ -100,10 +117,16 @@ Simply add the `com.github.ben-manes.caffeine:caffeine` dependency to enable sup
100117
for Caffeine. You can customize how caches are created in different ways, see
101118
`application.properties` for an example and the documentation for more details.
102119

120+
TIP: Run sample cache application using Hazelcast with
121+
`$mvn spring-boot:run -Pcaffeine`.
122+
103123

104124

105125
=== Guava
106-
Spring Boot does not provide any dependency management for _Guava_ so you'll have to add
107-
the `com.google.guava:guava` dependency with a version. You can customize how caches are
108-
created in different ways, see `application.properties` for an example and the
109-
documentation for more details.
126+
Spring Boot does not provide any dependency management for _Guava_ so you'll have to
127+
add the `com.google.guava:guava` dependency with a version. You can customize how
128+
caches are created in different ways, see `application.properties` for an example and
129+
the documentation for more details.
130+
131+
TIP: Run sample cache application using Hazelcast with
132+
`$mvn spring-boot:run -Pguava`.

spring-boot-samples/spring-boot-sample-cache/pom.xml

Lines changed: 100 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -33,78 +33,6 @@
3333
<groupId>org.springframework.boot</groupId>
3434
<artifactId>spring-boot-starter-actuator</artifactId>
3535
</dependency>
36-
37-
<!-- JSR-107 API (uncomment to try the JCache support) -->
38-
<!--
39-
<dependency>
40-
<groupId>javax.cache</groupId>
41-
<artifactId>cache-api</artifactId>
42-
</dependency>
43-
-->
44-
45-
<!-- Additional cache providers (uncomment to try them) -->
46-
<!--
47-
<dependency>
48-
<groupId>net.sf.ehcache</groupId>
49-
<artifactId>ehcache</artifactId>
50-
</dependency>
51-
-->
52-
<!--
53-
<dependency>
54-
<groupId>org.ehcache</groupId>
55-
<artifactId>ehcache</artifactId>
56-
</dependency>
57-
-->
58-
<!--
59-
<dependency>
60-
<groupId>com.hazelcast</groupId>
61-
<artifactId>hazelcast</artifactId>
62-
</dependency>
63-
<dependency>
64-
<groupId>com.hazelcast</groupId>
65-
<artifactId>hazelcast-spring</artifactId>
66-
</dependency>
67-
-->
68-
<!--
69-
<dependency>
70-
<groupId>org.infinispan</groupId>
71-
<artifactId>infinispan-spring4-embedded</artifactId>
72-
</dependency>
73-
<dependency>
74-
<groupId>org.infinispan</groupId>
75-
<artifactId>infinispan-jcache</artifactId>
76-
</dependency>
77-
-->
78-
<!--
79-
<dependency>
80-
<groupId>com.couchbase.client</groupId>
81-
<artifactId>java-client</artifactId>
82-
</dependency>
83-
<dependency>
84-
<groupId>com.couchbase.client</groupId>
85-
<artifactId>couchbase-spring-cache</artifactId>
86-
</dependency>
87-
-->
88-
<!--
89-
<dependency>
90-
<groupId>org.springframework.boot</groupId>
91-
<artifactId>spring-boot-starter-data-redis</artifactId>
92-
</dependency>
93-
-->
94-
<!--
95-
<dependency>
96-
<groupId>com.github.ben-manes.caffeine</groupId>
97-
<artifactId>caffeine</artifactId>
98-
</dependency>
99-
-->
100-
<!--
101-
<dependency>
102-
<groupId>com.google.guava</groupId>
103-
<artifactId>guava</artifactId>
104-
<version>18.0</version>
105-
</dependency>
106-
-->
107-
10836
<!-- Test -->
10937
<dependency>
11038
<groupId>org.springframework.boot</groupId>
@@ -120,4 +48,104 @@
12048
</plugin>
12149
</plugins>
12250
</build>
51+
<profiles>
52+
<profile>
53+
<id>jcache</id>
54+
<dependencies>
55+
<dependency>
56+
<groupId>javax.cache</groupId>
57+
<artifactId>cache-api</artifactId>
58+
</dependency>
59+
</dependencies>
60+
</profile>
61+
<profile>
62+
<id>caffeine</id>
63+
<dependencies>
64+
<dependency>
65+
<groupId>com.github.ben-manes.caffeine</groupId>
66+
<artifactId>caffeine</artifactId>
67+
</dependency>
68+
</dependencies>
69+
</profile>
70+
<profile>
71+
<id>couchbase</id>
72+
<dependencies>
73+
<dependency>
74+
<groupId>com.couchbase.client</groupId>
75+
<artifactId>java-client</artifactId>
76+
</dependency>
77+
<dependency>
78+
<groupId>com.couchbase.client</groupId>
79+
<artifactId>couchbase-spring-cache</artifactId>
80+
</dependency>
81+
</dependencies>
82+
</profile>
83+
<profile>
84+
<id>ehcache2</id>
85+
<dependencies>
86+
<dependency>
87+
<groupId>net.sf.ehcache</groupId>
88+
<artifactId>ehcache</artifactId>
89+
</dependency>
90+
</dependencies>
91+
</profile>
92+
<profile>
93+
<id>ehcache</id>
94+
<dependencies>
95+
<dependency>
96+
<groupId>javax.cache</groupId>
97+
<artifactId>cache-api</artifactId>
98+
</dependency>
99+
<dependency>
100+
<groupId>org.ehcache</groupId>
101+
<artifactId>ehcache</artifactId>
102+
</dependency>
103+
</dependencies>
104+
</profile>
105+
<profile>
106+
<id>hazelcast</id>
107+
<dependencies>
108+
<dependency>
109+
<groupId>com.hazelcast</groupId>
110+
<artifactId>hazelcast</artifactId>
111+
</dependency>
112+
<dependency>
113+
<groupId>com.hazelcast</groupId>
114+
<artifactId>hazelcast-spring</artifactId>
115+
</dependency>
116+
</dependencies>
117+
</profile>
118+
<profile>
119+
<id>infinispan</id>
120+
<dependencies>
121+
<dependency>
122+
<groupId>org.infinispan</groupId>
123+
<artifactId>infinispan-spring4-embedded</artifactId>
124+
</dependency>
125+
<dependency>
126+
<groupId>org.infinispan</groupId>
127+
<artifactId>infinispan-jcache</artifactId>
128+
</dependency>
129+
</dependencies>
130+
</profile>
131+
<profile>
132+
<id>redis</id>
133+
<dependencies>
134+
<dependency>
135+
<groupId>org.springframework.boot</groupId>
136+
<artifactId>spring-boot-starter-data-redis</artifactId>
137+
</dependency>
138+
</dependencies>
139+
</profile>
140+
<profile>
141+
<id>guava</id>
142+
<dependencies>
143+
<dependency>
144+
<groupId>com.google.guava</groupId>
145+
<artifactId>guava</artifactId>
146+
<version>18.0</version>
147+
</dependency>
148+
</dependencies>
149+
</profile>
150+
</profiles>
123151
</project>

0 commit comments

Comments
 (0)