• Spring注入bean的常用的六种方式


    一.通过注解注入的一般形式

    Bean类

    public class TestBean{
    }
    
    • 1
    • 2

    Configuration类
    @Configuration注解去标记了该类,这样标明该类是一个Spring的一个配置类,在加载配置的时候会去加载他。

    @Bean的注解,标明这是一个注入Bean的方法,会将下面的返回的Bean注入IOC

    //创建一个class配置文件
    @Configuration
    public class TestConfiguration{
     //将一个Bean交由Spring进行管理
        @Bean
        public TestBean myBean(){
            return new TestBean();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    测试类

    ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
    TestBean testBean = cotext.getBean("testBean",TestBean.class);
    System.out.println("testBean = " + testBean);
    
    • 1
    • 2
    • 3

    二.通过构造方法注入Bean

    我们在生成一个Bean实例的时候,可以使用Bean的构造方法将Bean实现注入
    Bean类

    @Component
    public class TestBeanConstructor {
    
     private AnotherBean anotherBeanConstructor;
    
     @Autowired
     public TeanBeanConstructor(AnotherBean anotherBeanConstructor){
         this.anotherBeanConstructor = anotherBeanConstructor;
     }
    
     @Override
     public String toString() {
         return "TeanBean{" +
             "anotherBeanConstructor=" + anotherBeanConstructor +
             '}';
     }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    AnotherBean类

    @Component(value="Bean的id,默认为类名小驼峰")
    public class AnotherBean {
    }
    
    • 1
    • 2
    • 3

    Configuration类

    @Configuration
    @ComponentScan("com.company.annotationbean")
    public class TestConfiguration{
    }
    
    • 1
    • 2
    • 3
    • 4

    注解解释

    @AutoWired
    自动装配??
    若是在这里注入的时候指定一个Bean的id就要使用@Qualifier注解。
    注释可以在 setter 方法中被用于自动连接 bean,就像 @Autowired 注释,容器,一个属性或者任意命名的可能带有多个参数的方法。
    还有不解的地方可以直接百度注解有更详细的解释跟实例

    @Component(默认单例模式)
    @Component 被称为元注释,它是@Repository、@Service、@Controller、@Configuration的父类,理论上可以使用@Component来注释任何需要Spring自动装配的类。但每个注释都有他们自己的作用,用来区分类的作用,Spring文档上也说明后续版本中可能为@Repository、@Service、@Controller、@Configuration这些注释添加其他功能,所以建议大家还是少使用@Component。
      @Repository注解是任何满足存储库角色或构造型(也称为数据访问对象或DAO)的类的标记。该标记的用途之一是异常的自动翻译,如异常翻译中所述。
      @Service注解一般使用在Service层。
      @Controller注解一般使用在Controller层的类,@RestController继承了@Controller。
      @Configuration注解一般用来配置类,用来项目启动时加载的类。
      
    @ComponentScan(“”)
    @ComponentScan注解一般用在Spring项目的核心配置类,或者在Spring Boot项目的启动类里使用。作用就是用来扫描@Component及其子类注释的类,用于Spring容器自动装配。@ComponentScan默认是扫描的路径是同级路径及同级路径的子目录,所以把Spring Boot的启动类放在根目录下,@ComponentScan是可以省略不写的。
    主要属性:

    1. value属性、basePackages属性

      @AliasFor(“basePackages”)
      String[] value() default {};

      @AliasFor(“value”)
      String[] basePackages() default {};

    这两个值的作用是一样的,是相互别名的关系。内容是填写要扫描的路径,如果是有一个路径,可以不用写value,有几种写法如下:

    @ComponentScan("com.zh.service")
    
    @ComponentScan(value = "com.zh.service")
    
    @ComponentScan(value = {"com.zh.dao", "com.zh.service"})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. includeFilters属性、excludeFilters属性
      这两个的作用都是过滤器,excludeFilters的作用是剔除values属性内的个别不需要加载的类,而includeFilters一般是和excludeFilters配合使用,就是被excludeFilters剔除的类里面,还需要其中的几个类,就用includeFilters再加上。

    举个例子,假设送了excludeFilters剔除了所有注解是Repository的类,但其中一个Stub开头的类,还要用到,就可以按下面的例子这样写。

    ComponentScan.Filter[] includeFilters() default {};
    
        ComponentScan.Filter[] excludeFilters() default {};
    
    • 1
    • 2
    • 3

    Filter作为过滤器的基本对象

    @Retention(RetentionPolicy.RUNTIME)
    @Target({})
    public @interface Filter {
        FilterType type() default FilterType.ANNOTATION;
    
        @AliasFor("classes")
        Class[] value() default {};
    
        @AliasFor("value")
        Class[] classes() default {};
    
        String[] pattern() default {};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    FilterType是过滤器的类型,定义方式有几种

    public enum FilterType {
        ANNOTATION,
        ASSIGNABLE_TYPE,
        ASPECTJ,
        REGEX,
        CUSTOM;
    
        private FilterType() {
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    三.通过set方法注入Bean

    我们可以在一个属性的set方法中去将Bean实现注入
    Bean类

    @Component
    public class TestBeanSet {
    
     private AnotherBean anotherBeanSet;
    
     @Autowired
     public void setAnotherBeanSet(AnotherBean anotherBeanSet) {
         this.anotherBeanSet = anotherBeanSet;
     }
    
     @Override
     public String toString() {
         return "TestBeanSet{" +
             "anotherBeanSet=" + anotherBeanSet +
             '}';
     }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    Configuration类

    @Configuration
    @ComponentScan("com.company.annotationbean")
    public class TestConfiguration{
    }
    
    • 1
    • 2
    • 3
    • 4

    Test类

    ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
    TestBean testBean = cotext.getBean("testBean",TestBean.class);
    System.out.println("testBean = " + testBean);
    
    • 1
    • 2
    • 3

    四.通过属性去注入Bean

    @Component
    public class TestBeanProperty {
    
     @Autowired
     private AnotherBean anotherBeanProperty;
    
     @Override
     public String toString() {
         return "TestBeanProperty{" +
             "anotherBeanProperty=" + anotherBeanProperty +
             '}';
     }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    这里可以通过@AutoWired去自动装配它。

    五.通过List注入Bean

    TestBeanList类

    @Component
    public class TestBeanList {
    
     private List stringList;
    
     @Autowired
     public void setStringList(List stringList) {
         this.stringList = stringList;
     }
    
     public List getStringList() {
         return stringList;
     }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    TestConfiguration类()配置类

    @Configuration
    @ComponentScan("annoBean.annotationbean")
    public class TestConfiguration {
    
        @Bean
        public List stringList(){
           List stringList = new ArrayList();
           stringList.add("List-1");
           stringList.add("List-2");
           return stringList;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    这里我们将TestBeanList进行了注入,对List中的元素会逐一注入。

    下面我们换一种注入List方式
    TestConfiguration类

    @Bean
    //通过该注解设定Bean注入的优先级,不一定连续数字
    @Order(34)
    public String string1(){
        return "String-1";
    }
    
    @Bean
    @Order(14)
    public String string2(){
        return "String-2";
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    注入与List中泛型一样的类型,会自动去匹配类型,及时这里没有任何List的感觉,只是String的类型,但他会去通过List的Bean的方式去注入。
    注意:
    第二种方式的优先级高于第一种,当两个都存在的时候,若要强制去使用第一种方式,则要去指定Bean的id即可。

    六.通过Map去注入Bean

    @Component
    public class TestBeanMap {
    
     private Map integerMap;
    
     public Map getIntegerMap() {
         return integerMap;
     }
    
     @Autowired
     public void setIntegerMap(Map integerMap) {
         this.integerMap = integerMap;
     }
    }
    /**
    *第一种注入map方式
    */
    @Bean
    public Map integerMap(){
        Map integerMap = new HashMap();
        integerMap.put("map-1",1);
        integerMap.put("map-2",2);
        return integerMap;
    }
    /**
    *第二种注入map方式
    */
    @Bean
    public Integer integer1(){
        return 1;
    }
    
    @Bean
    public Integer integer2(){
        return 2;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    这里跟List一样,第二种优先级大于第一种

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    java网络编程
    窗口函数OVER(PARTITION BY)详细用法——语法+函数+开窗范围ROWS和RANGE
    Hive时间日期函数一文详解+代码实例
    双连通与网络可靠性
    微信小程序遇到的一些问题及解决方法(设备安装)
    Vue3组件通信全解析:利用props、emit、provide/inject跨层级传递数据,expose与ref实现父子组件方法调用
    Python爬虫
    Java面试题汇总
    微服务架构详解
    备战金九银十!2022Java面试必刷461道大厂架构面试真题汇总+面经+简历模板都放这了,注意划重点!!
  • 原文地址:https://blog.csdn.net/m0_67393686/article/details/126116977