Thanks alot.
I had a look at the example and gave it a try with no luck so far. My
project is probably missing some dependencies (the exact dependencies are
not easy to figure out from the *pom.xml* files... I'm also not using a
full Java EE profile).
On Mon, Jul 11, 2016 at 6:24 PM, Pavel Bucek <pavel.bucek_at_oracle.com> wrote:
> Hi Francois,
>
> @Resource is not injected, since Jersey doesn't know that you want this
> bean to be managed by the container.
>
> You can try to mark your resource "MyResource" with @ManagedBean - then
> @Resource fields should be injected.
>
> See managed beans webapp example:
>
>
> https://github.com/jersey/jersey/tree/927b4d0605aeb119d15e9623dab65c8aec2c8e20/examples/managed-beans-webapp
>
> , concretely this class:
>
>
> https://github.com/jersey/jersey/blob/927b4d0605aeb119d15e9623dab65c8aec2c8e20/examples/managed-beans-webapp/src/main/java/org/glassfish/jersey/examples/managedbeans/resources/ManagedBeanSingletonResource.java
> Hope it helps,
> Pavel
>
>
> On 11/07/16 23:35, Francois Ross wrote:
>
> Hi,
>
> *How come @Resource annotations get picked up by servlets but not by
> Jersey resources?*
>
> I've been struggling to inject a DataSource via @Resource for a few hours
> with no success so far (I got lost along the way reading about jersey CDI,
> Weld, HK2, ...). I've set up a small Eclipse project on GitHub showing the
> behavior (see here
> <https://github.com/fross/jersey-resource-not-injected/tree/master/src/demo>
> ).
>
> I'm using:
>
> - Tomcat 7 (dynamic web module version 3.0).
> - a H2 data source configured in META-INF/context.xml
> - jersey-container-servlet 2.23.1
>
> Any help or explanation would be greatly appreciated (I'm a
> desktop/embedded software developer).
>
> import javax.annotation.Resource;import javax.sql.DataSource;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import javax.ws.rs.core.Response;
>
> @Path("/")public class MyResource {
>
> @Resource(name = "jdbc/myDatabase")
> private DataSource _dataSource;
>
> @GET
> @Produces(MediaType.TEXT_PLAIN)
> public Response doGet() {
> return Response
> .ok()
> .entity("jersey -> dataSource? " + (_dataSource != null)) *// jersey -> dataSource? false*
> .build();
> }
> }
>
> import java.io.IOException;
> import javax.annotation.Resource;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.sql.DataSource;
>
> @WebServlet(urlPatterns = { "/servlet" })public class MyServlet extends HttpServlet {
>
> @Resource(name = "jdbc/myDatabase")
> private DataSource _dataSource;
>
> @Override
> protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
> try {
> _dataSource.getConnection();
> } catch (Exception e) {
> e.printStackTrace();
> }
>
> resp.setContentType("text/plain");
> resp.getWriter().write("servlet -> dataSource? " + (_dataSource != null)); *// servlet -> dataSource? true*
> }
> }
>
>
>