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
18b1e8da
Commit
18b1e8da
authored
May 04, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.5.x' into 2.0.x
parents
959c5691
1314aaa3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
55 deletions
+68
-55
CacheAutoConfiguration.java
...work/boot/autoconfigure/cache/CacheAutoConfiguration.java
+17
-55
CacheAutoConfigurationTests.java
...boot/autoconfigure/cache/CacheAutoConfigurationTests.java
+51
-0
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfiguration.java
View file @
18b1e8da
...
...
@@ -18,14 +18,8 @@ package org.springframework.boot.autoconfigure.cache;
import
java.util.List
;
import
javax.annotation.PostConstruct
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.beans.factory.ObjectProvider
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.config.BeanDefinition
;
import
org.springframework.beans.factory.config.BeanFactoryPostProcessor
;
import
org.springframework.beans.factory.config.ConfigurableListableBeanFactory
;
import
org.springframework.boot.autoconfigure.AutoConfigureAfter
;
import
org.springframework.boot.autoconfigure.AutoConfigureBefore
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
...
...
@@ -46,7 +40,6 @@ import org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.context.annotation.ImportSelector
;
import
org.springframework.context.annotation.Role
;
import
org.springframework.core.type.AnnotationMetadata
;
import
org.springframework.orm.jpa.AbstractEntityManagerFactoryBean
;
import
org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
;
...
...
@@ -73,8 +66,6 @@ import org.springframework.util.Assert;
@Import
(
CacheConfigurationImportSelector
.
class
)
public
class
CacheAutoConfiguration
{
static
final
String
VALIDATOR_BEAN_NAME
=
"cacheAutoConfigurationValidator"
;
@Bean
@ConditionalOnMissingBean
public
CacheManagerCustomizers
cacheManagerCustomizers
(
...
...
@@ -83,14 +74,10 @@ public class CacheAutoConfiguration {
}
@Bean
@Role
(
BeanDefinition
.
ROLE_INFRASTRUCTURE
)
public
static
CacheManagerValidatorPostProcessor
cacheAutoConfigurationValidatorPostProcessor
()
{
return
new
CacheManagerValidatorPostProcessor
();
}
@Bean
(
name
=
VALIDATOR_BEAN_NAME
)
public
CacheManagerValidator
cacheAutoConfigurationValidator
()
{
return
new
CacheManagerValidator
();
public
CacheManagerValidator
cacheAutoConfigurationValidator
(
CacheProperties
cacheProperties
,
ObjectProvider
<
CacheManager
>
cacheManager
)
{
return
new
CacheManagerValidator
(
cacheProperties
,
cacheManager
);
}
@Configuration
...
...
@@ -105,50 +92,25 @@ public class CacheAutoConfiguration {
}
/**
* {@link BeanFactoryPostProcessor} to ensure that the {@link CacheManagerValidator}
* is triggered before {@link CacheAspectSupport} but without causing early
* instantiation.
*/
static
class
CacheManagerValidatorPostProcessor
implements
BeanFactoryPostProcessor
{
@Override
public
void
postProcessBeanFactory
(
ConfigurableListableBeanFactory
beanFactory
)
throws
BeansException
{
for
(
String
name
:
beanFactory
.
getBeanNamesForType
(
CacheAspectSupport
.
class
,
false
,
false
))
{
BeanDefinition
definition
=
beanFactory
.
getBeanDefinition
(
name
);
definition
.
setDependsOn
(
append
(
definition
.
getDependsOn
(),
VALIDATOR_BEAN_NAME
));
}
}
private
String
[]
append
(
String
[]
array
,
String
value
)
{
String
[]
result
=
new
String
[
array
!=
null
?
array
.
length
+
1
:
1
];
if
(
array
!=
null
)
{
System
.
arraycopy
(
array
,
0
,
result
,
0
,
array
.
length
);
}
result
[
result
.
length
-
1
]
=
value
;
return
result
;
}
}
/**
* Bean used to validate that a CacheManager exists and provide a more meaningful
* exception.
*/
static
class
CacheManagerValidator
{
static
class
CacheManagerValidator
implements
InitializingBean
{
private
final
CacheProperties
cacheProperties
;
@Autowired
private
CacheProperties
cacheProperties
;
private
final
ObjectProvider
<
CacheManager
>
cacheManager
;
@Autowired
(
required
=
false
)
private
CacheManager
cacheManager
;
CacheManagerValidator
(
CacheProperties
cacheProperties
,
ObjectProvider
<
CacheManager
>
cacheManager
)
{
this
.
cacheProperties
=
cacheProperties
;
this
.
cacheManager
=
cacheManager
;
}
@
PostConstruct
public
void
checkHasCacheManager
()
{
Assert
.
notNull
(
this
.
cacheManager
,
@
Override
public
void
afterPropertiesSet
()
{
Assert
.
notNull
(
this
.
cacheManager
.
getIfAvailable
()
,
()
->
"No cache manager could "
+
"be auto-configured, check your configuration (caching "
+
"type is '"
+
this
.
cacheProperties
.
getType
()
+
"')"
);
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java
View file @
18b1e8da
...
...
@@ -16,7 +16,9 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
cache
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
import
javax.cache.Caching
;
import
javax.cache.configuration.CompleteConfiguration
;
...
...
@@ -43,6 +45,7 @@ import org.junit.runner.RunWith;
import
org.springframework.beans.DirectFieldAccessor
;
import
org.springframework.beans.factory.BeanCreationException
;
import
org.springframework.beans.factory.config.BeanPostProcessor
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.autoconfigure.cache.support.MockCachingProvider
;
import
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration
;
...
...
@@ -745,6 +748,21 @@ public class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationT
.
run
(
this
::
validateCaffeineCacheWithStats
);
}
@Test
public
void
autoConfiguredCacheManagerCanBeSwapped
()
{
this
.
contextRunner
.
withUserConfiguration
(
CacheManagerPostProcessorConfiguration
.
class
)
.
withPropertyValues
(
"spring.cache.type=caffeine"
)
.
run
((
context
)
->
{
getCacheManager
(
context
,
SimpleCacheManager
.
class
);
CacheManagerPostProcessor
postProcessor
=
context
.
getBean
(
CacheManagerPostProcessor
.
class
);
assertThat
(
postProcessor
.
cacheManagers
).
hasSize
(
1
);
assertThat
(
postProcessor
.
cacheManagers
.
get
(
0
))
.
isInstanceOf
(
CaffeineCacheManager
.
class
);
});
}
private
void
validateCaffeineCacheWithStats
(
AssertableApplicationContext
context
)
{
CaffeineCacheManager
manager
=
getCacheManager
(
context
,
CaffeineCacheManager
.
class
);
...
...
@@ -1009,4 +1027,37 @@ public class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationT
}
@Configuration
@EnableCaching
static
class
CacheManagerPostProcessorConfiguration
{
@Bean
public
static
BeanPostProcessor
cacheManagerBeanPostProcessor
()
{
return
new
CacheManagerPostProcessor
();
}
}
private
static
class
CacheManagerPostProcessor
implements
BeanPostProcessor
{
private
final
List
<
CacheManager
>
cacheManagers
=
new
ArrayList
<>();
@Override
public
Object
postProcessBeforeInitialization
(
Object
bean
,
String
beanName
)
{
return
bean
;
}
@Override
public
Object
postProcessAfterInitialization
(
Object
bean
,
String
beanName
)
{
if
(
bean
instanceof
CacheManager
)
{
this
.
cacheManagers
.
add
((
CacheManager
)
bean
);
return
new
SimpleCacheManager
();
}
return
bean
;
}
}
}
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