spring属性依赖注入
一、构造方法方式注入
1、项目结构如下:

2、新建Customer类
package hjp.spring.attributeinject;
public class Customer {
private String name;
private Integer age;
private String city;
public Customer() {
}
public Customer(String name, Integer age) {
this.name = name;
this.age = age;
}
public Customer(Integer age, String city) {
this.age = age;
this.city = city;
}
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;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Customer [name=" + name + ", age=" + age + ", city=" + city + "]";
}
}
Cusomer
3、新建beans.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<!-- 如果没有配置 constructor-arg节点,则使用无参构造器-->
<bean id="customerId" class="hjp.spring.attributeinject.Customer">
<!--constructor-arg 配置构造参数
index 表示参数索引号
type 设置参数数据类型
value 设置普通数据
ref 设置引用数据 如果只使用index和value,而不指定数据类型,则默认匹配符合条件的第一个构造函数
如果配置了type,那么索引处的数据类型要对应正确
-->
<constructor-arg index="0" value="23" type="java.lang.Integer"></constructor-arg>
<constructor-arg index="1" value="Tom" type="java.lang.String"></constructor-arg>
</bean>
</beans>
4、新建测试类
package hjp.spring.attributeinject; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApp {
@Test
public void demo1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"hjp/spring/attributeinject/beans.xml");
Customer customer = applicationContext.getBean("customerId", Customer.class);
System.out.println(customer);
}
}
二、setter方法注入
1、新增类Contact
package hjp.spring.attributeinject;
public class Contact {
private String address;
private String telphone;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
@Override
public String toString() {
return "Contact [address=" + address + ", telphone=" + telphone + "]";
}
}
Contact
2、为Customer类新增属性contact,新增构造函数
public Customer(String name,Integer age,Contact contact){
this.name=name;
this.age=age;
this.contact=contact;
}
package hjp.spring.attributeinject;
public class Customer {
private String name;
private Integer age;
private String city;
private Contact contact;
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public Customer() {
}
public Customer(String name, Integer age) {
this.name = name;
this.age = age;
}
public Customer(Integer age, String city) {
this.age = age;
this.city = city;
}
public Customer(String name,Integer age,Contact contact){
this.name=name;
this.age=age;
this.contact=contact;
}
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;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Customer [name=" + name + ", age=" + age + ", city=" + city + ", contact=" + contact + "]";
}
}
Customer
3、更改beans.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="customerId" class="hjp.spring.attributeinject.Customer">
<property name="name" value="Tom"></property>
<property name="age" value="23"></property>
<property name="contact" ref="contactId"></property>
</bean>
<bean id="contactId" class="hjp.spring.attributeinject.Contact">
<property name="address" value="北京"></property>
<property name="telphone" value="12345678"></property>
</bean>
</beans>
4、测试类不变
三、集合注入
1、项目结构:

