Spring的自动装配Bean的三种方式
作者:漏断人初静v 发布时间:2023-08-24 23:05:15
spring的自动装配功能的定义:无须在Spring配置文件中描述javaBean之间的依赖关系(如配置<property>、<constructor-arg>)。IOC容器会自动建立javabean之间的关联关系。
如果没有采用自动装配的话,手动装配我们通常在配置文件中进行实现:一下代码就是手动装配:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="customerDAO" class="com.hebeu.customer.dao.JdbcCustomerDAO">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
通过<property name="dataSource" ref="dataSource" />向customerDAO的bean中注入了dataSource
在Spring框架,可以用 auto-wiring 功能会自动装配Bean。要启用它,只需要在 <bean>定义“autowire”属性。
<bean id="customer" class="com.yiibai.common.Customer" autowire="byName" />
在Spring中,支持 5 自动装配模式。
no – 缺省情况下,自动配置是通过“ref”属性手动设定
byName – 根据属性名称自动装配。如果一个bean的名称和其他bean属性的名称是一样的,将会自装配它。
byType – 按数据类型自动装配。如果一个bean的数据类型是用其它bean属性的数据类型,兼容并自动装配它。
constructor – 在构造函数参数的byType方式。
autodetect – 如果找到默认的构造函数,使用“自动装配用构造”; 否则,使用“按类型自动装配”。【在Spring3.0以后的版本被废弃,已经不再合法了】
第一种自动装配【根据属性名称自动装配】
package com.hebeu.model;
public class Customer {
private Address address;
public Customer() {
}
public Customer(int id, Address address) {
super();
this.address = address;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
package com.hebeu.model;
public class Address {
private String fulladdress;
public Address(){
}
public Address(String addr){
this.fulladdress = addr;
}
public String getFulladdress() {
return fulladdress;
}
public void setFulladdress(String fulladdress) {
this.fulladdress = fulladdress;
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="customer" class="com.hebeu.model.Customer" autowire="byName"></bean>
<bean id="address" class="com.hebeu.model.Address">
<property name="fulladdress" value="YiLong Road, CA 188"></property>
</bean>
</beans>
这样就将address注入到Customer中。这就是自动注入ByName.在Customer bean中公开了一个属性address,Spring容器会找到address bean,并且装配。这里必须要注意,Customer中要被注入的bean的set方法要求必须是public的,否则会报空指针异常。还有配置的bean的id必须和Customer中声明的变量名相同。
第二种自动装配【根据数据类型自动装配】
声明的俩个bean同样为Customer以及Address,将applicationContext.xml转换为这样的就是实现根据数据类型进行自动装配。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="customer" class="com.hebeu.model.Customer" <strong><span style="color:#FF0000;">autowire="byType"</span></strong>></bean>
<bean id="bean1" class="com.hebeu.model.Address">
<property name="fulladdress" value="YiLong Road, CA 188"></property>
</bean>
</beans>
类型自动装配的意思是如果一个bean的数据类型与其他的bean属性的数据类型相同,将会自动兼容装配它。当然要求只能配置一个某一个类型的bean.如果配置成这样,那么是会出错的。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="customer" class="com.hebeu.model.Customer" autowire="byType"></bean>
<bean id="bean1" class="com.hebeu.model.Address">
<property name="fulladdress" value="YiLong Road, CA 188"></property>
</bean>
<bean id="bean2" class="com.hebeu.model.Address">
<property name="fulladdress" value="YiLong Road, CA 188"></property>
</bean>
</beans>
第三种自动装配【根据构造方法自动装配】
案例同上,将applicationContext.xml转换为如下,就实现了按照构造方法自动装配:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="customer" class="com.hebeu.model.Customer">
<!-- 构造方法的参数 -->
<constructor-arg>
<ref bean="bean1"/>
</constructor-arg>
</bean>
<bean id="bean1" class="com.hebeu.model.Address">
<property name="fulladdress" value="YiLong Road, CA 188"></property>
</bean>
</beans>
它实际上是构造函数的参数类型自动装配。这意味着如果一个bean的数据类型与其他bean的构造器参数的数据类型是相同的,那么就自动装配。
来源:http://blog.csdn.net/qq_28663043/article/details/56495501?locationNum=2&fps=1


猜你喜欢
- 本文实例讲述了Spring实战之清除缓存操作。分享给大家供大家参考,具体如下:一 配置文件<?xml version="1.
- SpringBoot打jar包遇到的xml文件丢失在pom.xml的build标签中添加如下内容指定资源路径<resources>
- 前言Java虽然五脏俱全但总有软肋,譬如获取CPU等硬件信息,当然我们可以通过JNI调用C/C++来获取,但对于对C/C++和Windows
- 在开发Android App过程中,经常会遇见这样的功能。从当前的app跳转到一个应用商店并且跳转到自己app的详情页面,让用户给自己的Ap
- 一、导言1.1 介绍桥接模式及其应用背景桥接模式(Bridge Pattern)是一种结构型设计模式,它将抽象部分与实现部分分离,使它们可以
- 前言本次主要是实现一个Android应用,实现静态广播、动态广播两种改变 widget内容的方法,即在上篇博文中实验的基础上进行修改,所以此
- 脚本之家在以前介绍过关于C#创建、部署、调用WebService的教程,有兴趣的可以参阅:.NET C#创建WebService服务简单实例
- 这篇文章主要介绍了spring boot如何配置请求的入参和出参json数据格式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一
- 本篇和大家分享的是通过maven对springboot中打war包和jar包;war通常来说生成后直接放到tomcat的webapps下面就
- 这篇文章主要介绍了Java如何利用return结束方法调用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- 前言开发项目中需要进行单文件多文件的上传功能,下面演示的ApiResponse是自己分装的返回值,要根据自己的项目来完成。使用的mvvm框架
- Map在Java8中新增了两个replace的方法1.replace(k,v)在指定的键已经存在并且有与之相关的映射值时才会将指定的键映射到
- Android Studio是谷歌推出一个Android集成开发工具,基于IntelliJ IDEA。它类似于Eclipse ADT,And
- 本文通过实例详细阐述了C++关于智能指针的概念及用法,有助于读者加深对智能指针的理解。详情如下:一、简介由于 C++ 语言没有自动内存回收机
- 以下代码实现了android的免提开启和关闭功能需要添加的权限<uses-permission android:name="
- 定义广播 * 的Action:private static final String TAG ="SmsService"
- C#程序自删除核心实现方法就是调用 cmd 传入命令行,等待几秒之后删除文件;应用程序在运行时,是不能将 exe 文件进行删除的。但是可以将
- 使用WPF做的一个简单的操作文件的demo,包括复制和移动文件夹,核心思想就是使用递归,如果只是移动或者复制单一文件,直接使用File.Co
- 写在前面SpringBoot创建定时任务的方式很简单,主要有两种方式:一、基于注解的方式(@Scheduled)二、数据库动态配置。实际开发
- 本文为大家分享了C#基于Socket套接字的网络通信封装代码,供大家参考,具体内容如下摘要之所以要进行Socket套接字通信库封装,主要是直