Standard annotations
All the annotations as part of Java specification Request 330 are called as standard annotations. In Chapter 1 I provide a basic overview of JSR 330 and the various annotations which comes as part of this specification. I had also given a comparison between annotations by Spring and corresponding equivalent standard annotations as part of JSR-330. In this section I will be giving you details on the various Spring annotations and JSR-330 annotations in some details that you are more clearer on the various annotations that we can use in your code for bootstrapping you Spring Framework.
Spring @Autowired and JSR-330 @Inject
For referring to other bean for performing a specific bean function, annotations such as @Autowired and @Inject are used. Both @Autowired and @Inject can be applied to member variable, setter method, constructor and generic method.
@Autowired is dependent on Spring Framework and is useful when detailed Dependency Injection is required. In the following section I will give you the usage of @Autowired annotation according to its applicable location in a java class.
Member variable and constructor
Listing. Usage of @Autowired annotation in method variable and constructor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@Service public class LoyaltyServiceImpl implements LoyaltyService { @Autowired CustomerRepository customerRepository; MessageSource messageSource; @Autowired public LoyaltyServiceImpl(MessageSource messageSource) { this.messageSource = messageSource; } } |
Setter Method
Listing. Usage of @Autowired annotation in setter method
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Service public class LoyaltyServiceImpl implements LoyaltyService { CustomerRepository customerRepository; @Autowired public void setCustomerRepository(CustomerRepository customerRepository) { this.customerRepository = customerRepository; } |
Generic method
Listing. Usage of @Autowired annotation in generic method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@Service public class LoyaltyServiceImpl implements LoyaltyService { CustomerRepository customerRepository; @Autowired public void createCustomer(CustomerRepository customerRepository){ //..do repository stuff here } } |
@Inject as I can put in easy terms is standard annotation doing the stuff similar to @Autowired which is Spring dependent. @Autowired has something more than @Inject. An example of usage of @Inject is given below:
Listing. Usage of @Inject annotation
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Service("loyaltyService") public class LoyaltyServiceImpl implements LoyaltyService { @Inject @Named("customerRepository") CustomerRepository customerRepository; //….. } |
Spring @Component and JSR-330 @Named
@Component, a parent stereotype annotation, can be used to define all beans. However, Spring Framework divides component by layer and recommends using stereotype annotations to define beans. Section namely “Stereotype annotations” gives more detailed explanation on this.
@Components annotation’s equivalent JSR-330 annotation is @Named for component identification. For a specific bean class, JSR-330 grants @Named and helps the relevant bean managed by the container similar to the case granting Stereotype Annotation. For the bean granted with @Named, ‘Singleton’ will be applied as a default scope.
Spring and JSR-330 @Scope
Spring Framework provides five scopes based on bean instance creation mechanism, and to define such bean scope, use @Scope annotation. Both Spring and JSR-330 have named this annotation as @Scope. The various scopes which can be used is already detailed in chapter 3 in section “Bean Scoping”.
Listing. Declaration of @Component and @Named in the Java Class
1 2 3 4 5 6 7 8 9 |
@Scope("prototype") public class LoyaltyServiceImpl { @Autowired CustomerRepository customerRepository; } |
Page Visitors: 4349

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Hi there,
There is one mistake repeated several times here. The value of the propertyInteger is being set using “ref” instead of “value” and you may want to remove the spaces before and after some of the values/properties to avoid others new to the framework copying and pasting and getting tripped up.
Apart from that, thanks for making the material available for free.
Sure will do the necessary changes. I wrote the material using MS Word and after that when i copied, these spaces automatically came in.
Thanks for pointing it out.
Regards
Tomcy John