本文最后更新于:June 14, 2022 pm
                
              
            
            
              Spring 是目前主流的 Java Web 开发框架,是 Java 世界最为成功的框架。Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。该框架是一个轻量级的开源框架,具有很高的凝聚力和吸引力。Spring 框架不局限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何 Java 应用都可以从 Spring 中受益。Spring 框架还是一个超级粘合平台,除了自己提供功能外,还提供粘合其他技术和框架的能力。
目录 
个人理解:用来设置哪一个Bean是主Bean,那么在注入的时候,如果有多个相同的类型,则会选择使用主Bean进行注入,而不是默认的再根据名称进行注入。在一个类型拥有了主Bean之后,无论有多少个相同的类型,任意的名称都可以完成注入。因为有了主Bean之后,就不会再通过名称进行注入了,而是通过主Bean进行注入。主要有主Bean,名称就可以任意。
 
默认示例 目标类:
package  com.SpringTestAnnotation.TestValue;import  org.springframework.stereotype.Component;@Component public  class  OrderS   { }
 
默认使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package  com.SpringTestAnnotation.TestValue;import  org.springframework.beans.factory.annotation.Autowired;import  org.springframework.stereotype.Component;@Component public  class   Per   {     @Autowired      private  OrderS orderS;     public  void  show ()  {         System.out.println("Per show " +orderS);     } }
 
配置类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package  com.SpringTestAnnotation;import  com.SpringTestAnnotation.TestValue.OrderS;import  org.springframework.context.annotation.Bean;import  org.springframework.context.annotation.ComponentScan;@ComponentScan(value = "com.SpringTestAnnotation") public  class  TestConfig   {     @Bean      public  OrderS orderS123 ()  {         System.out.println("TestConfig orderS123" );         return  new  OrderS();     } }
 
测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package  com.SpringTestAnnotation;import  com.SpringTestAnnotation.TestValue.Per;import  org.springframework.context.annotation.AnnotationConfigApplicationContext;public  class  TestAnnotation   {     public  static  void  main (String[] args)   {         AnnotationConfigApplicationContext applicationContext = new  AnnotationConfigApplicationContext(TestConfig.class);         Per per = applicationContext.getBean("per" , Per.class);         per.show();     } }
 
输出:
TestConfig orderS123  Per show com.SpringTestAnnotation.TestValue.OrderS@7ba18f1b
 
设置主Bean示例 在上面的代码中,容器中只要两个OrderS的Bean:orderS、orderS123。现在通过设置主Bean,则无需再关心Bean的名称,因为只会同主Bean进行注入。
基本代码同上,只贴改变部分代码:
使用:(注意名称的区别)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package  com.SpringTestAnnotation.TestValue;import  org.springframework.beans.factory.annotation.Autowired;import  org.springframework.stereotype.Component;@Component public  class   Per   {     @Autowired      private  OrderS orderS777;      public  void  show ()  {         System.out.println("Per show " +orderS777);     } }
 
容器中没有Bean名称为orderS777的。
配置类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package  com.SpringTestAnnotation;import  com.SpringTestAnnotation.TestValue.OrderS;import  org.springframework.context.annotation.Bean;import  org.springframework.context.annotation.ComponentScan;import  org.springframework.context.annotation.Primary;@ComponentScan(value = "com.SpringTestAnnotation") public  class  TestConfig   {     @Bean      @Primary      public  OrderS orderS123 ()  {         System.out.println("TestConfig orderS123" );         return  new  OrderS();     } }
 
测试结果:
TestConfig orderS123 Per show com.SpringTestAnnotation.TestValue.OrderS@4b53f538
 
从示例可以看出,容器中没有名称为orderS777的Bean,但是依然注入成功。原因就在于通过注解@Primary设置了主Bean,那么在注入类型OrderS时,如果发现有多个相同的类型则会使用主Bean进行注入。
📢注意:一个类型下只能有一个主Bean!!!