我正在尝试将课程设置为单例,并使其在以下更改完成
Beans.xml has this:
<bean id="LdapUti" class="com.amazon.bpmsawsproxy.util.LdapUtil" scope="singleton" />
LdapUtil class:
public class LdapUtil {
private static Log logger = LogFactory.getLog(LdapUtil.class);
public DirContext GetLdapDirContext() throws NamingException {
Hashtable<String, Object> env = new Hashtable<String, Object>(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "*********");
env.put(Context.SECURITY_CREDENTIALS, "******");
env.put(Context.SECURITY_PROTOCOL, "ssl");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
DirContext ctx = new InitialLdapContext(env, null);
return ctx;}}
这是为测试单例类而编写的单元测试用例
@Test
public void testSingleton(){
LdapUtil ctx1 = new LdapUtil();
LdapUtil ctx2 = new LdapUtil();
assertEquals(System.identityHashCode(ctx1), System.identityHashCode(ctx2));
}
从单元测试用例:我得到两个不同的哈希码,我相信它创建了多个实例。如果我错过了什么,请告诉我