找到你要的答案

Q:Static Values are not visible in separate jars

Q:静态值不可见在单独的罐子

In my web application- ear, A value of static varialble located in a jar called "A" is changed in ear . That newly changed value is visible and retains as it is. But whenever that variablel is accessed in another jar called "B", the updated value is not available, and first initialized value is only available. I use JBoss and jars are in tomcat-sar folder. For example:

// Default Ajar's static variable status;

 Ajar.Aclass.staticVariableA = "initializedValue";  

inside my utility classes in ear, Ajar.Aclass.staticVariableA = "NewValue";  

// Then, when Bjar which is located in war also now access the Ajar's static variable,

 System.out.println("value in B jar : " + Ajar.Aclass.staticVariableA );

// This prints value in B jar : initializedValue

As I feel, the value of A.jar is valid in ear context as it is initialized inside the ear. B.jar has its own context and whenever variables in A.jar is called, those variables'initial value is returned.

Is there a way to solve this ?

In my web application- ear, A value of static varialble located in a jar called "A" is changed in ear . That newly changed value is visible and retains as it is. But whenever that variablel is accessed in another jar called "B", the updated value is not available, and first initialized value is only available. I use JBoss and jars are in tomcat-sar folder. For example:

/ /默认Ajar的静态变量状态;

 Ajar.Aclass.staticVariableA = "initializedValue";  

inside my utility classes in ear, Ajar.Aclass.staticVariableA = "NewValue";  

/ /然后,当Bjar位于战争现在访问微开的静态变量,

 System.out.println("value in B jar : " + Ajar.Aclass.staticVariableA );

//打印值B瓶:initializedvalue

我觉得,对a.jar值是有效穗上下文初始化时耳内。有自己的背景和b.jar每当变量在A.jar被称为,那些variables'initial返回值。

有没有办法解决这个问题?

answer1: 回答1:

You can use cross-context servletContext attributes as straight forward approach

But to answer your question, because you already have progressed using static variables

Warning: as per JAVA's offical documentation, using reflection should be carefully done, such a way that you do not manipulate inaccessible objects

Put the following crossContext="true" inside TOMCAT_HOME\conf\context.xml

<Context crossContext="true">

And you have to load the Class via ServletContext and ClassLoader Methodology

ServletContext context = request.getSession().getServletContext();
ServletContext othercontext = context.getContext("/otherwarrooturl");

Now you have you context and the other context, let us assume the target class is com.debora.MyClass

We are going to load the class using the other context

try {
     Class myclass = othercontext.loadClass("com.debora.MyClass");
     Field[] fields =  myclass.getDeclaredFields();
     //....your reflection program to fetch the static value...
     //....or using method invoke...
}catch (Exception e) {
    e.printStackTrace();
} finally {
    //your cleanup code
}

你可以使用ServletContext属性跨语境直截了当的方法

但要回答你的问题,因为你已经取得进展,使用静态变量

警告:根据java的官方文档,使用反射应仔细做,这样你不操控的不可访问对象

把下面的crosscontext =“true”里面tomcat_home \中\ context.xml中

<Context crossContext="true">

你要加载的类和类加载器方法通过ServletContext

ServletContext context = request.getSession().getServletContext();
ServletContext othercontext = context.getContext("/otherwarrooturl");

现在你有你的背景和其他背景下,让我们假设目标类是com.debora.myclass

我们将使用其他上下文加载类

try {
     Class myclass = othercontext.loadClass("com.debora.MyClass");
     Field[] fields =  myclass.getDeclaredFields();
     //....your reflection program to fetch the static value...
     //....or using method invoke...
}catch (Exception e) {
    e.printStackTrace();
} finally {
    //your cleanup code
}
java  java-ee  jboss