繁体   English   中英

从GlassFish 4.1中的自定义JNDI资源返回值

[英]Returning a value from a custom JNDI resource in GlassFish 4.1

我试图实现这么多方法,但这是对我最有意义的方式,我仍然无法从我的资源中返回任何内容。 我使用GlassFish管理GUI添加了资源(实质上,我试图在本地服务器上保存用户名和密码)。

虽然我得到一个空指针异常(NPE),请不要指向我这里,它根本没有帮助我。 什么是NullPointerException,我该如何解决?

这是我的支持班......

创建bean

   @LocalBean
   @Stateless

public class JndiProperties {

    @Resource(name="jndiCreds")
    private Properties properties;

    public String getUser() {
        return properties.getProperty("UserName");
    }
    public String getPass() {
        return properties.getProperty("UserPass");
    }
}

这是我的bean经理:

 @ManagedBean
 @ViewScoped
    public class GetCreds {
        @Inject
        private JndiProperties property;
        public String getUserName(){
            return property.getUser();
        }
        public String getPassword(){
            return property.getPass();
        }
    }

这就是我称之为的方式

GetCreds creds = new GetCreds();
String username = creds.getUserName();
String pass =  creds.getPassword();

我将资源命名为jndiCreds,并将名称UserName和UserPass与包含相应数据的值相对应。

以下是GlassFish GUI的视图:

在此输入图像描述

有任何想法为什么它不会返回我要求的信息? 当我从getCreds调用任一函数时,当我尝试调用资源时,我收到了NPE。

帮助将不胜感激; 我很困惑。

我决定放弃尝试使用bean并直接访问它(虽然我在这里放弃了一些安全性)。 我试图以上下文方式访问数据。 但! 我还是做不到! 这是我的新支持课程:

public class JndiProperties {

    public Properties getProperties(String jndiName) {
        Properties properties = null;
        try {
            InitialContext context = new InitialContext();
            properties = (Properties) context.lookup(jndiName);
            context.close();
        }
        catch (NamingException e) {

            return null;
        }
        return properties;
    }

这就是我获取信息的方式:

JndiProperties creds = new JndiProperties();

String username = creds.getProperties("jndiCreds").getProperty("UserName");
String pass =  creds.getProperties("jndiCreds").getProperty("UserPass");

String credentials = String.join(System.getProperty("line.separator"),
                                 "user=" + username,
                                 "password=" + pass);
System.out.print(credentials);

我使用上面显示的相同资源。 我仍然以空指针结束...非常感谢任何帮助。请随意回答我的bean实现的错误。

我已经弄清楚了!

发生了什么事情,首先,我是一个完全白痴,我试图用JUnit测试我的程序(它不会在服务器上运行!)

由于该程序未在GlassFish上运行,因此无法访问该资源。

其次,(最重要的是)我错过了appserver-rt.jar-非常重要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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