Skip to content

Instantly share code, notes, and snippets.

@pradeepsng30
Created March 15, 2020 06:51
Show Gist options
  • Save pradeepsng30/792ae746b301b994d8a2840ada4eead9 to your computer and use it in GitHub Desktop.
Save pradeepsng30/792ae746b301b994d8a2840ada4eead9 to your computer and use it in GitHub Desktop.

As programmers, we must configure Spring. Spring must know:

Where to start In this case, start with SpringApplication.run() Which classes the container must process In this case, use Annotations How to create instances of classes For Spring annotations, use @Component, @Service, @Bean, etc. For JSR330 annotations, use @Named Which variables to automatically assign For Spring annotations, use @Autowired For JSR330 annotations, use @Inject

@Named annotation tells Spring to process this file and to create an instance of the HelloService class used when we autowire the variables.

This instantiated class is known in Spring as a Bean from Java Bean. Normally a Java Bean is a class with:

Zero argument constructor All private fields Getters and setters for fields

Annotation Level Meaning @Named Type JSR330 - Used to define a Java bean for injection. Equivalent to @Component Spring Annotation @Component Type Spring Annotation - Used to define a Java type, annotation at the type level @Service Type Subclass of @Component. Used to designate a Service, like the service layer in JEE @Controller Type Subclass of @Component. Used to designate a Controller, like the controller layer in JEE @Bean Method Spring Annotation at the METHOD level. The method return type is the type of the generated bean @Scope Type, Method The resulting Java bean is either "singleton" or "prototype". Default is "singleton"

Annotation Level Meaning @Inject Field, Parameter JSR 330 - Instructs Spring to set a variable to an instance of a type. @Autowired Field, Parameter Instructs Spring to set a variable to an instance of a type @Qualifier @Bean, @Component Adds a String name to a type to distinguish multiple instances of a type @Qualifier Method, Parameter Uses the String name to @Autowired one of several instances of a type

Individual References But suppose our application wants two references to the two instances without using a list.

That's where the @Qualifier comes into play.

First associate a string with the @Bean instances as follows:

@Named public class MyFactory {

@Bean
@Qualifier("cat")
public AnimalFactory getAnimalFactory() {
    return new AnimalFactory("Cat");
}

@Bean
@Qualifier("dog")
public AnimalFactory getAnimalFactory2() {
    return new AnimalFactory("Dog");
}

} Then use the same @Qualifer values in the wiring:

@SpringBootApplication public class CoreApplication implements CommandLineRunner {

@Inject
@Qualifier("cat")
AnimalFactory animalFactory;

@Inject
@Qualifier("dog")
AnimalFactory animalFactory2;

When we run the application, we get:

AnimalFactory [hashcode = 1551446957; animalType=Cat; count = 2] AnimalFactory [hashcode = 1471948789; animalType=Dog; count = 2]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment