Skip to content

Commit dd6bf57

Browse files
committed
Add support for EnableCaching in the cli
Fixes spring-projectsgh-1431
1 parent a0c316d commit dd6bf57

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2014 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+
* http://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+
17+
package org.springframework.boot.cli.compiler.autoconfigure;
18+
19+
import org.codehaus.groovy.ast.ClassNode;
20+
import org.codehaus.groovy.control.CompilationFailedException;
21+
import org.codehaus.groovy.control.customizers.ImportCustomizer;
22+
23+
import org.springframework.boot.cli.compiler.AstUtils;
24+
import org.springframework.boot.cli.compiler.CompilerAutoConfiguration;
25+
import org.springframework.boot.cli.compiler.DependencyCustomizer;
26+
27+
/**
28+
* {@link CompilerAutoConfiguration} for the caching infrastructure.
29+
*
30+
* @author Stephane Nicoll
31+
* @since 1.2.0
32+
*/
33+
public class CachingAutoConfiguration extends CompilerAutoConfiguration {
34+
35+
@Override
36+
public boolean matches(ClassNode classNode) {
37+
return AstUtils.hasAtLeastOneAnnotation(classNode, "EnableCaching");
38+
}
39+
40+
@Override
41+
public void applyDependencies(DependencyCustomizer dependencies)
42+
throws CompilationFailedException {
43+
dependencies.add("spring-context-support");
44+
45+
}
46+
47+
@Override
48+
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
49+
imports.addStarImports("org.springframework.cache",
50+
"org.springframework.cache.annotation", "org.springframework.cache.concurrent");
51+
}
52+
53+
}

spring-boot-cli/src/main/resources/META-INF/services/org.springframework.boot.cli.compiler.CompilerAutoConfiguration

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ org.springframework.boot.cli.compiler.autoconfigure.SpringMvcCompilerAutoConfigu
44
org.springframework.boot.cli.compiler.autoconfigure.SpringBatchCompilerAutoConfiguration
55
org.springframework.boot.cli.compiler.autoconfigure.RabbitCompilerAutoConfiguration
66
org.springframework.boot.cli.compiler.autoconfigure.ReactorCompilerAutoConfiguration
7+
org.springframework.boot.cli.compiler.autoconfigure.CachingAutoConfiguration
78
org.springframework.boot.cli.compiler.autoconfigure.JdbcCompilerAutoConfiguration
89
org.springframework.boot.cli.compiler.autoconfigure.JmsCompilerAutoConfiguration
910
org.springframework.boot.cli.compiler.autoconfigure.JUnitCompilerAutoConfiguration

spring-boot-cli/src/test/java/org/springframework/boot/cli/ReproIntegrationTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,10 @@ public void jmsListener() throws Exception {
7878
this.cli.run("jms.groovy");
7979
assertThat(this.cli.getOutput(), containsString("Hello World"));
8080
}
81+
82+
@Test
83+
public void caching() throws Exception {
84+
this.cli.run("caching.groovy");
85+
assertThat(this.cli.getOutput(), containsString("Hello World"));
86+
}
8187
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.test
2+
3+
import java.util.concurrent.atomic.AtomicLong
4+
5+
@Configuration
6+
@EnableCaching
7+
class Sample {
8+
9+
@Bean CacheManager cacheManager() {
10+
new ConcurrentMapCacheManager()
11+
}
12+
13+
@Component
14+
static class MyClient implements CommandLineRunner {
15+
16+
private final MyService myService
17+
18+
@Autowired
19+
MyClient(MyService myService) {
20+
this.myService = myService
21+
}
22+
23+
void run(String... args) {
24+
long counter = myService.get('someKey')
25+
long counter2 = myService.get('someKey')
26+
if (counter == counter2) {
27+
println 'Hello World'
28+
} else {
29+
println 'Something went wrong with the cache setup'
30+
}
31+
32+
}
33+
}
34+
35+
@Component
36+
static class MyService {
37+
38+
private final AtomicLong counter = new AtomicLong()
39+
40+
@Cacheable('foo')
41+
Long get(String id) {
42+
return counter.getAndIncrement()
43+
}
44+
45+
}
46+
47+
}

spring-boot-docs/src/main/asciidoc/spring-boot-cli.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,12 @@ The following items are used as ``grab hints'':
113113
|`JdbcTemplate`, `NamedParameterJdbcTemplate`, `DataSource`
114114
|JDBC Application.
115115

116-
|`@EnableJmsMessaging`
116+
|`@EnableJms`
117117
|JMS Application.
118118

119+
|`@EnableCaching`
120+
|Caching abstraction.
121+
119122
|`@Test`
120123
|JUnit.
121124

0 commit comments

Comments
 (0)