EJB 嵌入式容器 - 依赖注入不起作用?
看一下下面的代码:
@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();
}
}
我希望在 start() 操作时有 simpletService 实例可用,但它等于 null。所有类都是一个项目的一部分(放置在单独的文件中)。我哪里做错了?谢谢你的建议。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后我找到了解决方案。当我将 start() 操作更改为公开时,注入开始工作正常。
Finally I have found solution. When I changed start() operation to be public and injection started work fine.