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 ...
随机推荐
- Android Studio使用中的异常
Android studio教程:[4]真机测试 1.连不上手机 Android Studio识别不了手机(最后还是恢复成勾中的状态),重启,Android Studio连接真机没反应? 2.连上手机 ...
- MySQL 约束的讲解
MySQL 约束 作用:保证数据的完整性和一致性按照约束的作用范围分为:表级约束和行级约束.常见的约束类型包括: Not null(非空约束) Primary key (主键约束) Unique ke ...
- [资料]Nginx做IP访问限制以及正则规则
nginx配置location总结及rewrite规则写法 Nginx Location配置总结 Nginx 禁止某个IP访问 server { listen 443; root /webroot/; ...
- [CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零
1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are ...
- 20145215《Java程序设计》第10周学习总结
20145215<Java程序设计>第十周学习总结 教材学习内容总结 网络编程 网络概述 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定 ...
- Windows的Subversion备份脚本
2015-12-08更新:备份时添加--revision head 只备份最新的版本,已从脚本中移除. 2013-12-09更新:forfiles命令添加错误输出日志. 2013-12-04更新:添加 ...
- 嵌入式Linux驱动开发之helloword心得
自从选择了物联网这个专业,智能XX的字样牵动着每一个学习这个专业的孩子. 大家兴致勃勃的来到了学校,结果一切想象和自己的设想并不一样.想象中的各种智能般梦幻的场景变成了真实的高数/电路/模电等等诸如此 ...
- Java并发编程-CopyOnWriteArrayList
CopyOnWriteArrayList原理 首先每次写操作,都将数组copy一份,并赋值给arrays 读操作读不加锁 写操作加锁 ReentrantLock 因为每次写都要copy数组,这是一项繁 ...
- Unity3D 的摄像机
什么是摄像机 Unity3D中,摄像机是一个非常非常重要的组件. 他的作用就是:将你设计的场景投影到设备的屏幕上. 摄像机的属性 1 clear flags 确定屏幕的哪一部分将被清除. 每个摄像机在 ...
- [BZOJ 2186][Sdoi2008]沙拉公主的困惑(欧拉函数)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2186 分析: 就是要求1~n!中与m!互质的数的个数 首先m!以内的就是φ(m!) 关 ...