Imagine, you have a bean with an init method, annotated with @PostConstruct and a configuration class, referencing the same init method on its own.
public class MyBean {
@PostConstruct
public void init() {
...
}
}
@Configuration
public class MyConfiguration {
@Bean(initMethod = "init")
public MyBean myBean() {
return new MyBean();
}
}
The init method will be invoked only once (what is pretty well).
This is true for all method modifiers except private.
As soon as you change the method modifier to private the init method is invoked twice.
public class MyBean {
@PostConstruct
- public void init() {
+ private void init() {
...
}
}
Imagine, you have a bean with an
initmethod, annotated with@PostConstructand a configuration class, referencing the sameinitmethod on its own.The
initmethod will be invoked only once (what is pretty well).This is true for all method modifiers except
private.As soon as you change the method modifier to
privatetheinitmethod is invoked twice.public class MyBean { @PostConstruct - public void init() { + private void init() { ... } }