springspring-annotationsjsr330

What is the default bean scope if we use @Inject with @Component?


I know the default bean scope is singleton when we use @Autowired with @Component.

But what if we use JSR-330's @Inject with spring's @Component (without using @Scope or @Singleton)?


Solution

  • There's no difference between @Inject or @Autowired

    two annotations works the same way as Spring has decided to support some JSR-299 annotations in addition to their own

    Note JSR-299 is built on top of JSR-330

    JSR-299 (Java Contexts and Dependency Injection), with Gavin King as lead, uses JSR-330 as base and enhances it significantly with modularization, cross cutting aspects (decorators, interceptors), custom scopes, or type safe injection capabilities. JSR-299 is layered on top of JSR-330

    All spring beans, as @Component, are by default singletons

    singleton bean is quite different from the Singleton pattern as defined in the seminal Gang of Four (GoF) patterns book. The GoF Singleton hard codes the scope of an object such that one and only one instance of a particular class will ever be created per ClassLoader. The scope of the Spring singleton is best described as per container and per bean. This means that if you define one bean for a particular class in a single Spring container, then the Spring container will create one and only one instance of the class defined by that bean definition. The singleton scope is the default scope in Spring