0

I wish to use the @PostInitialize annotation in one of my bean to load few properties after initialization is completed. Can someone please let me know where can I find this annotation? What maven dependency I need to add to get this annotation? Why is this not part of Spring 3.2.4 or am I missing some package of spring which I should have added?

Please advise

UPDATE

I tried using resthubs @PostInitialize but it does not work. Do I have to do any other configuration to get this annotation to work.

Pratik Shelar
  • 3,154
  • 7
  • 31
  • 51
  • Do you mean RESTHub's @PostInitialize? http://resthub.org/javadoc/2.0/org/resthub/common/util/PostInitialize.html – mttdbrd Jan 16 '14 at 14:34
  • Do I really need to use resthubs @Postinitialize. So just for one annotation I will have to have entire dependency which I will not be using anyways. – Pratik Shelar Jan 16 '14 at 14:39
  • 2
    If you read the link above you can see that PostInitialize is like Spring's @PostConstruct annotation: "Similar to PostConstruct Spring annotation, but executed later in application lifecycle, in order to get transaction management ready." – mttdbrd Jan 16 '14 at 14:42

4 Answers4

2

I think you mean @PostConstruct.

From the 3.2.4 reference manual,

The JSR-250 @PostConstruct and @PreDestroy annotations are generally considered best practice for receiving lifecycle callbacks in a modern Spring application. Using these annotations means that your beans are not coupled to Spring specific interfaces.

Emerson Farrugia
  • 11,153
  • 5
  • 43
  • 51
1

What you request is the patch from the Spring improvement request https://jira.spring.io/browse/SPR-5966.

Gedrox
  • 3,592
  • 1
  • 21
  • 29
0

RestHub has an @PostInitialize annotation. Checkout Resthub @GitHub or Resthub API.

<dependency>
    <groupId>org.resthub</groupId>
    <artifactId>resthub-common</artifactId>
    <version>2.1.0</version>
</dependency>
Pang
  • 9,564
  • 146
  • 81
  • 122
Ben Asmussen
  • 964
  • 11
  • 15
0

@PostInitialize and @PostConstruct are different.

In most cases you will be OK with @PostConstruct.

BUT!

The @PostContruct it's not called via spring proxies so for example the @Transactional won't work with @PostConstruct.

Because of these situations @PostInitialize is invented, which is called after context refreshed and it will be called via proxy so @Transactional will work with it.

Spring doesn't have @PostInitialize by default (because of lazy init beans) but you can add this dependency and you will have it:

<dependency>
    <groupId>fr.zebasto</groupId>
    <artifactId>spring-postinitialize</artifactId>
    <version>1.4.0</version>
</dependency>

Find more info here: https://github.com/bcecchinato/spring-postinitialize

Nagy Attila
  • 1,119
  • 1
  • 10
  • 14