gpt4 book ai didi

java - 访问其他 Java 类中的托管 CacheBean

转载 作者:行者123 更新时间:2023-12-01 09:40:56 25 4
gpt4 key购买 nike

我编写了一个 Java 托管缓存 ​​bean。该 bean 的用途之一是允许我的应用程序的其他部分设置各种数据库。我的模板将被其他应用程序使用,因此我希望能够将数据数据库的路径(我的编程在一个应用程序中,我的数据在另一个应用程序中)设置在缓存Bean中的一个位置,然后能够使用它贯穿我的应用程序。

我的解决方案似乎有效,但我想知道这是否是解决问题的最佳方法。

我的面孔配置是:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
<managed-bean>
<managed-bean-name>CacheBean</managed-bean-name>
<managed-bean-class>com.scoular.cache.CacheBean</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
</faces-config>

我已将 CacheBean 和其他 Java 类配对,因此仅显示相关类:

缓存Bean:

package com.xxxx.cache;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Vector;
import javax.faces.context.FacesContext;

public class CacheBean implements Serializable {

// Constants
private static final String BEAN_NAME = "CacheBean";

private static final long serialVersionUID = -2665922853615670023L;

private String serverPath;
private String pcAppDBpathJava;

public static CacheBean getCurrentInstance() {

// This is a neat way to get a handle on the instance of this bean in the scope from other Java code...
FacesContext context = FacesContext.getCurrentInstance();
return (CacheBean) context.getApplication().getVariableResolver().resolveVariable(context, BEAN_NAME);
}

public CacheBean() throws NotesException, Exception {
loadDBPaths();
}

public void loadDBPaths() {
Session session = Factory.getSession();
Database tmpDB = session.getCurrentDatabase();
String tmpStr = tmpDB.getServer();
pcAppDBpathJava = tmpStr + "!!" + "scoApps\\PC\\PCApp.nsf";
}

public String getServerPath() {
Session session = Factory.getSession();
Database tmpDB = session.getCurrentDatabase();
serverPath = tmpDB.getServer();
return serverPath;
}

public void setServerPath(String serverPath) {
this.serverPath = serverPath;
}

public String getPcAppDBpathJava() {
return pcAppDBpathJava;
}

public void setPcAppDBpathJava(String pcAppDBpathJava) {
this.pcAppDBpathJava = pcAppDBpathJava;
}

}

另一个 Java 类是:

package com.xxxx.model;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import javax.faces.context.FacesContext;
import org.openntf.domino.*;
import org.openntf.domino.utils.Factory;
import org.openntf.domino.xsp.XspOpenLogUtil;
import com.scoular.cache.CacheBean;

public class PC implements Serializable {

private static final long serialVersionUID = -928898373594997220L;

@SuppressWarnings("unused")
private Database appData;

public PC() {
}

public void loadByUnid(String unid) {
try {

Database PCDataDB = this.getAppData();
Document doc = PCDataDB.getDocumentByUNID(unid);
if (null == doc) {
System.out.println("Document not found");
} else {
loadValues(doc);
}
} catch (Exception e) {
XspOpenLogUtil.logError(e);
}
}

public Database getAppData() {
CacheBean cache = CacheBean.getCurrentInstance();
Session session = Factory.getSession();
Database PCDataDB = session.getDatabase(cache.getPcDataDBpathJava());
return PCDataDB;
}

public void setAppData(Database appData) {
this.appData = appData;
}
}

如有任何建议,我们将不胜感激。

最佳答案

您可以从 JSF 引擎获取作为“变量”的 Bean。看看这里:https://www.mindoo.com/web/blog.nsf/dx/18.07.2009191738KLENAL.htm?opendocument&comments

只需调用辅助类来获取绑定(bind)值,您将获得对象引用或某些方法的返回值 - 基于您使用的 EL。

更新,错过了实际问题:

My solution seems to work, but I wonder if this is the best way to approach the problem.

是的,我认为这是最好的选择。

关于java - 访问其他 Java 类中的托管 CacheBean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38460559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com