繁体   English   中英

如何实现单例-Weblogic中

[英]how to implement a singleton - in weblogic

我尝试实现单例类方法,该方法由过滤器类调用。 过滤器类和其余类作为共享库添加到WLS。

我有两个运行的单独Web应用程序-带过滤器的servlet-都在同一台受管服务器上。

因此,除了单例被实例化两次之外,其他所有东西都工作正常。 请在下面的代码片段中找到。

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
   {
   }

}

过滤方式:

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());
   .......

  }
 }
}

在日志中有两个不同的参考-说

Test @ f1a2e06 Test @ f180f10

我在这里做错什么了吗? 任何帮助都会很棒。

Servlet容器针对不同的应用程序使用不同的类加载器。 因此,我认为无法共享一个实例。 您可能要做的就是通过JNDI注册实例。

暂无
暂无

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

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