为什么在初始上下文中找不到我的bean?

问题描述

我想测试我的javaee项目。 这是结构简化

-src
  - main
    - java
      - socnet
        - services 
          - AuthService.java
  - test
    - java
      - AuthTest.java

AuthService.java

package socnet.services;

...

@Stateless
@LocalBean
public class AuthService {
  ...
}

RegistrationTest.java

public class RegistrationTest {

    private AuthService aBean;

    @Before
    public void init() throws NamingException {
        Hashtable<String,String> hashtable = new Hashtable();
        hashtable.put("java.naming.factory.initial","org.apache.openejb.client.LocalInitialContextFactory");
        hashtable.put("log4j.category.OpenEJB.startup","debug");
        hashtable.put("log4j.category.OpenEJB.options","debug");
        hashtable.put("log4j.category.OpenEJB.startup.config","debug");

        hashtable.put("openejb.deployments.classpath.exclude","");
        hashtable.put("openejb.deployments.classpath.include",".*");


        hashtable.put("myDS","new://Resource?type=DataSource");
        hashtable.put("myDS.JdbcDriver","org.postgresql.Driver");
        hashtable.put("myDS.JdbcUrl","jdbc:postgresql://localhost:5432/testdb-test");
        hashtable.put("myDS.JtaManaged","true");
        hashtable.put("myDS.DefaultAutoCommit","true");
        hashtable.put("myDS.UserName","admin");
        hashtable.put("myDS.Password","1234");

        InitialContext ctx = new InitialContext(hashtable);
        aBean = (AuthService) ctx.lookup("java:AuthService");
    }

    @Test
    public void saveTest() {
        ...
    }
}

开始测试时出现错误javax.naming.NameNotFoundException: Name "AuthService" not found.

我尝试使用@Stateles(nameMapping =“ AuthService”),但无法正常工作。 我也尝试过这段代码来查看上下文中的内容

NamingEnumeration<Binding> list = initialContext.listBindings("java:openejb/");
while (list.hasMore()) {
    Binding item = list.next();
    System.out.println(item.getClassName() +" :: " + "java:openejb/" + item.getName());
}

结果似乎没有纳米豆

org.apache.openejb.core.ivm.naming.IvmContext :: java:openejb/local
org.apache.openejb.core.ivm.naming.IvmContext :: java:openejb/remote
org.apache.openejb.core.ivm.naming.IvmContext :: java:openejb/client
org.apache.openejb.core.security.SecurityServiceImpl :: java:openejb/SecurityService
org.apache.openejb.core.ivm.naming.IvmContext :: java:openejb/global
org.apache.openejb.core.ivm.naming.IvmContext :: java:openejb/Deployment
org.apache.openejb.core.ivm.naming.IvmContext :: java:openejb/Resource
org.apache.geronimo.transaction.manager.GeronimoTransactionManager :: java:openejb/TransactionManager

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...