Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: custom endpoints to rotuer function register for plugin
- Loading branch information
Showing
4 changed files
with
88 additions
and
43 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
application/src/main/java/run/halo/app/plugin/BeanPostProcessorUtils.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,50 @@ | ||
package run.halo.app.plugin; | ||
|
||
import lombok.experimental.UtilityClass; | ||
import org.springframework.beans.factory.config.BeanDefinition; | ||
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | ||
import org.springframework.beans.factory.support.RootBeanDefinition; | ||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* Utility class that allows for convenient registration of common bean post-processors for | ||
* {@link PluginApplicationContext}. | ||
* | ||
* @author guqing | ||
* @since 2.11.0 | ||
*/ | ||
@UtilityClass | ||
public class BeanPostProcessorUtils { | ||
|
||
public static final String CUSTOM_ENDPOINT_PROCESSOR_BEAN_NAME = | ||
"run.halo.app.plugin.pluginCustomEndpointBeanFactoryPostProcessor"; | ||
|
||
/** | ||
* Register all relevant annotation post-processors in the given registry. | ||
* | ||
* @param registry the registry to operate on | ||
*/ | ||
public static void registerBeanPostProcessors(BeanDefinitionRegistry registry) { | ||
registerBeanPostProcessors(registry, null); | ||
} | ||
|
||
/** | ||
* Register all relevant annotation post-processors in the given registry. | ||
* | ||
* @param registry the registry to operate on | ||
* @param source the configuration source element (already extracted) | ||
* that this registration was triggered from. May be {@code null} | ||
*/ | ||
public static void registerBeanPostProcessors( | ||
BeanDefinitionRegistry registry, @Nullable Object source) { | ||
|
||
if (!registry.containsBeanDefinition(CUSTOM_ENDPOINT_PROCESSOR_BEAN_NAME)) { | ||
RootBeanDefinition def = new RootBeanDefinition( | ||
PluginCustomEndpointBeanFactoryPostProcessor.class); | ||
def.setSource(source); | ||
def.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); | ||
registry.registerBeanDefinition(CUSTOM_ENDPOINT_PROCESSOR_BEAN_NAME, def); | ||
} | ||
// add more post processors | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
...ation/src/main/java/run/halo/app/plugin/PluginCustomEndpointBeanFactoryPostProcessor.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,34 @@ | ||
package run.halo.app.plugin; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.web.reactive.function.server.RouterFunction; | ||
import run.halo.app.core.extension.endpoint.CustomEndpoint; | ||
import run.halo.app.core.extension.endpoint.CustomEndpointsBuilder; | ||
|
||
/** | ||
* <p>A {@link CustomEndpoint} initialization {@link BeanFactoryPostProcessor} to build | ||
* {@link RouterFunction}s to bean factory.</p> | ||
* | ||
* @author guqing | ||
* @since 2.11.0 | ||
*/ | ||
public class PluginCustomEndpointBeanFactoryPostProcessor implements BeanFactoryPostProcessor { | ||
|
||
@Override | ||
public void postProcessBeanFactory(@NonNull ConfigurableListableBeanFactory beanFactory) | ||
throws BeansException { | ||
String[] customEndpointBeans = beanFactory.getBeanNamesForType(CustomEndpoint.class); | ||
if (customEndpointBeans.length == 0) { | ||
return; | ||
} | ||
CustomEndpointsBuilder endpointBuilder = new CustomEndpointsBuilder(); | ||
for (String beanName : customEndpointBeans) { | ||
CustomEndpoint customEndpoint = (CustomEndpoint) beanFactory.getBean(beanName); | ||
endpointBuilder.add(customEndpoint); | ||
} | ||
beanFactory.registerSingleton("pluginCustomEndpointRouter", endpointBuilder.build()); | ||
} | ||
} |
43 changes: 0 additions & 43 deletions
43
application/src/main/java/run/halo/app/plugin/PluginCustomEndpointInitialization.java
This file was deleted.
Oops, something went wrong.