Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
7df05c7d
Commit
7df05c7d
authored
Feb 22, 2016
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.3.x'
parents
ca79ba82
094f7aa2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
141 additions
and
95 deletions
+141
-95
HazelcastCacheConfiguration.java
...boot/autoconfigure/cache/HazelcastCacheConfiguration.java
+4
-86
HazelcastInstanceConfiguration.java
...t/autoconfigure/cache/HazelcastInstanceConfiguration.java
+120
-0
CacheAutoConfigurationTests.java
...boot/autoconfigure/cache/CacheAutoConfigurationTests.java
+17
-9
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java
View file @
7df05c7d
/*
/*
* Copyright 2012-201
5
the original author or authors.
* Copyright 2012-201
6
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -16,25 +16,17 @@
...
@@ -16,25 +16,17 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
cache
;
package
org
.
springframework
.
boot
.
autoconfigure
.
cache
;
import
java.io.Closeable
;
import
java.io.IOException
;
import
com.hazelcast.core.Hazelcast
;
import
com.hazelcast.core.HazelcastInstance
;
import
com.hazelcast.core.HazelcastInstance
;
import
com.hazelcast.spring.cache.HazelcastCacheManager
;
import
com.hazelcast.spring.cache.HazelcastCacheManager
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate
;
import
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration
;
import
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration
;
import
org.springframework.boot.autoconfigure.hazelcast.HazelcastConfigResourceCondition
;
import
org.springframework.boot.autoconfigure.hazelcast.HazelcastConfigResourceCondition
;
import
org.springframework.boot.autoconfigure.hazelcast.HazelcastInstanceFactory
;
import
org.springframework.cache.CacheManager
;
import
org.springframework.cache.CacheManager
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Conditional
;
import
org.springframework.context.annotation.Conditional
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.co
re.io.Resource
;
import
org.springframework.co
ntext.annotation.Import
;
/**
/**
* Hazelcast cache configuration. Can either reuse the {@link HazelcastInstance} that has
* Hazelcast cache configuration. Can either reuse the {@link HazelcastInstance} that has
...
@@ -49,84 +41,10 @@ import org.springframework.core.io.Resource;
...
@@ -49,84 +41,10 @@ import org.springframework.core.io.Resource;
* @see HazelcastConfigResourceCondition
* @see HazelcastConfigResourceCondition
*/
*/
@Configuration
@Configuration
@ConditionalOnClass
({
HazelcastInstance
.
class
,
HazelcastCacheManager
.
class
})
@ConditionalOnClass
({
HazelcastInstance
.
class
,
HazelcastCacheManager
.
class
})
@ConditionalOnMissingBean
(
CacheManager
.
class
)
@ConditionalOnMissingBean
(
CacheManager
.
class
)
@Conditional
(
CacheCondition
.
class
)
@Conditional
(
CacheCondition
.
class
)
@Import
({
HazelcastInstanceConfiguration
.
Existing
.
class
,
HazelcastInstanceConfiguration
.
Specific
.
class
})
class
HazelcastCacheConfiguration
{
class
HazelcastCacheConfiguration
{
@Configuration
@ConditionalOnSingleCandidate
(
HazelcastInstance
.
class
)
static
class
ExistingHazelcastInstanceConfiguration
{
@Autowired
private
CacheProperties
cacheProperties
;
@Bean
public
HazelcastCacheManager
cacheManager
(
HazelcastInstance
existingHazelcastInstance
)
throws
IOException
{
Resource
config
=
this
.
cacheProperties
.
getHazelcast
().
getConfig
();
Resource
location
=
this
.
cacheProperties
.
resolveConfigLocation
(
config
);
if
(
location
!=
null
)
{
HazelcastInstance
cacheHazelcastInstance
=
new
HazelcastInstanceFactory
(
location
).
getHazelcastInstance
();
return
new
CloseableHazelcastCacheManager
(
cacheHazelcastInstance
);
}
return
new
HazelcastCacheManager
(
existingHazelcastInstance
);
}
}
@Configuration
@ConditionalOnMissingBean
(
HazelcastInstance
.
class
)
@Conditional
(
ConfigAvailableCondition
.
class
)
static
class
DefaultHazelcastInstanceConfiguration
{
@Autowired
private
CacheProperties
cacheProperties
;
@Bean
public
HazelcastInstance
hazelcastInstance
()
throws
IOException
{
Resource
config
=
this
.
cacheProperties
.
getHazelcast
().
getConfig
();
Resource
location
=
this
.
cacheProperties
.
resolveConfigLocation
(
config
);
if
(
location
!=
null
)
{
new
HazelcastInstanceFactory
(
location
).
getHazelcastInstance
();
}
return
Hazelcast
.
newHazelcastInstance
();
}
@Bean
public
HazelcastCacheManager
cacheManager
()
throws
IOException
{
return
new
HazelcastCacheManager
(
hazelcastInstance
());
}
}
/**
* {@link HazelcastConfigResourceCondition} that checks if the
* {@code spring.cache.hazelcast.config} configuration key is defined.
*/
static
class
ConfigAvailableCondition
extends
HazelcastConfigResourceCondition
{
ConfigAvailableCondition
()
{
super
(
"spring.cache.hazelcast"
,
"config"
);
}
}
private
static
class
CloseableHazelcastCacheManager
extends
HazelcastCacheManager
implements
Closeable
{
private
final
HazelcastInstance
hazelcastInstance
;
CloseableHazelcastCacheManager
(
HazelcastInstance
hazelcastInstance
)
{
super
(
hazelcastInstance
);
this
.
hazelcastInstance
=
hazelcastInstance
;
}
@Override
public
void
close
()
throws
IOException
{
this
.
hazelcastInstance
.
shutdown
();
}
}
}
}
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastInstanceConfiguration.java
0 → 100644
View file @
7df05c7d
/*
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
autoconfigure
.
cache
;
import
java.io.Closeable
;
import
java.io.IOException
;
import
com.hazelcast.core.Hazelcast
;
import
com.hazelcast.core.HazelcastInstance
;
import
com.hazelcast.spring.cache.HazelcastCacheManager
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate
;
import
org.springframework.boot.autoconfigure.hazelcast.HazelcastConfigResourceCondition
;
import
org.springframework.boot.autoconfigure.hazelcast.HazelcastInstanceFactory
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Conditional
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.io.Resource
;
/**
* Actual {@link HazelcastInstance} configurations imported by
* {@link HazelcastCacheConfiguration}.
*
* @author Stephane Nicoll
*/
abstract
class
HazelcastInstanceConfiguration
{
@Configuration
@ConditionalOnSingleCandidate
(
HazelcastInstance
.
class
)
static
class
Existing
{
@Autowired
private
CacheProperties
cacheProperties
;
@Bean
public
HazelcastCacheManager
cacheManager
(
HazelcastInstance
existingHazelcastInstance
)
throws
IOException
{
Resource
config
=
this
.
cacheProperties
.
getHazelcast
().
getConfig
();
Resource
location
=
this
.
cacheProperties
.
resolveConfigLocation
(
config
);
if
(
location
!=
null
)
{
HazelcastInstance
cacheHazelcastInstance
=
new
HazelcastInstanceFactory
(
location
).
getHazelcastInstance
();
return
new
CloseableHazelcastCacheManager
(
cacheHazelcastInstance
);
}
return
new
HazelcastCacheManager
(
existingHazelcastInstance
);
}
}
@Configuration
@ConditionalOnMissingBean
(
HazelcastInstance
.
class
)
@Conditional
(
ConfigAvailableCondition
.
class
)
static
class
Specific
{
@Autowired
private
CacheProperties
cacheProperties
;
@Bean
public
HazelcastInstance
hazelcastInstance
()
throws
IOException
{
Resource
config
=
this
.
cacheProperties
.
getHazelcast
().
getConfig
();
Resource
location
=
this
.
cacheProperties
.
resolveConfigLocation
(
config
);
if
(
location
!=
null
)
{
return
new
HazelcastInstanceFactory
(
location
).
getHazelcastInstance
();
}
return
Hazelcast
.
newHazelcastInstance
();
}
@Bean
public
HazelcastCacheManager
cacheManager
()
throws
IOException
{
return
new
HazelcastCacheManager
(
hazelcastInstance
());
}
}
/**
* {@link HazelcastConfigResourceCondition} that checks if the
* {@code spring.cache.hazelcast.config} configuration key is defined.
*/
static
class
ConfigAvailableCondition
extends
HazelcastConfigResourceCondition
{
ConfigAvailableCondition
()
{
super
(
"spring.cache.hazelcast"
,
"config"
);
}
}
private
static
class
CloseableHazelcastCacheManager
extends
HazelcastCacheManager
implements
Closeable
{
private
final
HazelcastInstance
hazelcastInstance
;
CloseableHazelcastCacheManager
(
HazelcastInstance
hazelcastInstance
)
{
super
(
hazelcastInstance
);
this
.
hazelcastInstance
=
hazelcastInstance
;
}
@Override
public
void
close
()
throws
IOException
{
this
.
hazelcastInstance
.
shutdown
();
}
}
}
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java
View file @
7df05c7d
/*
/*
* Copyright 2012-201
5
the original author or authors.
* Copyright 2012-201
6
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -326,16 +326,21 @@ public class CacheAutoConfigurationTests {
...
@@ -326,16 +326,21 @@ public class CacheAutoConfigurationTests {
cacheManager
.
getCache
(
"defaultCache"
);
cacheManager
.
getCache
(
"defaultCache"
);
assertThat
(
cacheManager
.
getCacheNames
()).
containsOnly
(
"defaultCache"
);
assertThat
(
cacheManager
.
getCacheNames
()).
containsOnly
(
"defaultCache"
);
assertThat
(
this
.
context
.
getBean
(
HazelcastInstance
.
class
))
assertThat
(
this
.
context
.
getBean
(
HazelcastInstance
.
class
))
.
isEqualTo
(
new
DirectFieldAccessor
(
cacheManager
)
.
isEqualTo
(
getHazelcastInstance
(
cacheManager
));
.
getPropertyValue
(
"hazelcastInstance"
));
}
}
@Test
@Test
public
void
hazelcastCacheWithConfig
()
{
public
void
hazelcastCacheWithConfig
()
throws
IOException
{
load
(
DefaultCacheConfiguration
.
class
,
"spring.cache.type=hazelcast"
,
load
(
DefaultCacheConfiguration
.
class
,
"spring.cache.type=hazelcast"
,
"spring.cache.hazelcast.config=org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"
);
"spring.cache.hazelcast.config=org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"
);
HazelcastInstance
hazelcastInstance
=
this
.
context
.
getBean
(
HazelcastInstance
.
class
);
HazelcastCacheManager
cacheManager
=
validateCacheManager
(
HazelcastCacheManager
cacheManager
=
validateCacheManager
(
HazelcastCacheManager
.
class
);
HazelcastCacheManager
.
class
);
HazelcastInstance
actual
=
getHazelcastInstance
(
cacheManager
);
assertThat
(
actual
).
isSameAs
(
hazelcastInstance
);
assertThat
(
actual
.
getConfig
().
getConfigurationUrl
()).
isEqualTo
(
new
ClassPathResource
(
"org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"
).
getURL
());
cacheManager
.
getCache
(
"foobar"
);
cacheManager
.
getCache
(
"foobar"
);
assertThat
(
cacheManager
.
getCacheNames
()).
containsOnly
(
"foobar"
);
assertThat
(
cacheManager
.
getCacheNames
()).
containsOnly
(
"foobar"
);
}
}
...
@@ -353,9 +358,8 @@ public class CacheAutoConfigurationTests {
...
@@ -353,9 +358,8 @@ public class CacheAutoConfigurationTests {
load
(
HazelcastCustomHazelcastInstance
.
class
,
"spring.cache.type=hazelcast"
);
load
(
HazelcastCustomHazelcastInstance
.
class
,
"spring.cache.type=hazelcast"
);
HazelcastCacheManager
cacheManager
=
validateCacheManager
(
HazelcastCacheManager
cacheManager
=
validateCacheManager
(
HazelcastCacheManager
.
class
);
HazelcastCacheManager
.
class
);
assertThat
(
new
DirectFieldAccessor
(
cacheManager
)
assertThat
(
getHazelcastInstance
(
cacheManager
))
.
getPropertyValue
(
"hazelcastInstance"
))
.
isEqualTo
(
this
.
context
.
getBean
(
"customHazelcastInstance"
));
.
isEqualTo
(
this
.
context
.
getBean
(
"customHazelcastInstance"
));
}
}
@Test
@Test
...
@@ -372,8 +376,7 @@ public class CacheAutoConfigurationTests {
...
@@ -372,8 +376,7 @@ public class CacheAutoConfigurationTests {
HazelcastCacheManager
.
class
);
HazelcastCacheManager
.
class
);
HazelcastInstance
hazelcastInstance
=
this
.
context
HazelcastInstance
hazelcastInstance
=
this
.
context
.
getBean
(
HazelcastInstance
.
class
);
.
getBean
(
HazelcastInstance
.
class
);
assertThat
(
new
DirectFieldAccessor
(
cacheManager
)
assertThat
(
getHazelcastInstance
(
cacheManager
)).
isEqualTo
(
hazelcastInstance
);
.
getPropertyValue
(
"hazelcastInstance"
)).
isEqualTo
(
hazelcastInstance
);
assertThat
(
hazelcastInstance
.
getConfig
().
getConfigurationFile
())
assertThat
(
hazelcastInstance
.
getConfig
().
getConfigurationFile
())
.
isEqualTo
(
new
ClassPathResource
(
mainConfig
).
getFile
());
.
isEqualTo
(
new
ClassPathResource
(
mainConfig
).
getFile
());
}
}
...
@@ -543,6 +546,11 @@ public class CacheAutoConfigurationTests {
...
@@ -543,6 +546,11 @@ public class CacheAutoConfigurationTests {
this
.
context
=
applicationContext
;
this
.
context
=
applicationContext
;
}
}
private
static
HazelcastInstance
getHazelcastInstance
(
HazelcastCacheManager
cacheManager
)
{
return
(
HazelcastInstance
)
new
DirectFieldAccessor
(
cacheManager
)
.
getPropertyValue
(
"hazelcastInstance"
);
}
@Configuration
@Configuration
static
class
EmptyConfiguration
{
static
class
EmptyConfiguration
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment