为什么我在initialcontext中找不到我的bean?

vsikbqxv  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(310)

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

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

authservice.java文件

package socnet.services;

...

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

注册测试.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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题