Home > Blockchain >  Is @PostConstruct method called for a static method?
Is @PostConstruct method called for a static method?

Time:10-23

I am wondering whether @PostConstruct method is ever called when a static method from a component is called for e.g.

@Component
public class SomeComponent{
@PostConstruct
void init(){ // set up static variables};

SomeComponent{};

public static someStaticMethod{};
}
@Service
public class SomeService{

public method(){
SomeComponent.someStaticMethod(); //is @PostConstructor called here?
}

Is @PostConstructor method ever called when the static method of the component is called above? If not, in what way can the @PostConstructor method be called? Much thanks!

CodePudding user response:

No, @PostConstruct can not be applied to a static method. The whole point of this annotation is to be called on a method of a Bean after the bean has been constructed. In the case of a static method, there is no corresponding Bean (Java object) and so it doesn't make sense to have a static @PostConstruct method.

  • Related