[英]how to implement a singleton - in weblogic
I tried implementing a singleton class method , which is invoked by a filter class. 我尝试实现单例类方法,该方法由过滤器类调用。 The filter class and the rest of the classes , are added to WLS as a shared library.
过滤器类和其余类作为共享库添加到WLS。
And i have two separate web apps running - servlets with filter - both on the same managed server. 我有两个运行的单独Web应用程序-带过滤器的servlet-都在同一台受管服务器上。
So everything is working fine except , the singleton is getting instantiated twice . 因此,除了单例被实例化两次之外,其他所有东西都工作正常。 plz find below the code snippet.
请在下面的代码片段中找到。
public class Test
{
private static Test ref ;
private DataSource X;
static int Y;
long Z ;
private Test ()
{
// Singleton
Z= 100 ;
}
public static synchronized Test getinstance() throws NamingException, SQLException
{
if(ref == null)
{
ref = new Test() ;
InitialContext ic = new InitialContext();
ref.X = (DataSource)ic.lookup ("jdbc/Views");
}
return ref ;
}
public Object clone()throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
}
public int sampleMethod (int X) throws SQLException
{
}
}
Filter method: 过滤方式:
public final class Filter implements Filter
{
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException
{
try
{
Test ref = Test.getinstance();
log.logNow(ref.toString());
.......
}
}
}
In the log am getting two different references - say 在日志中有两个不同的参考-说
Test@f1a2e06 Test@f180f10 Test @ f1a2e06 Test @ f180f10
Am i doing something wrong here ? 我在这里做错什么了吗? Any help would be great.
任何帮助都会很棒。
A servlet container uses different classloaders for different application. Servlet容器针对不同的应用程序使用不同的类加载器。 So I don't believe it is possible to share an instance.
因此,我认为无法共享一个实例。 What you can do is perhaps register the instance via JNDI.
您可能要做的就是通过JNDI注册实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.