2

I want to refactor a method annotated with @PostContruct in a common class of all my controller.

public abstract class Controller {
    @PostConstruct
    protected void PostContruct() { ..}
}

public class AuthController extends Controller {}

public class CartController extends Controller {}

But spring doesn't seems to call my inherit method. What is the pattern to use in this situation ?

mathk
  • 7,973
  • 6
  • 45
  • 74
  • Why don't you call `PostContruct()` method from sub class `@PostConstruct` method? – Naman Gala Aug 14 '15 at 10:05
  • The annotation isn't inherited, so when overriding you basically remove the annotation. – M. Deinum Aug 14 '15 at 10:07
  • @NamanGala the whole point of adding an annotation is to let spring call the method for you. – mathk Aug 14 '15 at 10:11
  • @M.Deinum You mean that you do not inherit the annotation? (I am not overriding anything here :) ) – mathk Aug 14 '15 at 10:12
  • @mathk, that is what I am telling, you can create a method in sub-class annotated with `@PostConstruct` and then call your super class `postContruct()` method from it. This way spring will call your sub-class method and internally super class method is called. – Naman Gala Aug 14 '15 at 10:13
  • 1
    I'm using spring 4.2.0 (with spring-boot 1.2.5) and `@PostConstruct`'s method is always called: in the parent, in the child, in an overriden method and in an abstract method – cahen Aug 14 '15 at 10:17
  • 1
    @NamanGala already thought about this solution but it is a hack. Fact is that spring should have scan the entire accessible method. – mathk Aug 14 '15 at 10:19
  • @CarlosRodriguez what do you mean by a method being call in a parent ? You can call/apply a method to an object. And the method could have been define in a parent class of that object. Call in a parent/child... is obscure to me. – mathk Aug 14 '15 at 10:27
  • Sitting there with a breakpoint clearly show that the @PostContruct method is not called. I might be doing something else wrong. – mathk Aug 14 '15 at 10:30
  • @mathk maybe I should have said "inherited from the parent" instead of "in the parent". My point was that `@PostConstruct` worked everywhere in my project, including the scenario you described in the question – cahen Aug 14 '15 at 10:31
  • @CarlosRodriguez Is your inherit method declared as protected ? – mathk Aug 14 '15 at 10:33
  • @mathk I tested your code and it is working as expected. Can you tell me how you are configuring your beans? `XML`/`Explicit JavaCongif`/`Implicit bean discovery`? – Naman Gala Aug 14 '15 at 10:48
  • 1
    Ok many thing was going wrong here. I am using amq and it was not getting initialized due to the fact that I had error when starting amq (Me not paying attention). So the thread was block waiting for the connection. My `PostContruct` method was then never called. Leading me to the wrong conclusion that is was due to the change I introduced. Thanks all for letting me know that it was possible to have an inherit PostConstruct method – mathk Aug 14 '15 at 11:01
  • @mathk, Kindly update your question so that it does not create confusion in future whoever finds this question. – Naman Gala Aug 14 '15 at 11:07

1 Answers1

2

This works with Spring 4.2.0 and Spring Boot 1.2.5

public abstract class AbstractController {
    @PostConstruct
    protected void postConstruct() {
        System.out.println("post construct");
    }
}

@Controller
public class ConcreteController extends AbstractController {

}

It also works if you mark the method as abstract, keep the @PostConstruct in the parent and implement it in the child.

It does NOT work if @Controller is in the parent.

cahen
  • 15,807
  • 13
  • 47
  • 78