我有点困惑。 javax.inject.Singleton
和 javax.ejb.Singleton
之间的确切区别是什么?
最佳答案
我找到了一个合理的解释 here :
By default,
javax.ejb.Singleton
session beans are transactional (section 13.3.7 of the EJB 3.1 specification) and require acquisition of an exclusive lock for every business method invocation (sections 4.8.5.4 and 4.8.5.5).In contrast, a
javax.inject.Singleton
is not transactional and does not support container-managed concurrency (the major consequence being that no locking scheme is implemented by the container). [...]If you don't need EJB features, stick with
@ApplicationScoped
(javax.inject.Singleton
is not defined by CDI, and its semantics are therefore not governed by that specification).
为了减少将来的困惑,我使用了这个小单元测试(需要替换第一级包名):
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import org.junit.Test;
public class SingletonTest {
/** requires com.tngtech.archunit:archunit-junit:0.4.0 */
@Test
public void detectWrongSingletonAnnotation() {
final ClassFileImporter importer = new ClassFileImporter();
final JavaClasses classes = importer.importPackages("first_level_package");
noClasses().should().beAnnotatedWith("javax.inject.Singleton")
.as("Please use javax.ejb.Singleton instead of javax.inject.Singleton.")
.check(classes);
}
}
关于java - javax.inject.Singleton 和 javax.ejb.Singleton 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36568344/