gpt4 book ai didi

详解Spring中bean的几种注入方式

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章详解Spring中bean的几种注入方式由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

首先,要学习spring中的bean的注入方式,就要先了解什么是依赖注入。依赖注入是指:让调用类对某一接口的实现类的实现类的依赖关系由第三方注入,以此来消除调用类对某一接口实现类的依赖.

spring容器中支持的依赖注入方式主要有属性注入、构造函数注入、工厂方法注入。接下来将为大家详细介绍这三种依赖注入的方式以及它们的具体配置方法.

1.属性注入 。

属性注入即通过setxxx( )方法注入bean的属性值或依赖对象。由于属性注入方式具有可选择性和灵活性高的特点,因此它也是实际开发中最常用的注入方式.

spring首先会调用bean的默认构造函数实例化bean对象,然后再通过反射的方法调用set方法来注入属性值.

属性注入要求bean提供一个 默认的构造函数 ,并且得为需要注入的属性提供 set方法 .

tips:所谓默认的构造函数,即不带参数的构造函数。如果类中没有自定义任何构造函数,则系统(jvm)会自动生成一个不带参的默认构造函数,如果类中显式的自定义了有参数的构造函数,则系统就不会在自动生成默认构造函数,需要自己手动再加一个无参的构造函数.

下面通过一个实例来演示spring中bean的属性注入方式:

编写一个user类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.kevin.bean;
/**
  * 创建一个类测试bean的属性注入方式
  * @author kevin
  *
  */
public class user { 
   private string username;
   public string getusername() {
     return username;
   }
 
   public void setusername(string username) {
     this .username = username;
   }
}

配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version= "1.0" encoding= "utf-8" ?>
<beans xmlns= "http://www.springframework.org/schema/beans"
   xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance"
   xmlns:context= "http://www.springframework.org/schema/context" xsi:schemalocation="
     http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
 
   <!-- 配置对象 -->
   <bean id= "user" class = "com.kevin.bean.user" >
     <property name= "username" >
       <value>kevin</value>
     </property>
   </bean> 
</beans>

其中,每个属性值对应一个property标签,name属性值为类中属性的名称。在bean实现类中拥有与其对应的实现方法setusername( ).

tips:spring只会检查bean中是否含有setter方法,而对是否有对应的属性变量则不作具体要求,但按照约定俗成的规则我们最好为其设定相应的属性变量.

spring中<property>标签的命名规范:

  1. spring的<property>标签所指定的属性名称和bean实现类的setter方法满足sun javabean的属性命名规范,即xxx的属性对应setxxx( )的方法。

一般情况下,java的属性变量名都以小写字母开头,但考虑到一些特殊意义的英文缩略词,java bean也允许一些大写字母开头的变量名。但必须满足以下两点:

  1. 变量的前两个字母要么全部大写,要么全部小写;
  2. 但以一般编程习惯来说,属性名最好全部使用小写字母,方便编程和阅读。

对于属性注入方式来说,只能人为的在配置文件中提供保证,而无法在语法级别提供保证。此时就需要使用构造函数注入这种方式,以此来更好的满足要求.

2.构造函数注入 。

构造函数注入是除属性注入之外的另一种常用的注入方式,它可以保证一些必要的属性在bean实例化时就得到了设置,并在实例化后就可以使用.

使用构造函数注入的前提是: bean必须提供带参的构造函数.

对于构造函数的注入,配置文件可以有以下几种方式:

  1. 按类型匹配入参
  2. 按索引匹配入参
  3. 联合使用类型和索引匹配入参
  4. 通过自身类型反射匹配入参

【按类型匹配入参方式】 。

编写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.kevin.bean;
/**
  * 编写bean测试按类型匹配入参方式
  * @author kevin
  *
  */
public class person {
   private string name;
   private integer age;
   public string getname() {
     return name;
   }
   public void setname(string name) {
     this .name = name;
   }
   public integer getage() {
     return age;
   }
   public void setage(integer age) {
     this .age = age;
   }
}

编写配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version= "1.0" encoding= "utf-8" ?>
<beans xmlns= "http://www.springframework.org/schema/beans"
   xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance"
   xmlns:context= "http://www.springframework.org/schema/context" xsi:schemalocation="
     http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
 
   <!-- 配置对象 -->
   <bean id= "person" class = "com.kevin.bean.person" >
     <constructor-arg type= "string" >
       <value>kevin</value>
     </constructor-arg>
     <constructor-arg type= "integer" >
       <value> 20 </value>
     </constructor-arg>
   </bean> 
</beans>

