2 yanqiaoli yanqiaoli 于 2016.04.13 10:14 提问

struts2 scope类语法问题 2C

package com.opensymphony.xwork2.inject;

import java.util.concurrent.Callable;

public enum Scope {

DEFAULT {
@Override
InternalFactory<? extends T> scopeFactory(Class type, String name,
InternalFactory<? extends T> factory) {
return factory;
}
},

SINGLETON {
@Override
InternalFactory<? extends T> scopeFactory(Class type, String name,
final InternalFactory<? extends T> factory) {
return new InternalFactory() {
T instance;
public T create(InternalContext context) {
synchronized (context.getContainer()) {
if (instance == null) {
instance = factory.create(context);
}
return instance;
}
}

    @Override
    public String toString() {
      return factory.toString();
    }
  };
}

},

THREAD {
@Override
InternalFactory<? extends T> scopeFactory(Class type, String name,
final InternalFactory<? extends T> factory) {
return new InternalFactory() {
final ThreadLocal threadLocal = new ThreadLocal();
public T create(final InternalContext context) {
T t = threadLocal.get();
if (t == null) {
t = factory.create(context);
threadLocal.set(t);
}
return t;
}

    @Override
    public String toString() {
      return factory.toString();
    }
  };
}

},

REQUEST {
@Override
InternalFactory<? extends T> scopeFactory(final Class type,
final String name, final InternalFactory<? extends T> factory) {
return new InternalFactory() {
public T create(InternalContext context) {
Strategy strategy = context.getScopeStrategy();
try {
return strategy.findInRequest(
type, name, toCallable(context, factory));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

    @Override
    public String toString() {
      return factory.toString();
    }
  };
}

},

SESSION {
@Override
InternalFactory<? extends T> scopeFactory(final Class type,
final String name, final InternalFactory<? extends T> factory) {
return new InternalFactory() {
public T create(InternalContext context) {
Strategy strategy = context.getScopeStrategy();
try {
return strategy.findInSession(
type, name, toCallable(context, factory));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

    @Override
    public String toString() {
      return factory.toString();
    }
  };
}

},

WIZARD {
@Override
InternalFactory<? extends T> scopeFactory(final Class type,
final String name, final InternalFactory<? extends T> factory) {
return new InternalFactory() {
public T create(InternalContext context) {
Strategy strategy = context.getScopeStrategy();
try {
return strategy.findInWizard(
type, name, toCallable(context, factory));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

    @Override
    public String toString() {
      return factory.toString();
    }
  };
}

};

Callable<? extends T> toCallable(final InternalContext context,
final InternalFactory<? extends T> factory) {
return new Callable() {
public T call() throws Exception {
return factory.create(context);
}
};
}

abstract InternalFactory<? extends T> scopeFactory(
Class type, String name, InternalFactory<? extends T> factory);

public interface Strategy {

<T> T findInRequest(Class<T> type, String name,
    Callable<? extends T> factory) throws Exception;


<T> T findInSession(Class<T> type, String name,
    Callable<? extends T> factory) throws Exception;


<T> T findInWizard(Class<T> type, String name,
    Callable<? extends T> factory) throws Exception;

}
}

以上java类中定义的DEFAULT、SINGLETON等后面直接加大括号的是什么语法?请大侠解释一下

1个回答

CSDNXIAOD
CSDNXIAOD   2016.04.13 10:22

Struts2 中文乱码问题
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片