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 ...
随机推荐
- 全面理解HTTP
URL与URI 我们经常接触到的就是URL了,它就是我们访问web的一个字符串地址,那么URI是什么呢?他们是什么关系呢? URL:uniform resource location 统一资源定位符U ...
- SpringMVC中的@PathVariable
@PathVariable是用来动态获得url中的参数的,代码示例如下: 可以在代码中获得lev_1.lev_2和target参数的值看一下 // 支持跳转到WEB-INF/目录下二层目录 @Requ ...
- MemCached配置与缓存知识概述
先看看百度百科里面对缓存的介绍: 缓存(Cache memory)是硬盘控制器上的一块内存芯片,具有极快的存取速度,它是硬盘内部存储和外界接口之间的缓冲器.由于硬盘的内部数据传输速度和外界介面传输速度 ...
- Android清单文件详解(三)----应用程序的根节点<application>
<application>节点是AndroidManifest.xml文件中必须持有的一个节点,它包含在<manifest>节点下.通过<application>节 ...
- 20135326、20135303-linux实验三实验报告
北京电子科技学院(BESTI) 实 验 报 告 课程:信息安全系统设计基础实验 班级:1353 姓名:魏昊卿 王亦可 学号:20135303 20135326 成绩: ...
- c#字符串转换为日期,支持任意字符串
文章关键字: c#字符串转换为日期 c#日期转换字符串 字符串转换日期 字符串转换为date 整数转换为字符串 浮点数转换为字符串 字符串转换为时间 将字符串转换为时间 字符转 ...
- HTML5 IE兼容问题
最近,为公司做产品的时候用到了HTML5,用IE11打开的时候出现了界面乱或者加载js错误的问题. IE10 or IE11 :Browser Mode is IE10 .Document Mode: ...
- 上位机用USB做虚拟串口,总算抓到一个纯代码的总结了,没有坑的完美解决。
用libUSB来实现自己的驱动+下位机理论速度.=1M字节每秒. 达到极限速度 WINDOWS已经自带虚拟串口驱动,只不过还需要一个Inf文件 方法1:直接下载一个串口inf,来修改文件. 方 ...
- 直接修改托管堆栈中的type object pointer(类型对象指针)
都知道.NET是一个强对象类型的框架. 那么对于对象类型又是怎么确定的呢. 最初的我简单认为数据的类型就是定义时字段的类型修饰决定的(回来发现这种观点是绝对错误的) 我们知道引用对象存储在托管堆栈中, ...
- IT男的”幸福”生活"系列暂停更新通知
首先谢谢博客园,这里给了我很多快乐.更给了大家一个学习的好地方. 在这几天更新过程中,看到了很多哥们的关注,在这里我谢谢你们,是你们给了我动力,是你们又一次给了我不一样的幸福. 在续5中我已回复了,博 ...