简体   繁体   中英

EJB embedded container - dependency injection don't work?

Have a look at following code:

@Local
public interface MyService {

    void printMessage();
}

@Stateless
public class MyServiceImpl implements MyService {

    @Override
    public void printMessage() {
        System.out.println("Hello from MyService.");
    }
}

@Stateless
@Named
public class Application {

    @EJB
    public MyService sampleService;

    private static Application getApplication() throws NamingException {
        Properties properties = new Properties();
        properties.setProperty(EJBContainer.APP_NAME, "admin");
        EJBContainer.createEJBContainer(properties); //.getContext();
        Context context = new InitialContext();

        Application application = (Application) context.lookup("java:global/admin/classes/Application");
        return application;
    }

    public static void main(String[] args) throws NamingException {
        Application application = getApplication();
        application.start(args);
    }

    private void start(String[] args) {
        sampleService.printMessage();
    }
}

I expected to have simpletService instance available at start() operation, but it is equal to null. All classes are part of one project (placed in separated files). Where I have made mistake? Thanks for advice.

Finally I have found solution. When I changed start() operation to be public and injection started work fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM