Configure with Java Annotations

Last Updated on: January 13, 2021 pm

Java Annotations

Meta-data about the class.

Processed aannotations at compile time and run time.

Inversion of Control

Development Process

  1. Enable component canning in Spring config file

    Spring will scan the package recursively.

    1
    <context:component-scan base-package="com.luv2code.annotations"/>
  2. Add the @Component Annotation to your Java class.

    1
    2
    3
    @Component // use Default Bean Name
    public class TennisCoach {
    }
  3. Retrieve bean from Spring container.

    Same as before.

Dependency Injection

AutoWiring

Spring wil look for a class that matches the property.

Match by type: Class or Interface

Spring will inject it automatically.


Constructor Injection

Define a dependency interface and class

Define FortuneService interface and BadFortuneService Class.

1
2
3
4
5
6
7
@Component
public class BadFortuneService implements FortuneService {
@Override
public String getFortune() {
return "Today is your bad day!'";
}
}
Create a constructor for injections
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Component
public class TennisCoach implements Coach {
// constructor injection
private FortuneService fortuneService;
// Spring will scan for a component that implements the fortuneService interface.
@Autowired
public TennisCoach(FortuneService theFortuneService){
fortuneService = theFortuneService;
}
@Override
public String getDailyWorkout() {
return "Practice your back hand volley";
}

@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}
Configure with @Autowired annotation

Add @Autowired annotation to the constructor.


Setter Injection

  1. Create setter methods in your class for injections.

  2. Configure the dependency injection with Autowired Annotation.

1
2
3
4
5
@Autowired
public void setFortuneService(FortuneService theFortuneService){
System.out.println(">> TennisCoach : inside setFortuneService() method!");
fortuneService = theFortuneService;
}

Method Injection

1
2
3
4
5
@Autowired
public void doSomeCrazyStuff(FortuneService theFortuneService){
System.out.println(">> TennisCoach : inside doSomeCrazyStuff() method!");
fortuneService = theFortuneService;
}

Field Injection

  1. Configure the dependency injection with @Autowired , no need for setter method.
  2. Directly set the @Autowired annotation to the field. Spring will injection the FortuneService implementation directly to the class, using Java technology called Reflection.

Which injection should I use?

Choose a style and stay consistent in your project.

Qualifiers for Dependency Injection

  • What if there is more than one implemetations to choose when injecting?
  • Use the @Qualifier with the Default Name of the implementation.
  • For example, Class Name is HappyFortuneService, then the Default Name should be happyFortuneService.

You can use @Qualifier to all kinds of injections.

Bean Scope and Lifecycle

Scope

@Scope("Singleton")

@Scope("Prototype")

Lifecycle Methods

  1. Define your methods for initialization and destroy.

@PostConstruct

@PreDestroy

  1. Add Annotations to these methods.

Note: For “prototype” scoped beans, Spring does not call the @PreDestroy method. Gasp!

Reference: Udemy, Spring & Hibernate for Beginners (including SpringBoot)