2、新建CollectionTest类
package hjp.spring.attributeinject.collection; import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class CollectionTest {
private List<String> listData;
private Set<String> setData;
private Map<String, String> mapData;
private String[] arrayData;
private Properties propsData; public List<String> getListData() {
return listData;
} public void setListData(List<String> listData) {
this.listData = listData;
} public Set<String> getSetData() {
return setData;
} public void setSetData(Set<String> setData) {
this.setData = setData;
} public Map<String, String> getMapData() {
return mapData;
} public void setMapData(Map<String, String> mapData) {
this.mapData = mapData;
} public String[] getArrayData() {
return arrayData;
} public void setArrayData(String[] arrayData) {
this.arrayData = arrayData;
} public Properties getPropsData() {
return propsData;
} public void setPropsData(Properties propsData) {
this.propsData = propsData;
} @Override
public String toString() {
return "CollectionText [\nlistData=" + listData + ", \nsetData=" + setData + ", \nmapData=" + mapData
+ ", \narrayData=" + Arrays.toString(arrayData) + ", \npropsData=" + propsData + "\n]";
}
}
CollectionTest
3、新建beans.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<!-- 集合内部,普通值用value,引用值用ref -->
<bean id="collectionTestId" class="hjp.spring.attributeinject.collection.CollectionTest">
<!-- list集合 -->
<property name="listData">
<list>
<value>Jim</value>
<value>Tom</value>
<value>Jerry</value>
</list>
</property>
<!-- set集合 -->
<property name="setData">
<set>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</set>
</property>
<!-- map集合 -->
<property name="mapData">
<map>
<!-- 第一种写法 -->
<entry key="A" value="a"></entry>
<entry key="B" value="b"></entry>
<entry key="C" value="c"></entry>
<!-- 第二种写法 -->
<entry>
<key>
<value>D</value>
</key>
<value>d</value>
</entry>
</map>
</property>
<!-- 数组 -->
<property name="arrayData">
<array>
<value>a</value>
<value>b</value>
<value>c</value>
</array>
</property>
<!-- Properties -->
<property name="propsData">
<props>
<prop key="X">x</prop>
<prop key="Y">y</prop>
<prop key="Z">z</prop>
</props>
</property>
</bean>
</beans>
beans
4、新建测试类
package hjp.spring.attributeinject.collection; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApp {
@Test
public void demo1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"hjp/spring/attributeinject/collection/beans.xml");
CollectionTest collectionTest = applicationContext.getBean("collectionTestId", CollectionTest.class);
System.out.println(collectionTest);
}
}
spring属性依赖注入的更多相关文章
- Spring 属性依赖注入
1.1 属性依赖注入 依赖注入方式:手动装配 和 自动装配 手动装配:一般进行配置信息都采用手动 基于xml装配:构造方法.setter方法 基于注解装配: 自动装配:struts和spring ...
- (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)
Spring要把xml配置中bean的属性实例化为具体的bean,"依赖注入"是关卡.所谓的"依赖注入",就是把应用程序对bean的属性依赖都注入到spring ...
- Spring的依赖注入(DI)三种方式
Spring依赖注入(DI)的三种方式,分别为: 1. 接口注入 2. Setter方法注入 3. 构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...
- 一步一步深入spring(3)--spring的依赖注入方式
对于spring配置一个bean时,如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象的过程,sprin ...
- spring的依赖注入是什么意思
最近学习spring框架,对依赖注入有些模糊,遂上网翻阅资料,做了下列总结,原博客为CSDN 南夏的 spring的依赖注入是什么意思,侵删! Spring 能有效地组织J2EE应用各层的对象.不管是 ...
- Spring.NET依赖注入框架学习--实例化容器常用方法
Spring.NET依赖注入框架学习---实例化容器常用方法 本篇学习实例化Spring.NET容器的俩种方式 1.通过XmlObjectFactory创建一个Spring.NET容器 IResour ...
- Spring.NET依赖注入框架学习--简单对象注入
Spring.NET依赖注入框架学习--简单对象注入 在前面的俩篇中讲解了依赖注入的概念以及Spring.NET框架的核心模块介绍,今天就要看看怎么来使用Spring.NET实现一个简单的对象注入 常 ...
- Spring.NET依赖注入框架学习-- 泛型对象的创建和使用
Spring.NET依赖注入框架学习-- 泛型对象的创建和使用 泛型对象的创建方法和普通对象是一样的. 通过构造器创建泛型对象 下面是一个泛型类的代码: namespace GenericsPlay ...
- Spring中依赖注入的四种方式
在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入 这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...
随机推荐
- ie6,ie7兼容性总结(转)
其实浏览器的不兼容,我们往往是各个浏览器对于一些标准的定义不一致导致的,因此,我们可以进行一些初始化,很多问题都很轻松解决. 下面是14条特殊情况仅供参考: 1. 文字本身的大小不兼容.同样是font ...
- workerman & swoole
Socket 开发 workerman swoole swoole与phpdaemon/reactphp/workerman等纯PHP网络库的差异
- Qt——QLineEdit使用总结
QLineEdit是一个单行文本编辑控件. 使用者可以通过很多函数,输入和编辑单行文本,比如撤销.恢复.剪切.粘贴以及拖放等. 通过改变QLineEdit的 echoMode() ,可以设置其属性,比 ...
- 发布HTML5 2D游戏引擎YEngine2D
关于YEngine2D YEngine2D是一个开源的.采用HTML5技术和Javscript语言创建的2D游戏框架,用来构建web二维游戏. GitHub地址 最新版本 v0.1.2 浏览器支持 C ...
- Oracle备份数据库
1.前言 工作中数据库备份是一个很重要的事情,难免有时候一个不小心就会误操作,造成无法挽回的措施.在昨天的工作中,我们的一个产品经理在操作定制端的时候一个误操作,清空了几张表的数据同时还删除了几张系统 ...
- Dictionary使用
/// <summary> /// 除去数组中的空值和签名参数并以字母a到z的顺序排序 /// </summary> /// <param name="dicA ...
- javascript键盘输入控制
获取键盘控制事件 document.onkeydown = keyDown 当浏览器读到这个语句时,无论按下键盘上的哪个键,都将呼叫KeyDown()函数. 不同浏览器的实现: Netscape Ne ...
- WCF学习(二)对控件简单了解以及4个文本控件的简介
WPF基础控件 系统默认提供的基础控件: 文本控件介绍与用法 Label控件 label控件:一般用户描述性文字显示. 在Label控件使用时,一般给予用户提示.用法上没有什么很特殊的,label控件 ...
- java.io.FileNotFoundException:文件名、目录名或卷标语法不正确
出现次错误的原因主要是在windows系统下创建文件需要遵循其文件名的规则.导致创建文件失败,从而提示filenotfound异常,文件未找到 Windows 中文件夹命名规则是: ① 文件名或文件夹 ...
- 【SDOI2008】解题汇总
好叭我真的是闲的了... /---------------------------------------------/ BZOJ-2037 [SDOI2008]Sue的小球 DP+相关费用提前计算 ...