About Question enthuware.jwpv6.2.670 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
paf
Posts: 2
Joined: Thu Feb 16, 2012 7:24 am
Contact:

About Question enthuware.jwpv6.2.670 :

Post by paf »

Consider the following servlet code:

@WebServlet("/wftestservlet1")
public class WFTestServlet1 extends HttpServlet{

@Resource
private DataSource myDS;

//Other valid methods
...
}

Which of the following methods can access the DataSource myDS injected into the servlet by the container?
1. A method annotated with @Init
2. A method annotated with @PostConstruct
3. A method annotated with @Resource
4. The servlet's init() method.
5. In the servlet's constructor.

Explanation:
Note that resources are be injected prior to any lifecycle methods being called and the component instance being made available the application. This means, the injected resource will be available in the init() method and the @PostConstruct method (along with other methods such as service() or doXXX(), or even @PreDestroy method).

It cannot be available in the constructor because before the container can access the field to set the reference, the object needs to be constructed.
If this statement is true:
This means, the injected resource will be available in the init() method and the @PostConstruct method (along with other methods such as service() or doXXX(), or even @PreDestroy method).

then "A method annotated with @Resource" also can have access to the myDS, cannot it?

admin
Site Admin
Posts: 8858
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.670 :

Post by admin »

Yes, it should be valid.
This has now been fixed. Thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests