0
我想知道如何確保Spring配置文件被加載一次。任何設置來實現這一目標?如何保證彈簧配置只能加載一次?
我想知道如何確保Spring配置文件被加載一次。任何設置來實現這一目標?如何保證彈簧配置只能加載一次?
最簡單的方法是配置某種豆與靜態康特變量指望它多久initzalized。
如果該計數器更多的則是零,如果在創建bean時,那麼這個限制它volated和異常應thorwn。
@Component
public OnlyOneInstanceGuard() {
private static int instanceCounter = 0;
public OnlyOneInstanceGuard() {
if(!isFirstInstance) {
throw new RuntimeException(
"is not the first instance, let the spring context crash!");
}
}
private syncronized isFirstInstance() {
instanceCounter++;
return instanceCounter == 1;
}
}