2 annotation dependency injection

What is Spring AutoWiring?

  • For dependency injection, Spring can use auto wiring

  • Spring will look for a class that matches the property

    • matches by type: class or interface

  • Spring will inject it automatically ... hence it is autowired

7.1 Autowiring Injection Types

  • Constructor Injection

  • Setter Injection

  • Field Injections

Which type of Injection should I use?

Choose a style Stay consistent in your project!

7.2 Autowiring Example

  • Injecting FortuneService into a Coach implementation

  • Spring will scan @Components

  • Any one implements FortuneService interface???

  • If so, let's inject them. For example: HappyFortuneService

7.2.1 Constructor Injection--Development Process

  1. Define the dependency interface and class

  2. Create a constructor in your class for injections

  3. Configure the dependency injection with @Autowired Annotation

note:

What if there are multiple FortuneService implementations?

Answer:

Spring has special support to handle this case. Use the @Qualifier annotation.

7.2.2 Setter Injection with Annotations and Autowiring

Development Process-Setter Injection

  1. Create setter methods in your class for injections

  2. Configure the dependency injection with @Autowired Annotation

note: Inject dependencies by calling Any method on your class--Simply give:@Autowired

7.2.3 Field Injection with Annotations and Autowiring

Inject dependencies by setting field values on your class directly(even private fields), Accomplished by using Java Reflection

Development Process - Field Injection

  1. Configure the dependency injection with Autowired Annotation

  2. Applied directly to the field

  3. No need for setter methods

7.3 Annotation Autowiring and Qualifiers

If there are multiple FortuneService Implementations, may cause Exception: NoUniqueBeanDefinitionException.

And we need to tell Spring which bean to use...

Solution: Be specific! - @Qualifier

e.g.

@Autowired
@Qualifier("happyFortuneService")//The desired bean id: default bean id
private FortuneService theFortuneService;

Injection Types

  • Can apply @Qualifier annotation to

    • Constructor injection

    • Setter Injection methods

    • Field Injection

Note: Annotations - Default Bean Names ... and the special Case

In general, when using Annotations, for the default bean name, Spring uses the following rule.

If the annotation's value doesn't indicate a bean name, an appropriate name will be built based on the short name of the class (with the first letter lower-cased).

For example: HappyFortuneService --> happyFortuneService

However, for the special case of when BOTH the first and second characters of the class name are upper case, then thename is NOT converted.

For the case of RESTFortuneService

RESTFortuneService --> RESTFortuneService

_No conversion _since the first two characters are upper case.

7.4 How to inject properties file using Java annotations

Answer:

This solution will show you how inject values from a properties file using annotatons. The values will no longer be hard coded in the Java code.

1. Create a properties file to hold your properties. It will be a name value pair.

New text file: src/sport.properties

foo.email = myasd@gmail.com
foo.team = skljfa lsajkfd aslkjf

Note the location of the properties file is very important. It must be stored in src/sport.properties

2. Load the properties file in the XML config file.

File: applicationContext.xml

Add the following lines:

<context:property-placeholder location="classpath:sport.properties"/>

This should appear just after the <context:component-scan .../> line

3. Inject the properties values into your Swim Coach: SwimCoach.java

@Value("${foo.email}")
private String email;

@Value("${foo.team}")
private String team;

Last updated