spring的配置文件采用和元素标签顺序无关的配置策略,因此可以在一定程度上保证配置信息的确定性.

那么当bean中的构造函数的 多个类型参数一样时 ,按照类型匹配入参的这种方式容易产生混淆,此时就需要使用另一种方式:按照索引匹配入参.

【按照索引匹配入参】 。

编写bean代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.kevin.bean;
/**
  * 编写bean测试按照索引方式入参
  * @author kevin
  *
  */
public class student {
   private string name;
   private string gender;
   private double score;
   public student(string name, string gender, double score) {
     super ();
     this .name = name;
     this .gender = gender;
     this .score = score;
   }
}

配置文件编写如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version= "1.0" encoding= "utf-8" ?>
<beans xmlns= "http://www.springframework.org/schema/beans"
   xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance"
   xmlns:context= "http://www.springframework.org/schema/context" xsi:schemalocation="
     http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
 
   <!-- 配置对象 -->
   <bean id= "student" class = "com.kevin.bean.student" >
     <constructor-arg index= "0" value= "kevin" ></constructor-arg>
     <constructor-arg index= "1" value= "male" ></constructor-arg>
     <constructor-arg index= "2" value= "66" ></constructor-arg>
   </bean> 
</beans>

tips: 在属性注入时,spring按java bean的规范确定配置 属性 和对应的 setter方法 ,并使用java反射机制调用属性的setter方法完成属性注入。但java反射机制并 不会记住构造函数的入参名 ,因此我们不能通过制定构造函数的入参名称来进行构造函数的配置,所以我们只能通过 入参的类型及索引 来间接完成构造函数的属性注入.

【联合使用类型和索引匹配入参】 。

在某些复杂的配置文件当中,需要使用type和index同时出马才能完成构造函数的参数注入。下面使用一个实例来演示.

编写bean:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.kevin.bean;
/**
  * 编写bean测试联合使用类型和索引匹配入参
  * @author kevin
  *
  */
public class teacher {
   private string name;
   private string address;
   private double salary;
   private int age;
   public teacher(string name, string address, double salary) {
     super ();
     this .name = name;
     this .address = address;
     this .salary = salary;
   }
   public teacher(string name, string address, int age) {
     super ();
     this .name = name;
     this .address = address;
     this .age = age;
   }
}

在这个类中,有两个重载的构造函数,他们都有三个参数,在这种情况下使用type和index的方法都不能完成要求,这时候就需要他们两个属性同时使用了.

配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version= "1.0" encoding= "utf-8" ?>
<beans xmlns= "http://www.springframework.org/schema/beans"
   xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance"
   xmlns:context= "http://www.springframework.org/schema/context" xsi:schemalocation="
     http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
 
   <!-- 配置对象 -->
   <bean id= "teacher" class = "com.kevin.bean.teacher" >
     <constructor-arg index= "0" type= "string" >
       <value>kevin</value>
     </constructor-arg>
     <constructor-arg index= "1" type= "string" >
       <value>china</value>
     </constructor-arg>
     <constructor-arg index= "2" type= "int" >
       <value> 20 </value>
     </constructor-arg>
   </bean> 
</beans>

可以看到其实重点在于第三个入参的类型,所以我们在配置文件中指定了索引和类型,这样便可以使得spring知道对哪个构造函数进行参数注入了.

tips: 加入我们得配置文件中存在歧义问题,spring容器是可以正常启动的,并不会报错,它将随机采用一个匹配的构造函数实例化bean。而随机选择的构造函数可能并不是用户所需要的,所以我们在编程时要小心避免出现这种歧义情况.

【通过自身类型反射匹配入参】 。

如果bean构造函数入参的类型是可辨别的,由于java反射机制可以获取构造函数入参的类型,即使构造函数的注入不提供类型和索引的信息,spring依旧可以完成构造函数信息的注入。例如下面实例中manager类的构造函数的入参类型就是可以辨别的.

编写manager类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.kevin.bean;
/**
  * 编写bean测试通过自身类型反射匹配入参方式
  * @author kevin
  *
  */
public class manager {
   private string name;
   private double salary;
   private person person;
   public manager(string name, double salary, person person) {
     super ();
     this .name = name;
     this .salary = salary;
     this .person = person;
   }
}

编写配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version= "1.0" encoding= "utf-8" ?>
<beans xmlns= "http://www.springframework.org/schema/beans"
   xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance"
   xmlns:context= "http://www.springframework.org/schema/context" xsi:schemalocation="
     http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
 
   <!-- 配置对象 -->
   <bean id= "manager" class = "com.kevin.bean.manager" >
     <constructor-arg>
       <value>kevin</value>
     </constructor-arg>
     <constructor-arg>
       <ref bean= "user" />
     </constructor-arg>
     <constructor-arg>
       <ref bean= "person" />
     </constructor-arg>
   </bean> 
