-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add legacy configurations detect (#1161)
Co-authored-by: 致节 <hzj266771@antgroup.com>
- Loading branch information
1 parent
f93c89b
commit b984985
Showing
5 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
...java/com/alipay/sofa/boot/autoconfigure/detect/LegacyAutoConfigurationDetectListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 com.alipay.sofa.boot.autoconfigure.detect; | ||
|
||
import com.alipay.sofa.boot.log.SofaBootLoggerFactory; | ||
import org.slf4j.Logger; | ||
import org.springframework.beans.factory.BeanClassLoaderAware; | ||
import org.springframework.boot.autoconfigure.AutoConfigurationImportEvent; | ||
import org.springframework.boot.autoconfigure.AutoConfigurationImportListener; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.core.io.support.SpringFactoriesLoader; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* Implements for {@link AutoConfigurationImportListener} used to detect configurations defined in legacy model. | ||
* | ||
* @author huzijie | ||
* @version LegacyAutoConfigurationDetectListener.java, v 0.1 2023年04月18日 10:28 AM huzijie Exp $ | ||
*/ | ||
public class LegacyAutoConfigurationDetectListener implements AutoConfigurationImportListener, | ||
BeanClassLoaderAware { | ||
|
||
private static final Logger LOGGER = SofaBootLoggerFactory | ||
.getLogger(LegacyAutoConfigurationDetectListener.class); | ||
|
||
private ClassLoader beanClassLoader; | ||
|
||
@Override | ||
public void onAutoConfigurationImportEvent(AutoConfigurationImportEvent event) { | ||
// configurations form *.import file | ||
Set<String> importConfigurations = new HashSet<>(); | ||
importConfigurations.addAll(event.getCandidateConfigurations()); | ||
importConfigurations.addAll(event.getExclusions()); | ||
|
||
// configurations from spring.factories file | ||
List<String> configurations = new ArrayList<>( | ||
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, beanClassLoader)); | ||
|
||
// configurations which in spring.factories but not in *.import will be ignored. | ||
Set<String> legacyConfigurations = new HashSet<>(); | ||
configurations.forEach(className -> { | ||
if (!importConfigurations.contains(className)) { | ||
legacyConfigurations.add(className); | ||
} | ||
}); | ||
|
||
if (!legacyConfigurations.isEmpty()) { | ||
LOGGER.warn(builderWarnLog(legacyConfigurations).toString()); | ||
} | ||
} | ||
|
||
private StringBuilder builderWarnLog(Set<String> legacyConfigurations) { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
stringBuilder.append("These configurations defined in spring.factories file will be ignored:"); | ||
stringBuilder.append("\n"); | ||
legacyConfigurations.forEach(legacyConfiguration -> { | ||
stringBuilder.append("--- "); | ||
stringBuilder.append(legacyConfiguration); | ||
stringBuilder.append("\n"); | ||
}); | ||
return stringBuilder; | ||
} | ||
|
||
@Override | ||
public void setBeanClassLoader(ClassLoader classLoader) { | ||
this.beanClassLoader = classLoader; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
sofa-boot-project/sofa-boot-autoconfigure/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# AutoConfigurationImportListener | ||
org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\ | ||
com.alipay.sofa.boot.autoconfigure.detect.LegacyAutoConfigurationDetectListener |
79 changes: 79 additions & 0 deletions
79
...est/java/com/alipay/sofa/smoke/tests/boot/LegacyAutoConfigurationDetectListenerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 com.alipay.sofa.smoke.tests.boot; | ||
|
||
import com.alipay.sofa.boot.autoconfigure.detect.LegacyAutoConfigurationDetectListener; | ||
import com.alipay.sofa.boot.util.LogOutPutUtils; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.system.CapturedOutput; | ||
import org.springframework.boot.test.system.OutputCaptureExtension; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link LegacyAutoConfigurationDetectListener}. | ||
* | ||
* @author huzijie | ||
* @version LegacyAutoConfigurationDetectListenerTests.java, v 0.1 2023年04月18日 10:46 AM huzijie Exp $ | ||
*/ | ||
@SpringBootTest(classes = BootSofaBootApplication.class) | ||
@ExtendWith(OutputCaptureExtension.class) | ||
public class LegacyAutoConfigurationDetectListenerTests { | ||
|
||
static { | ||
LogOutPutUtils.openOutPutForLoggers(LegacyAutoConfigurationDetectListener.class); | ||
} | ||
|
||
@Autowired | ||
private ApplicationContext applicationContext; | ||
|
||
@Test | ||
public void checkWarnLog(CapturedOutput capturedOutput) { | ||
assertThat(applicationContext.containsBean("legacyBean")).isFalse(); | ||
assertThat(applicationContext.containsBean("bothBean")).isTrue(); | ||
|
||
assertThat(capturedOutput.getAll()).contains( | ||
"These configurations defined in spring.factories file will be ignored:"); | ||
assertThat(capturedOutput.getAll()) | ||
.contains( | ||
"--- com.alipay.sofa.smoke.tests.boot.LegacyAutoConfigurationDetectListenerTests$LegacyAutoConfiguration"); | ||
} | ||
|
||
@AutoConfiguration | ||
public static class LegacyAutoConfiguration { | ||
|
||
@Bean | ||
public Object legacyBean() { | ||
return new Object(); | ||
} | ||
} | ||
|
||
@AutoConfiguration | ||
public static class BothAutoConfiguration { | ||
|
||
@Bean | ||
public Object bothBean() { | ||
return new Object(); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...-boot-smoke-tests/sofa-boot-smoke-tests-boot/src/test/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ | ||
com.alipay.sofa.smoke.tests.boot.LegacyAutoConfigurationDetectListenerTests$LegacyAutoConfiguration,\ | ||
com.alipay.sofa.smoke.tests.boot.LegacyAutoConfigurationDetectListenerTests$BothAutoConfiguration |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.alipay.sofa.smoke.tests.boot.LegacyAutoConfigurationDetectListenerTests$BothAutoConfiguration |