Spring Book – Chapter 4 – Configuration Styles

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

Setter Method

Listing. Usage of @Autowired annotation in setter method

Generic method

Listing. Usage of @Autowired annotation in generic method

@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

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

Page Visitors: 3714

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 4 – Configuration Styles

  1. 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.

    1. 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

Leave a Reply

Your email address will not be published. Required fields are marked *