Tom*_*Tom 0 java singleton servlets weblogic-10.x servlet-filters
我尝试实现一个单例类方法,该方法由过滤器类调用.过滤器类和其余类将作为共享库添加到WLS.
我有两个独立的Web应用程序 - 带有过滤器的servlet - 都在同一个托管服务器上.
所以一切都工作正常,除了,单例实例化两次.plz在代码片段下方找到.
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
{
}
}
Run Code Online (Sandbox Code Playgroud)
过滤方式:
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());
.......
}
}
}
Run Code Online (Sandbox Code Playgroud)
在日志中得到两个不同的引用 - 比如说
测试@ f1a2e06测试@ f180f10
我在这里做错了吗?任何帮助都会很棒.
| 归档时间: |
|
| 查看次数: |
1388 次 |
| 最近记录: |