【轉】子類會調用父類的@PostConstruct方法

  若是一個類用@Service或@Component,那麼只須要用@PostConstruct修飾某個方法,該方法能在類實例化的過程當中自動執行,至關於類的構造函數。同時,具有了構造函數不具有的功能。框架

@Component
class Test{
    @PostConstruct
    public void sayHello(){
        System.out.println("Hello!");
    }
}

  可是須要注意:函數

  子類實例化過程當中會調用父類中的@PostConstruct方法spa

@Component 
class TestChild extends Test{
    @PostConstruct
    public void sayBye(){
        System.out.println("Bye!");
    }
}

  啓動項目,輸出以下:.net

Hello! 
Bye!

  轉載自《慎用Spring框架-子類會調用父類的@PostConstruct方法code

相關文章
相關標籤/搜索