- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
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>标签的命名规范:
一般情况下,java的属性变量名都以小写字母开头,但考虑到一些特殊意义的英文缩略词,java bean也允许一些大写字母开头的变量名。但必须满足以下两点:
对于属性注入方式来说,只能人为的在配置文件中提供保证,而无法在语法级别提供保证。此时就需要使用构造函数注入这种方式,以此来更好的满足要求.
2.构造函数注入 。
构造函数注入是除属性注入之外的另一种常用的注入方式,它可以保证一些必要的属性在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.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提供了三种可供选择的注入方式,但在实际应用中,我们究竟该选择哪种注入方式,并没有统一的标准,如下是一些可以参考的理由:
构造函数注入理由:
属性注入理由:
其实spring为我们注入参数提供了这么多方法,那么这些方法必然有他们存在的道理,每个方法在某一问题上会有独特的优势,我们只需要按照我们具体的使用需求选择适合的方法来使用就好了,但一般不太推荐工厂方法注入.
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://www.cnblogs.com/Kevin-ZhangCG/p/9080183.html 。
最后此篇关于详解Spring中bean的几种注入方式的文章就讲到这里了,如果你想了解更多关于详解Spring中bean的几种注入方式的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!