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
b80620fe
Commit
b80620fe
authored
Mar 14, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Revert "Group auto-configuration import selectors together"
This reverts commit
26d9c261
parent
282bd9f0
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
219 deletions
+37
-219
AutoConfigurationImportSelector.java
...k/boot/autoconfigure/AutoConfigurationImportSelector.java
+37
-87
AutoConfigurationImportSelectorIntegrationTests.java
...gure/AutoConfigurationImportSelectorIntegrationTests.java
+0
-132
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationImportSelector.java
View file @
b80620fe
...
...
@@ -16,16 +16,14 @@
package
org
.
springframework
.
boot
.
autoconfigure
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.LinkedHashMap
;
import
java.util.LinkedHashSet
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.concurrent.TimeUnit
;
import
java.util.stream.Collectors
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
...
...
@@ -90,12 +88,14 @@ public class AutoConfigurationImportSelector
if
(!
isEnabled
(
annotationMetadata
))
{
return
NO_IMPORTS
;
}
try
{
AutoConfigurationMetadata
autoConfigurationMetadata
=
AutoConfigurationMetadataLoader
.
loadMetadata
(
this
.
beanClassLoader
);
AnnotationAttributes
attributes
=
getAttributes
(
annotationMetadata
);
List
<
String
>
configurations
=
getCandidateConfigurations
(
annotationMetadata
,
attributes
);
configurations
=
removeDuplicates
(
configurations
);
configurations
=
sort
(
configurations
,
autoConfigurationMetadata
);
Set
<
String
>
exclusions
=
getExclusions
(
annotationMetadata
,
attributes
);
checkExcludedClasses
(
configurations
,
exclusions
);
configurations
.
removeAll
(
exclusions
);
...
...
@@ -103,10 +103,9 @@ public class AutoConfigurationImportSelector
fireAutoConfigurationImportEvents
(
configurations
,
exclusions
);
return
StringUtils
.
toStringArray
(
configurations
);
}
@Override
public
Class
<?
extends
Group
>
getImportGroup
()
{
return
AutoConfigurationGroup
.
class
;
catch
(
IOException
ex
)
{
throw
new
IllegalStateException
(
ex
);
}
}
protected
boolean
isEnabled
(
AnnotationMetadata
metadata
)
{
...
...
@@ -227,6 +226,13 @@ public class AutoConfigurationImportSelector
return
(
excludes
==
null
?
Collections
.
emptyList
()
:
Arrays
.
asList
(
excludes
));
}
private
List
<
String
>
sort
(
List
<
String
>
configurations
,
AutoConfigurationMetadata
autoConfigurationMetadata
)
throws
IOException
{
configurations
=
new
AutoConfigurationSorter
(
getMetadataReaderFactory
(),
autoConfigurationMetadata
).
getInPriorityOrder
(
configurations
);
return
configurations
;
}
private
List
<
String
>
filter
(
List
<
String
>
configurations
,
AutoConfigurationMetadata
autoConfigurationMetadata
)
{
long
startTime
=
System
.
nanoTime
();
...
...
@@ -266,6 +272,17 @@ public class AutoConfigurationImportSelector
this
.
beanClassLoader
);
}
private
MetadataReaderFactory
getMetadataReaderFactory
()
{
try
{
return
getBeanFactory
().
getBean
(
SharedMetadataReaderFactoryContextInitializer
.
BEAN_NAME
,
MetadataReaderFactory
.
class
);
}
catch
(
NoSuchBeanDefinitionException
ex
)
{
return
new
CachingMetadataReaderFactory
(
this
.
resourceLoader
);
}
}
protected
final
<
T
>
List
<
T
>
removeDuplicates
(
List
<
T
>
list
)
{
return
new
ArrayList
<>(
new
LinkedHashSet
<>(
list
));
}
...
...
@@ -353,71 +370,4 @@ public class AutoConfigurationImportSelector
return
Ordered
.
LOWEST_PRECEDENCE
-
1
;
}
private
static
class
AutoConfigurationGroup
implements
DeferredImportSelector
.
Group
,
BeanClassLoaderAware
,
BeanFactoryAware
,
ResourceLoaderAware
{
private
ClassLoader
beanClassLoader
;
private
BeanFactory
beanFactory
;
private
ResourceLoader
resourceLoader
;
private
final
Map
<
String
,
AnnotationMetadata
>
entries
=
new
LinkedHashMap
<>();
@Override
public
void
setBeanClassLoader
(
ClassLoader
classLoader
)
{
this
.
beanClassLoader
=
classLoader
;
}
@Override
public
void
setBeanFactory
(
BeanFactory
beanFactory
)
{
this
.
beanFactory
=
beanFactory
;
}
@Override
public
void
setResourceLoader
(
ResourceLoader
resourceLoader
)
{
this
.
resourceLoader
=
resourceLoader
;
}
@Override
public
void
process
(
AnnotationMetadata
annotationMetadata
,
DeferredImportSelector
deferredImportSelector
)
{
String
[]
imports
=
deferredImportSelector
.
selectImports
(
annotationMetadata
);
for
(
String
importClassName
:
imports
)
{
this
.
entries
.
put
(
importClassName
,
annotationMetadata
);
}
}
@Override
public
Iterable
<
Entry
>
selectImports
()
{
return
sortAutoConfigurations
().
stream
().
map
((
importClassName
)
->
new
Entry
(
this
.
entries
.
get
(
importClassName
),
importClassName
))
.
collect
(
Collectors
.
toList
());
}
private
List
<
String
>
sortAutoConfigurations
()
{
List
<
String
>
autoConfigurations
=
new
ArrayList
<>(
this
.
entries
.
keySet
());
if
(
this
.
entries
.
size
()
<=
1
)
{
return
autoConfigurations
;
}
AutoConfigurationMetadata
autoConfigurationMetadata
=
AutoConfigurationMetadataLoader
.
loadMetadata
(
this
.
beanClassLoader
);
return
new
AutoConfigurationSorter
(
getMetadataReaderFactory
(),
autoConfigurationMetadata
).
getInPriorityOrder
(
autoConfigurations
);
}
private
MetadataReaderFactory
getMetadataReaderFactory
()
{
try
{
return
this
.
beanFactory
.
getBean
(
SharedMetadataReaderFactoryContextInitializer
.
BEAN_NAME
,
MetadataReaderFactory
.
class
);
}
catch
(
NoSuchBeanDefinitionException
ex
)
{
return
new
CachingMetadataReaderFactory
(
this
.
resourceLoader
);
}
}
}
}
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationImportSelectorIntegrationTests.java
deleted
100644 → 0
View file @
282bd9f0
/*
* Copyright 2012-2018 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
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.junit.Test
;
import
org.springframework.boot.test.context.assertj.AssertableApplicationContext
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.util.ClassUtils
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
/**
* Integration tests for {@link AutoConfigurationImportSelector}.
*
* @author Stephane Nicoll
*/
public
class
AutoConfigurationImportSelectorIntegrationTests
{
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
();
@Test
public
void
singleSelectorWithNoImports
()
{
this
.
contextRunner
.
withUserConfiguration
(
NoConfig
.
class
)
.
run
((
context
)
->
assertThat
(
getImportedConfigBeans
(
context
)).
isEmpty
());
}
@Test
public
void
singleSelector
()
{
this
.
contextRunner
.
withUserConfiguration
(
SingleConfig
.
class
)
.
run
((
context
)
->
{
assertThat
(
getImportedConfigBeans
(
context
)).
containsExactly
(
"ConfigC"
);
});
}
@Test
public
void
multipleSelectorShouldMergeAndSortCorrectly
()
{
this
.
contextRunner
.
withUserConfiguration
(
Config
.
class
,
AnotherConfig
.
class
)
.
run
((
context
)
->
{
assertThat
(
getImportedConfigBeans
(
context
)).
containsExactly
(
"ConfigA"
,
"ConfigB"
,
"ConfigC"
,
"ConfigD"
);
});
}
@Test
public
void
multipleSelectorWithRedundantImportsShouldMergeAndSortCorrectly
()
{
this
.
contextRunner
.
withUserConfiguration
(
SingleConfig
.
class
,
Config
.
class
,
AnotherConfig
.
class
).
run
((
context
)
->
{
assertThat
(
getImportedConfigBeans
(
context
)).
containsExactly
(
"ConfigA"
,
"ConfigB"
,
"ConfigC"
,
"ConfigD"
);
});
}
private
List
<
String
>
getImportedConfigBeans
(
AssertableApplicationContext
context
)
{
String
shortName
=
ClassUtils
.
getShortName
(
AutoConfigurationImportSelectorIntegrationTests
.
class
);
int
beginIndex
=
shortName
.
length
()
+
1
;
List
<
String
>
orderedConfigBeans
=
new
ArrayList
<>();
for
(
String
bean
:
context
.
getBeanDefinitionNames
())
{
if
(
bean
.
contains
(
"$Config"
))
{
String
shortBeanName
=
ClassUtils
.
getShortName
(
bean
);
orderedConfigBeans
.
add
(
shortBeanName
.
substring
(
beginIndex
));
}
}
return
orderedConfigBeans
;
}
@ImportAutoConfiguration
static
class
NoConfig
{
}
@ImportAutoConfiguration
(
ConfigC
.
class
)
static
class
SingleConfig
{
}
@ImportAutoConfiguration
({
ConfigD
.
class
,
ConfigB
.
class
})
static
class
Config
{
}
@ImportAutoConfiguration
({
ConfigC
.
class
,
ConfigA
.
class
})
static
class
AnotherConfig
{
}
@Configuration
static
class
ConfigA
{
}
@Configuration
@AutoConfigureAfter
(
ConfigA
.
class
)
@AutoConfigureBefore
(
ConfigC
.
class
)
static
class
ConfigB
{
}
@Configuration
static
class
ConfigC
{
}
@Configuration
@AutoConfigureAfter
(
ConfigC
.
class
)
static
class
ConfigD
{
}
}
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