</beans>

以上几种方法都可以实现 构造函数参数的注入 ,但是为了避免问题的发生,还是建议使用 显式的index和type 来配置构造函数的入参信息.

3.工厂方法注入 。

工厂方法是应用中被经常使用的设计模式,也是 控制反转 和 单实例设计思想 的主要实现方法。工厂类负责创建一个或多个工厂类实例,工厂类方法一般以接口或抽象类变量的形式返回目标类实例.

工厂类对外屏蔽了目标类的实例化步骤,调用者甚至根本不用指定具体的目标类是什么。由于spring容器以框架的方法提供工厂方法的功能,并以透明的方式开放给开发者。因此很少需要手工编写工程方法。但在一些遗留系统或第三方类库中还是会碰到工程方法,此时便可以使用spring工厂注入的方法来进行spring的注入.

spring工厂注入的方法可以分为 静态 和 非静态 两种.

【非静态工厂方法】 。

有些工厂方法是非静态的,必须实例化工厂类之后才能调用工厂方法。下面通过一个实例来演示.

编写工厂类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.kevin.factorybean;
/**
  * 编写工厂类测试非静态工厂方法注入
  * @author kevin
  *
  */
 
public class bookfactory {
   public book buybook(){
     book book = new book();
     book.setname( "think in java" );
     return book;
   }
}

配置文件编写:

?
1
2
3
4
5
6
7
8
9
10
11
<?xml version= "1.0" encoding= "utf-8" ?>
<beans xmlns= "http://www.springframework.org/schema/beans"
   xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance"
   xmlns:context= "http://www.springframework.org/schema/context" xsi:schemalocation="
     http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
 
   <!-- 配置对象 -->
   <bean id= "bookfactory" class = "com.kevin.factorybean.bookfactory" ></bean>
   <bean id= "book" factory-bean= "bookfactory" factory-method= "buybook" ></bean>
   </beans>

由于bookfactory的工厂方法不是静态的,因此需要先定义一个工厂类的bean,然后通过 factory-bean 属性来引用工厂bean实例。再通过属性 factory-method 来指定对应的工厂方法.

【静态工厂方法】 。

很多工厂类方法都是静态的,这意味着无需创建工厂类实例的情况下就可以调用工厂类方法。因此静态工程方法比非静态工厂方法的调用更加方便简洁。下面通过一个实例来演示静态工厂方法.

编写factory类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.kevin.factorybean;
/**
  * 编写工厂类测试静态工厂方法注入
  * @author kevin
  *
  */
public class carfactory {
   public static car createcar(){
     car car = new car();
     car.setbrand( "lamborghini" );
     return car;
   }
}

编写配置文件:

?
1
2
3
4
5
6
7
8
9
10
<?xml version= "1.0" encoding= "utf-8" ?>
<beans xmlns= "http://www.springframework.org/schema/beans"
   xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance"
   xmlns:context= "http://www.springframework.org/schema/context" xsi:schemalocation="
     http: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http: //www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
 
   <!-- 配置对象 -->
   <bean id= "car" class = "com.kevin.factorybean.car" factory-method= "createcar" ></bean> 
</beans>

总结 。

spring提供了三种可供选择的注入方式,但在实际应用中,我们究竟该选择哪种注入方式,并没有统一的标准,如下是一些可以参考的理由:

构造函数注入理由:

  1. 构造函数保证重要属性预先设置;
  2. 无需提供每个属性的setter方法,减少类的方法个数;
  3. 可以更好地封装类变量,避免外部错误调用。

属性注入理由:

  1. 属性过多时,构造函数变的臃肿;
  2. 构造函数注入灵活性不强,有时需要为属性注入null值;
  3. 多个构造函数时,配置上产生歧义,复杂度升高;
  4. 构造函数不利于类的继承和扩展;
  5. 构造函数注入会引起循环依赖的问题。 

其实spring为我们注入参数提供了这么多方法,那么这些方法必然有他们存在的道理,每个方法在某一问题上会有独特的优势,我们只需要按照我们具体的使用需求选择适合的方法来使用就好了,但一般不太推荐工厂方法注入.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

原文链接:http://www.cnblogs.com/Kevin-ZhangCG/p/9080183.html 。

最后此篇关于详解Spring中bean的几种注入方式的文章就讲到这里了,如果你想了解更多关于详解Spring中bean的几种注入方式的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com