一、构造方法方式注入

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. c# nullable类型有什么用

    可空类型,语法: ;            int? inully = 10; Nullable<int> inullx0 = null;            int? inully0 ...

  2. Math类和Random类(数学公式相关类)

    Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. 常用方法: 1.static 数值类型 abs(数值类型 a)      返回 double 值的绝对值. 2.sta ...

  3. GDB堆栈跟踪与汇编调试

    GDB堆栈跟踪与汇编调试 堆栈跟踪 源代码: 对预先编写的 stack.c 文件进行编译,并且使用 CGDB 进行调试,对堆栈进行跟踪,了解该代码堆栈是如何变化的. 在 CGDB 中,先设置 main ...

  4. GCD工作单元

    #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak,nonatomic ...

  5. 深入浅出jsonp

    前言 第一次听说jsonp,其实早在2年之前.当时在做一个活动页面的抽奖模块,要从服务端get一个概率,当时什么都不懂,同事说用ajax,我就用ajax,同事说dataType改成jsonp,我就改成 ...

  6. JQuery上传文件插件Uploadify使用笔记

    新工作的第一份任务就是给实现 限制Uploadify 上传文件格式为图片 测试出来报错,选择了非图片文件,提示错误后,再选择其他文件,上传时还是包含了之前清空的非图片文件 最后实现效果的代码是 //上 ...

  7. java heep space错误解决办法

    1.双击tomcat 2.Open launch configuration 3.Argument 4. VM arguments中添加:-Xmx1024M -Xms512M -XX:MaxPermS ...

  8. php模式设计之 单例模式

    模式设计是什么?初学者一开始会被这高大上的名称给唬住.而对于有丰富编程经验的老鸟来说,模式设计又是无处不在.很多接触的框架就是基于各种模式设计形成的. 简单说,在写代码的过程中一开始往往接触的是面向过 ...

  9. WDCP安装并配置php5.4和mongodb

    记录一下,免得忘了.全部都是自己测试用过的安装过程没有问题. linux常用命令 mv 移动文件 mkdir 创建文件夹 rm 删除 cp 复制 netstat 网络状态 tar 解压 wget 下载 ...

  10. angular_$inject

    <!DOCTYPE HTML> <html lang="zh-cn" ng-app="MainApp"> <head> &l ...