Questions tagged [postconstruct]

@PostConstruct is a Java EE annotation used to select a method that needs to be executed after dependency injection is done to perform any initialization.

The @PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. This method MUST be invoked before the class is put into service.

This annotation is recognized by all Java EE compliant containers as well as .

See also

242 questions
12
votes
1 answer

JSF bean: call @PostConstruct function after ViewParam is set

I have a product.xhtml and a ProductBean. I use /product/{id} to access the products so I have a viewParam in product.xhtml with value=ProductBean.id. The problem is that inside the bean I use an init function with a PostConstruct annotation in…
user579674
  • 2,159
  • 6
  • 30
  • 40
10
votes
1 answer

@PostConstruct method is called even if the ManagedBean has already been instantiated (e.g. on AJAX-calls)

I have a @ViewScope ManagedBean and a @PostConstruct initialisation method. This method is called when a new instance is created, but also on every ajax call. Why is this so? On an AJAX-call the init-Method is called and executed, but no changes are…
elisman
  • 143
  • 1
  • 1
  • 8
10
votes
2 answers

Spring @PostConstruct depending on @Profile

I'd like to have multiple @PostConstruct annotated methods in one configuration class, that should be called dependent on the @Profile. You can imagine a code snipped like this: @Configuration public class SilentaConfiguration { private static…
Adam Bogdan Boczek
  • 1,720
  • 6
  • 21
  • 34
10
votes
2 answers

Spring singleton being called twice

getting some problem into my spring application. I have very fairly simple spring beans, they are injected into various other spring beans. While debugging I found, they are being called twice, Constructor & @PostConstruct both called two times. My…
Faisal Basra
  • 1,624
  • 4
  • 25
  • 40
9
votes
2 answers

Java 11 - Replace Spring @PostConstruct with afterPropertiesSet or using initMethod

I'm using spring applications which sometimes uses @PostConstruct for setup in code and tests It seems that annotation will be excluded from Java 11: Note that both @PostConstruct and @PreDestroy annotations are part of Java EE. And since Java EE…
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
9
votes
4 answers

Spring cache using @Cacheable during @PostConstruct does not work

related to the commit in spring framework https://github.com/spring-projects/spring-framework/commit/5aefcc802ef05abc51bbfbeb4a78b3032ff9eee3 the initialisation is set to a later stage from afterPropertiesSet() to afterSingletonsInstantiated() In…
TimothyBrake
  • 551
  • 6
  • 9
9
votes
1 answer

calling @PostConstruct on super bean AND extending bean

I have a BaseBean with a @PostConstruct, and a bean extending it on which i would like to call another @PostConstruct. I have read several places where it said it was possible, however, it seems the @postConstruct on the extending class is called…
Yotam Soen
  • 305
  • 1
  • 3
  • 6
8
votes
1 answer

@SessionScoped bean looses scope and gets recreated all the time, fields become null

I have a very strange problem with a SessionScoped bean in a JSF 2.0 project. Using Netbeans 6.9.1, Glassfish 3 server and PrimeFaces 3 as the JSF component library. Here is some code: package com.hia.jsf; import com.hia.netlabel.jpa.Genre; import…
Namphibian
  • 12,046
  • 7
  • 46
  • 76
8
votes
3 answers

Testing @Postconstruct with Mockito

Why when I injecting mocks via Mockito my @Postconstruckt method is not calling? @Service public class MyService { public MyService() { System.out.println("CONSTRUKTOR"); } @PostConstruct public void init() { …
Jan Testowy
  • 649
  • 3
  • 13
  • 32
8
votes
2 answers

@PostConstruct fails silently on a Grails Service

I thought the Spring annotations were supposed to work out of the box in a Grails environment, but I cannot get it to work at all. I also tried the afterProperties method, which did not work either. Can anyone spot a mistake? Is there some…
willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111
7
votes
1 answer

Conditional @PostConstruct annotation

I sometimes may not need to post-construct of a bean at startup according to values passed via JVM argument. I tried @Conditional annotation but it only works on @Bean annotations. Have you tried/needed such a thing before?
jit
  • 452
  • 1
  • 7
  • 26
7
votes
1 answer

All @Resource injection before any @PostConstruct again

JSR-250 says all @Resource annotated methods will be called before the @PostConstruct method.. My question is: Does that mean that all @Resource annotated methods on all beans in a context will be called before any @PostConstruct annotated methods…
Tim P
  • 948
  • 1
  • 12
  • 19
6
votes
2 answers

@PostConstruct didn't get called by JSF if ManagedBean is inside jar library

I'm running with the following problem. I have a few Managed Beans that are shared between, at this moment, two JSF applications. As I don't want to copy and paste the code in the two (more in the coming future) I've put this shared managed beans…
ebianchini
  • 73
  • 1
  • 1
  • 5
6
votes
3 answers

How to skip @PostConstruct when unit testing

I have a scheduled task that aggregates data every night. The task runs whenever I start up the application and I would like to stop it from running when I run jUnit tests on the applicaton. @Scheduled(cron = "0 0 0 1 * ?") public void…
gnommando
  • 63
  • 1
  • 1
  • 4
6
votes
3 answers

@Inject and @PostConstruct not working in singleton pattern

I have a class as below: public class UserAuthenticator { private static UserAuthenticator authenticator = @Inject private UserRepository userRepository; @PostConstruct public void init() { List allUsers =…
1
2
3
16 17