[Spring Boot] @Component, @AutoWired and @Primary

Spring boot is really good for Dependencies injection by using Autowiring. Each class instancse in spring boot is called 'Bean', we can use 'Bean' to help us to simplfy the task.

Fro example we have main class:

@SpringBootApplication
public class In28minutesApplication {

    // What are the beans? --@Component & Bean
    // What are the dependencies of a bean? -- @AutoWired
    // Where to search for beans => NO NEED

    public static void main(String[] args) {
        // Application Context
        ApplicationContext applicationContext =
                SpringApplication.run(In28minutesApplication.class, args);
        //BinarySearchImpl binarySearch = new BinarySearchImpl(new QuickSortAlgo());
        BinarySearchImpl binarySearch = applicationContext.getBean(BinarySearchImpl.class);
        int result = binarySearch.binarySearch(new int[] {1,2,3,4}, 3);
        System.out.println(result);

    }

}

We can get Bean by using application context and class itself. Now we need to tell Spring boot where to find those Beans, that's by @Component:

@Component
public class BinarySearchImpl {

    @Autowired
    private SortAlgo sortAlgo;

    public int binarySearch(int [] numbers, int target) {
        // Sorting an array

        sortAlgo.sort(numbers);
        System.out.println(sortAlgo);
        // Quick sort

        // Return the result
        return 3;
    }

}

In BinarySearchImpl, we need to autowirte a dependency for 'SortAlgo'.

public interface SortAlgo {
    public int[] sort(int[] number);
}

There are two algotihms implements 'SortAlgo' interface:

@Component
@Primary
public class QuickSortAlgo implements SortAlgo{
    public int[] sort(int[] numbers) {
        return numbers;
    }
}
@Component
public class BubbleSortAlgo implements SortAlgo{
    public int[] sort(int[] numbers) {
        return numbers;
    }
}

Both are marked '@Component', this is important to tell Spring boot, those classes can be autowired. @Primary tell that when multi @Component have the same interface implemented, use @Primary one to autowired.

We can change the logging level to 'debug' in application.properties:

logging.level.org.springframework = debug

Therefore we can see the log:

2019-04-03 13:28:35.502 INFO 6720 --- [ main] c.e.in28minutes.In28minutesApplication : Starting In28minutesApplication on FINPWM10824441 with PID 6720 (C:Usersz000879learnin28minutesin28minutes argetclasses started by z000879 in C:Usersz000879learnin28minutes)
2019-04-03 13:28:35.503 INFO 6720 --- [ main] c.e.in28minutes.In28minutesApplication : No active profile set, falling back to default profiles: default
2019-04-03 13:28:35.503 DEBUG 6720 --- [ main] o.s.boot.SpringApplication : Loading source class com.example.in28minutes.In28minutesApplication
2019-04-03 13:28:35.573 DEBUG 6720 --- [ main] o.s.b.c.c.ConfigFileApplicationListener : Loaded config file 'file:/C:/Users/z000879/learn/in28minutes/in28minutes/target/classes/application.properties' (classpath:/application.properties)
2019-04-03 13:28:35.574 DEBUG 6720 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6f3b5d16
2019-04-03 13:28:35.590 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2019-04-03 13:28:35.608 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory'
2019-04-03 13:28:35.696 DEBUG 6720 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [C:Usersz000879learnin28minutesin28minutes argetclassescomexamplein28minutesBinarySearchImpl.class]
2019-04-03 13:28:35.697 DEBUG 6720 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [C:Usersz000879learnin28minutesin28minutes argetclassescomexamplein28minutesBubbleSortAlgo.class]
2019-04-03 13:28:35.700 DEBUG 6720 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [C:Usersz000879learnin28minutesin28minutes argetclassescomexamplein28minutesQuickSortAlgo.class]
2019-04-03 13:28:35.867 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.boot.autoconfigure.condition.BeanTypeRegistry'
2019-04-03 13:28:36.038 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'propertySourcesPlaceholderConfigurer'
2019-04-03 13:28:36.045 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
2019-04-03 13:28:36.047 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata'
2019-04-03 13:28:36.048 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
2019-04-03 13:28:36.051 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
2019-04-03 13:28:36.052 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
2019-04-03 13:28:36.056 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor'
2019-04-03 13:28:36.063 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'in28minutesApplication'
2019-04-03 13:28:36.071 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'binarySearchImpl'
2019-04-03 13:28:36.084 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'quickSortAlgo'
2019-04-03 13:28:36.086 DEBUG 6720 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Creating shared instance of singleton bean 'bubbleSortAlgo'

原文地址:https://www.cnblogs.com/Answer1215/p/10650812.html