0
点赞
收藏
分享

微信扫一扫

不同类型的Java类变量初始化方法

大柚子top 2022-11-06 阅读 195


Map类型

使用static块

public class LogbackLoggingSystem extends Slf4JLoggingSystem {

private static final String CONFIGURATION_FILE_PROPERTY = "logback.configurationFile";

private static final LogLevels<Level> LEVELS = new LogLevels<>();

static {
LEVELS.map(LogLevel.TRACE, Level.TRACE);
LEVELS.map(LogLevel.TRACE, Level.ALL);
LEVELS.map(LogLevel.DEBUG, Level.DEBUG);
LEVELS.map(LogLevel.INFO, Level.INFO);
LEVELS.map(LogLevel.WARN, Level.WARN);
LEVELS.map(LogLevel.ERROR, Level.ERROR);
LEVELS.map(LogLevel.FATAL, Level.ERROR);
LEVELS.map(LogLevel.OFF, Level.OFF);
}


数组

private static final Class<?>[] EVENT_TYPES = { ApplicationStartingEvent.class,
ApplicationEnvironmentPreparedEvent.class, ApplicationPreparedEvent.class,
ContextClosedEvent.class, ApplicationFailedEvent.class };


List类型

也是用static块

public class ApplicationPidFileWriter
implements ApplicationListener<SpringApplicationEvent>, Ordered {

private static final Log logger = LogFactory.getLog(ApplicationPidFileWriter.class);

private static final String DEFAULT_FILE_NAME = "application.pid";

private static final List<Property> FILE_PROPERTIES;

static {
List<Property> properties = new ArrayList<>();
properties.add(new SpringProperty("spring.pid.", "file"));
properties.add(new SpringProperty("spring.", "pidfile"));
properties.add(new SystemProperty("PIDFILE"));
FILE_PROPERTIES = Collections.unmodifiableList(properties);
}

private static final List<Property> FAIL_ON_WRITE_ERROR_PROPERTIES;

static {
List<Property> properties = new ArrayList<>();
properties.add(new SpringProperty("spring.pid.", "fail-on-write-error"));
properties.add(new SystemProperty("PID_FAIL_ON_WRITE_ERROR"));
FAIL_ON_WRITE_ERROR_PROPERTIES = Collections.unmodifiableList(properties);
}

举报

相关推荐

0 条评论