@Resource annotated member not entered - bad code or error?

I use the @Resource annotation in the class created in the ServletContextListener method contextInitialized(...), but the member is always zero. Here is my sample code.

LISTENER:

public void contextInitialized(ServletContextEvent sce) {
  System.err.println("In contextInitialised");
  new ResourceListenerTargetTest().executeMe(); 
}

ResourceListenerTargetTest:

@Resource(name="MyJDBCResource")
private DataSource source;
public void executeMe() {

  /*try {
    InitialContext ictx = new InitialContext();
    source = (DataSource)ictx.lookup("java:comp/env/MyJDBCResource");
  } catch (NamingException e) {
  e.printStackTrace();
  }*/

  System.err.println("source is " + source);
}

If I switch comments and run a manual resource search, it works fine.

If the @Resource annotation works like this when used in the context of the initialized method?

Appserver is WAS 7.0.0.5, if it works, I think this is a bug? Can anyone confirm?

+3
source share
2 answers

, , , , , , . , , ResourceListenerTargetTest, WAS , .

, , , . JVM WAS , , .

/ , , ( ).

+1

. .

Webcontainer . , , .

contextInitialized , , .

+2

Source: https://habr.com/ru/post/1743541/


All Articles