一、构造方法方式注入

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属性依赖注入的更多相关文章

  1. Spring 属性依赖注入

    1.1    属性依赖注入 依赖注入方式:手动装配 和 自动装配 手动装配:一般进行配置信息都采用手动 基于xml装配:构造方法.setter方法 基于注解装配: 自动装配:struts和spring ...

  2. (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)

    Spring要把xml配置中bean的属性实例化为具体的bean,"依赖注入"是关卡.所谓的"依赖注入",就是把应用程序对bean的属性依赖都注入到spring ...

  3. Spring的依赖注入(DI)三种方式

    Spring依赖注入(DI)的三种方式,分别为: 1.  接口注入 2.  Setter方法注入 3.  构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...

  4. 一步一步深入spring(3)--spring的依赖注入方式

    对于spring配置一个bean时,如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象的过程,sprin ...

  5. spring的依赖注入是什么意思

    最近学习spring框架,对依赖注入有些模糊,遂上网翻阅资料,做了下列总结,原博客为CSDN 南夏的 spring的依赖注入是什么意思,侵删! Spring 能有效地组织J2EE应用各层的对象.不管是 ...

  6. Spring.NET依赖注入框架学习--实例化容器常用方法

    Spring.NET依赖注入框架学习---实例化容器常用方法 本篇学习实例化Spring.NET容器的俩种方式 1.通过XmlObjectFactory创建一个Spring.NET容器 IResour ...

  7. Spring.NET依赖注入框架学习--简单对象注入

    Spring.NET依赖注入框架学习--简单对象注入 在前面的俩篇中讲解了依赖注入的概念以及Spring.NET框架的核心模块介绍,今天就要看看怎么来使用Spring.NET实现一个简单的对象注入 常 ...

  8. Spring.NET依赖注入框架学习-- 泛型对象的创建和使用

    Spring.NET依赖注入框架学习-- 泛型对象的创建和使用 泛型对象的创建方法和普通对象是一样的. 通过构造器创建泛型对象 下面是一个泛型类的代码: namespace GenericsPlay ...

  9. Spring中依赖注入的四种方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...

随机推荐

  1. poj3984迷宫问题 广搜+最短路径+模拟队列

    转自:http://blog.csdn.net/no_retreats/article/details/8146585   定义一个二维数组: int maze[5][5] = { 0, 1, 0, ...

  2. C#属性有什么作用

    1,主要作用:将读,写权限分开.如果不使用属性,仅使用public, protected, private,这几个限制都是读,写属性一起的,可读就可写,不可读同时也不可写.不能实现只读不可写,只写不可 ...

  3. OAF页面隐藏右上角的全局按钮(主页,注销等)

    OAPageLayoutBean page = pageContext.getPageLayoutBean(); page.prepareForRendering(pageContext); page ...

  4. 20135316王剑桥 linux第十一周课实验笔记

    getenv函数 1.获得环境变量值的函数 2.参数是环境变量名name,例如"HOME"或者"PATH".如果环境变量存在,那么getenv函数会返回环境变量 ...

  5. 【面试必备】Swift 面试题及其答案

    初级 问题1- Swift 1.0 or later 什么是optional类型,它是用来解决什么问题的? 答案:optional类型被用来表示任何类型的变量都可以表示缺少值.在Objective-C ...

  6. 学习Java第一卷--态度的转变

    你对自己的未来有什么规划?做了哪些准备? 认真学习,修身养性,做自己感兴趣的,在大学一定学好Java,将自己的专业学好学精. 我觉得未来十几年或几十年是自己无法完全控制的,只有自己朝着自己心中的目标努 ...

  7. JavaScript,php文件上传简单实现

    非ajax,非iframe,最原始使用file控件的文件上传,记录过程备忘.(同步,页面刷新) 实现目标,能够将文件上传到指定位置. 客户端用input的file控件: <form action ...

  8. VS 2010配置Qt 4.8.5

    由于早上偶然的一个念想,今天的宝贵时光就奉献给了Qt的安装及其和VS 2010的配置上了. 说实话,开始时真的查找了资料,包括博客.文库.知道等等,作者们写得都很全面,细致. 但自己动手尝试就是错误百 ...

  9. 百度地图 IOS版开发经验分享

    最近刚完成app中地图的应用,感觉非常的曲折,折腾了很久,刚才开始一直不能成功使用百度地图api,报一大堆的错误,后来换到高德地图,又发现服务端使用的百度的经纬度,又从高德换回百度,泪奔. 这里简单比 ...

  10. 《TCP/IP详解卷1:协议》第17、18章 TCP:传输控制协议(2)-读书笔记

    章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...