Код: Выделить всё
@Slf4j
@Component
public class HandlerMappingAfterSpringInitializationExporter implements ApplicationListener {
@Autowired
private ApplicationContext applicationContext;
private volatile boolean exported = false;
private volatile int count = 0;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
synchronized (HandlerMappingAfterSpringInitializationExporter.class) {
count+=1;
if (exported) {
return;
}
try {
//here are some other io operations like File.write() which are ignored
log.info("the value of count is :{}",count);
log.info("the value of exported is :{}",exported)
} catch (Exception e) {
System.err.println("导出Web组件信息失败: " + e.getMessage());
e.printStackTrace();
}
exported = true;
}
}
}
< /code>
Первый, как ниже < /p>
"the value of count is :1"
"the value of exported is :false"
< /code>
Второй, как показано ниже < /p>
"the value of count is :2"
"the value of exported is :false"
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
< /code>
Я пишу код, чтобы экспортировать некоторую информацию о весенних бобах только на этот раз, поэтому я использую Synchronizde и Electile. Спасибо.
Подробнее здесь: https://stackoverflow.com/questions/797 ... ace-in-spr
Мобильная версия