当前位置:网站首页>Implement a middleware from -1
Implement a middleware from -1
2022-06-21 22:08:00 【InfoQ】
New projects

test-infrademo<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.aixiaoxian.infra</groupId>
<artifactId>aixiaoxian-infra</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>aixiaoxian-infra</name>
<description>aixiaoxian-infra</description>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<modules>
<module>demo</module>
<module>test-infra</module>
</modules>
<dependencies>
</dependencies>
<build>
<plugins>
<!-- Source -->
<plugin>
<artifactId>maven-source-plugin</artifactId>
<inherited>true</inherited>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Developing middleware
resourcesMETA-INFA/spring.factoriesorg.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.aixiaoxian.testInfra.config.TestConfiguration
TestConfiguration@Bean@Configuration
public class TestConfiguration implements BeanDefinitionRegistryPostProcessor, EnvironmentAware {
private Environment environment;
@Bean
public TestManager getTestManager() {
return new TestManager();
}
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}
TestManager@Slf4j
public class TestManager {
public TestManager() {
init();
}
public void init(){
log.info("TestManager start");
}
}
demo <dependency>
<groupId>com.aixiaoxian.infra</groupId>
<artifactId>test-infra</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Change position
BeanBeanDefinitionRegistryPostProcessorTestConfigurationpostProcessBeanDefinitionRegistryaiManager@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(AiManager.class);
beanDefinitionBuilder.addConstructorArgValue(this.environment);
beanDefinitionRegistry.registerBeanDefinition("aiManager", beanDefinitionBuilder.getBeanDefinition());
}
@Slf4j
public class AiManager {
private Environment environment;
public AiManager(Environment environment) {
this.environment = environment;
init();
}
public void init(){
log.info("AiManager start");
}
}
Another pose
@ImportTestImportSelectorTestImportSelector@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import({TestImportSelector.class})
@Documented
public @interface TestAnnotation {
}
public class TestImportSelector implements DeferredImportSelector {
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
return new String[]{AnnotationConfiguration.class.getName()};
}
}
AnnotationConfigurationpublic class AnnotationConfiguration implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(AnnotationManager.class);
beanDefinitionRegistry.registerBeanDefinition("annotationManager", beanDefinitionBuilder.getBeanDefinition());
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
}
}
@Slf4j
public class AnnotationManager {
public AnnotationManager() {
init();
}
public void init(){
log.info("AnnotationManager start");
}
}
demo@SpringBootApplication
@TestAnnotation
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2022-06-21 19:05:07.433 INFO 4598 --- [ main] c.a.testInfra.manager.TestManager : TestManager start
2022-06-21 19:05:07.456 INFO 4598 --- [ main] c.a.testInfra.manager.AiManager : AiManager start
2022-06-21 19:05:07.456 INFO 4598 --- [ main] c.a.testInfra.manager.AnnotationManager : AnnotationManager start
边栏推荐
- 搭建Eureka-Server集群
- Jerry's near end tone change problem of opening four channel call [chapter]
- Ruiji housekeeper, a century old classic, is waiting for your elegant journey to start again
- Pinduoduo 618 mobile phone brand official flag sales increased by 124% year-on-year, and 4000+ high-priced mobile phones increased by 156% year-on-year
- Precautions for Jerry's external radio [chapter]
- 企业数据防泄漏解决方案分享
- 华为鸿蒙开发第三课
- Abbkine细胞周期染色试剂盒特色和实验建议
- 洛谷P1514 [NOIP2010 提高组] 引水入城 题解
- How to search foreign literature efficiently?
猜你喜欢

Excuse me, which website is better for college students to check literature?

C# AboutBox怎么显示自己定义的界面

新能源行业商业采购协同系统:赋能新能源行业采购业务,提升产业协同

先进封装,一个大周期的开始——“迎风国潮”半导体设备研讨会

I2C【2】-IIC为什么需要用开漏输出和上拉电阻bug

#16迭代器经典案例

C language array and pointer exercises (original question + analysis + original code)

Yx2811 landscape installation driver IC

杰理之蓝牙发射器的搜索设备的时间修改方法【篇】

JS异步的执行顺序是什么
随机推荐
六个拿来就能用的有趣网页特效
B2B mall website helps enterprises speed up distribution and build an efficient and intelligent B2B online distribution platform
ADT Spec RI AF CheckRep Safety from Rep Exposure
杰理之蓝牙发射器的搜索设备的时间修改方法【篇】
Abbkine细胞周期染色试剂盒特色和实验建议
Fu · new life, chain · future! The conference on enabling innovation and development of urban chain technology industry was held grandly
phpmailer 通过smtp发送邮件,相同发送内容有的成功有的失败
杰理之外挂收音注意事项【篇】
从随便到无聊到有病
Pinduoduo 618 mobile phone brand official flag sales increased by 124% year-on-year, and 4000+ high-priced mobile phones increased by 156% year-on-year
Jerry's acquisition of current audio file (long) file name and current (long) folder name [chapter]
三维度八视图
ADT Spec RI AF CheckRep Safety from Rep Exposure
Huawei Hongmeng development lesson 3
Supplier management system of digital commerce cloud Paper Group: promote enterprise information construction and comprehensively improve supplier management efficiency
龙蜥社区成立云原生SIG,引入3大核心技术
How to use the free and easy-to-use reference management software Zotero? Can I support both Chinese and English
Xr34082a high efficiency boost dc/dc regulator IC
广东疾控提醒:暑期将至,返粤大学生这样安全“归巢”
How to write a proposal for an English paper?