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
101
votes
2 answers

When to use f:viewAction / preRenderView versus PostConstruct?

When should one use the f:viewAction or preRenderView event to initialize data for a page versus using the @PostConstruct annotation? Is the rationale to use one or the other based on the type of scope of the backing bean e.g. If the backing bean is…
BestPractices
  • 12,738
  • 29
  • 96
  • 140
71
votes
8 answers

Guice call init method after instantinating an object

Is it possible to tell Guice to call some method (i.e. init()) after instantinating an object of given type? I look for functionality similar to @PostConstruct annotation in EJB 3 (and Spring).
mgamer
  • 13,580
  • 25
  • 87
  • 145
41
votes
3 answers

@PostConstruct & Checked exceptions

In the @PostConstruct doc it says about the annotated methods: "The method MUST NOT throw a checked exception." How would one deal with e.g. an IOException which can be thrown in such a method? Just wrap it in a RuntimeException and let the user…
fasseg
  • 17,504
  • 8
  • 62
  • 73
32
votes
5 answers

Why does @PostConstruct callback fire every time even though bean is @ViewScoped? JSF

I am using datatable on page and using binding attribute to bind it to my backing bean. This is my code :-
TCM
  • 16,780
  • 43
  • 156
  • 254
31
votes
1 answer

@ViewScoped calls @PostConstruct on every postback request

This doesn't seem right. I was doing some cleanup of my code and I just noticed this. Every ajax request is firing the constructor and @PostConstruct of my @ViewScoped bean. Even a simple database pagination is firing it. I understood that…
Drew H
  • 4,699
  • 9
  • 57
  • 90
28
votes
6 answers

Multiple PostConstruct methods?

It says in Java's documentation page for PostConstruct that Only one method can be annotated with this annotation But I just tried annotating three methods of a standalone application with PostConstruct. No compile errors, and all three of them…
Luis Sep
  • 2,384
  • 5
  • 27
  • 33
25
votes
4 answers

How to test constructor of a class that has a @PostConstruct method using Spring?

If I have a class with a @PostConstruct method, how can I test its constructor and thus its @PostConstruct method using JUnit and Spring? I can't simply use new ClassName(param, param) because then it's not using Spring -- the @PostConstruct method…
AHungerArtist
  • 9,332
  • 17
  • 73
  • 109
23
votes
3 answers

How can I add FacesMessage during page load? Using @PostConstruct does not seem to work

In a backing bean's @PostConstruct method, I make a call to an EJB which might return some messages that I want to display on the page via p:messages. However, even if I add the FacesMessages e.g. FacesContext.getCurrentInstance().addMessage(...),…
BestPractices
  • 12,738
  • 29
  • 96
  • 140
22
votes
5 answers

Why @PostConstruct method is not called when autowiring prototype bean with constructor argument

I have a prototype-scope bean, which I want to be injected by @Autowired annotation. In this bean, there is also @PostConstruct method which is not called by Spring and I don't understand why. My bean definition: package somepackage; import…
gerard
  • 221
  • 1
  • 2
  • 5
19
votes
2 answers

@PostConstruct method called twice for the same request

I'm using JSF 2.0 with GlassFish 3.0. I have the following Managed Bean: @ManagedBean @RequestScoped public class OverviewController{ private List eventList; @PostConstruct public void init(){ System.out.println("=>…
Ionut
  • 2,788
  • 6
  • 29
  • 46
17
votes
2 answers

Unit testing: Call @PostConstruct after defining mocked behaviour

I have two classes: public MyService { @Autowired private MyDao myDao; private List list; @PostConstruct private void init(){ list = myDao.getItems(); } } Now I'm wanting to involve MyService in a…
dwjohnston
  • 11,163
  • 32
  • 99
  • 194
15
votes
3 answers

Testing spring bean with post construct

I have a bean similar to this: @Service public class A { @Autowired private B b; @PostConstruct public void setup() { b.call(param); } } @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes =…
Andy
  • 953
  • 1
  • 8
  • 18
15
votes
2 answers

Can I use @PostConstruct on an interface method?

I have a number of beans implementing an interface and I'd like them all to have the same @PostConstruct. I've added the @PostConstruct annotation to my interface method, then added to my bean definitions:
Abby
  • 3,169
  • 1
  • 21
  • 40
15
votes
4 answers

How to disable @PostConstruct in Spring during Test

Within a Spring Component I have a @PostConstruct statement. Similar to below: @Singleton @Component("filelist") public class FileListService extends BaseService { private List listOfFiles = new Arrays.list(); //some other functions …
Ben
  • 1,086
  • 3
  • 15
  • 30
13
votes
1 answer

Mockito and CDI bean injection, does @InjectMocks call @PostConstruct?

I have this code: class Patient { @Inject Syringe syringe; @PostConstruct void sayThankyouDoc() { System.out.println("That hurt like crazy!"); } } @RunWith(MockitoJUnitRunner.class) class TestCase { @Mock Syringe…
gurghet
  • 7,591
  • 4
  • 36
  • 63
1
2 3
16 17