Spring ImportBeanDefinitionRegistrar 使用

EnableJPipeConfiguration

自定义注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import org.springframework.context.annotation.Import;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* enable bigPipe
*
* @author Created by YL on 2018/8/15
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Import({JPipeConfigurationRegistrar.class})
public @interface EnableJPipeConfiguration {
/**
* the maximum number of threads to allow in the pool
*/
int maxPoolSize() default 1024;

/**
* when the number of threads is greater than the core, this is the maximum time that excess idle threads will
* wait for new tasks before terminating.
* 单位:s
*/
int keepAliveTime() default 60;

/**
* the queue to use for holding tasks before they are executed. This queue will hold only the {@code Runnable}
* tasks submitted by the {@code execute}
* method.
*/
int workQueueSize() default 512;
}

JPipeConfigurationRegistrar

读取自定义注解配置,注册 bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;

/**
* @author Created by YL on 2018/8/16
*/
@Slf4j
public class JPipeConfigurationRegistrar implements ImportBeanDefinitionRegistrar {

@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
AnnotationAttributes attr = AnnotationAttributes.fromMap(metadata
.getAnnotationAttributes(EnableJPipeConfiguration.class.getName()));
if (attr != null) {
int maxPoolSize = attr.getNumber("maxPoolSize");
int keepAliveTime = attr.getNumber("keepAliveTime");
int workQueueSize = attr.getNumber("workQueueSize");

log.info("maxPoolSize: {}", maxPoolSize);
log.info("keepAliveTime: {}", keepAliveTime);
log.info("workQueueSize: {}", workQueueSize);
}
}
}
  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/8246f0e2.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!