SSH与SSM学习之Spring05——Spring属性注入之构造函数注入
来源:互联网 发布:软件测试与分析 编辑:程序博客网 时间:2024/05/21 09:22
- SSH与SSM学习之Spring05Spring属性注入之构造函数注入
- 一构造函数注入说明
- 二Car类
- 三User类
- 四根据参数默认调用构造函数
- 1 配置文件
- 2 测试代码
- 3 测试结果
- 五使用index来指定参数在构造函数中的位置
- 1 配置文件
- 2 测试代码
- 3 测试结果
- 六使用 type 构造函数的参数类型
- 1 配置文件
- 2 测试代码
- 3 测试结果
- 七通过constructor-arg 中的ref指定引用
- 1 配置文件
- 2 测试代码
- 3 测试结果
- 八源码下载
SSH与SSM学习之Spring05——Spring属性注入之构造函数注入
一、构造函数注入说明
构造函数注入,就是通过构造函数来实现给属性注入值。前提就是得有构造函数。
如下面的一配置文件
<!--这里会调用 User(int name, int age)--><bean name="user4" class="com.qwm.spring1.bean.User"> <constructor-arg name="name" value="2014" type="int" index="0"/> <constructor-arg name="age" value="18"/> <property name="car" ref="car"/></bean>
说明如下
constructor-arg 使用构造函数的时候,是用来给参数赋值
name属性: 构造函数的参数名
index属性: 构造函数的参数索引
type属性: 构造函数的参数类型
下面我们来测试。我们使用到 User 类 和 Car 类
二、Car类
package com.qwm.spring1.bean;/** * @author: wiming * @date: 2017-09-27 14:59:50 星期三 * @decription: */public class Car { private String color; private String name; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Car{color='" + color + '\'' + ", name='" + name + '\'' + '}'; }}
三、User类
package com.qwm.spring1.bean;import java.io.Serializable;/** * @author: wiming * @date: 2017-09-25 14:45:56 星期一 * @decription: */public class User implements Serializable { private String name; private int age; private Car car; public User() { System.out.println("User实例化了----" + this); } public User(String name, int age) { this.name = name; this.age = age; System.out.println("构造方法: User(String name, int age)"); } public User(int name, int age) { this.name = name + ""; this.age = age; System.out.println("构造方法: User(int name, int age)"); } public User(int age, String name) { this.name = name; this.age = age; System.out.println("构造方法: User(int age, String name)"); } public User(String name, int age, Car car) { this.name = name; this.age = age; this.car = car; System.out.println("构造方法: User(String name, int age, Car car)"); } public void init() { System.out.println("---User---init---"); } public void destory() { System.out.println("---User---destory---"); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } @Override public String toString() { return "User{name='" + name + '\'' + ", age=" + age + ", car=" + car + '}'; }}
四、根据参数默认调用构造函数
这种情况就是我们不指定 index 和 type,默认调用。
4.1 配置文件
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <!--Car配置--> <bean name="car" class="com.qwm.spring1.bean.Car"> <!--值类型注入--> <property name="name" value="兰博基尼"/> <property name="color" value="红色"/> </bean> <!--=============构造方法注入测试============--> <!--constructor-arg 使用构造函数的时候,是用来给参数赋值--> <!-- name属性: 构造函数的参数名 --> <!-- index属性: 构造函数的参数索引 --> <!-- type属性: 构造函数的参数类型--> <bean name="user2" class="com.qwm.spring1.bean.User"> <constructor-arg name="name" value="qiwenming"/> <constructor-arg name="age" value="18"/> <property name="car" ref="car"/> </bean></beans>
4.2 测试代码
/** * 构造方法 方式注入测试 */@Testpublic void test2(){ ApplicationContext ac = new ClassPathXmlApplicationContext("com/qwm/spring1/c_inject/applicationContext.xml"); User user = (User) ac.getBean("user2"); System.out.println(user);}
4.3 测试结果
构造方法: User(int age, String name)User{name='qiwenming', age=18, car=Car{color='红色', name='兰博基尼'}}
五、使用index来指定参数在构造函数中的位置
5.1 配置文件
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <!--Car配置--> <bean name="car" class="com.qwm.spring1.bean.Car"> <!--值类型注入--> <property name="name" value="兰博基尼"/> <property name="color" value="红色"/> </bean> <!--使用 index 来指定参数的索引--> <!--这里会调用 User(String name, int age, Car car)--> <bean name="user3" class="com.qwm.spring1.bean.User"> <constructor-arg name="name" value="qiwenming" index="0"/> <constructor-arg name="age" value="18"/> <property name="car" ref="car"/> </bean></beans>
5.2 测试代码
/** * 构造方法 方式注入测试 * 使用了 index */@Testpublic void test3(){ ApplicationContext ac = new ClassPathXmlApplicationContext("com/qwm/spring1/c_inject/applicationContext.xml"); User user = (User) ac.getBean("user3"); System.out.println(user);}
5.3 测试结果
构造方法: User(String name, int age)User{name='qiwenming', age=18, car=Car{color='红色', name='兰博基尼'}}
六、使用 type 构造函数的参数类型
6.1 配置文件
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <!--Car配置--> <bean name="car" class="com.qwm.spring1.bean.Car"> <!--值类型注入--> <property name="name" value="兰博基尼"/> <property name="color" value="红色"/> </bean> <!--使用 type 构造函数的参数类型--> <!--这里会调用 User(int name, int age)--> <bean name="user4" class="com.qwm.spring1.bean.User"> <constructor-arg name="name" value="2014" type="int" index="0"/> <constructor-arg name="age" value="18"/> <property name="car" ref="car"/> </bean></beans>
6.2 测试代码
/** * 构造方法 方式注入测试 * 使用了 type */@Testpublic void test4(){ ApplicationContext ac = new ClassPathXmlApplicationContext("com/qwm/spring1/c_inject/applicationContext.xml"); User user = (User) ac.getBean("user4"); System.out.println(user);}
6.3 测试结果
构造方法: User(int name, int age)User{name='2014', age=18, car=Car{color='红色', name='兰博基尼'}}
七、通过constructor-arg 中的ref指定引用
7.1 配置文件
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <!--Car配置--> <bean name="car" class="com.qwm.spring1.bean.Car"> <!--值类型注入--> <property name="name" value="兰博基尼"/> <property name="color" value="红色"/> </bean> <!--通过constructor-arg 中的ref指定引用--> <!--这里会调用 User(String name, int age, Car car)--> <bean name="user5" class="com.qwm.spring1.bean.User"> <constructor-arg name="name" value="xiaoming"/> <constructor-arg name="age" value="18"/> <constructor-arg name="car" ref="car"/> </bean></beans>
7.2 测试代码
/** * 构造方法 方式注入测试 * 通过constructor-arg 中的ref指定引用 */@Testpublic void test5(){ ApplicationContext ac = new ClassPathXmlApplicationContext("com/qwm/spring1/c_inject/applicationContext.xml"); User user = (User) ac.getBean("user5"); System.out.println(user);}
7.3 测试结果
构造方法: User(String name, int age, Car car)User{name='xiaoming', age=18, car=Car{color='红色', name='兰博基尼'}}
八、源码下载
https://github.com/wimingxxx/spring01/tree/master/src/com/qwm/spring1/c_inject
阅读全文
0 0
- SSH与SSM学习之Spring05——Spring属性注入之构造函数注入
- SSH与SSM学习之Spring04——Spring属性注入之set方法注入
- SSH与SSM学习之Spring06——Spring属性注入之p名称空间注入
- SSH与SSM学习之Spring07——Spring属性注入之spel注入
- SSH与SSM学习之Spring08——Spring属性注入之复杂类型注入
- spring之构造函数注入属性值与set方法注入属性值
- 【Spring学习】之 属性注入与对象属性注入
- SSH框架构造之三---------------spring 注入
- spring学习之属性注入
- Spring之属性注入
- spring之属性注入
- Spring之构造注入与设值注入
- Spring之set注入+构造注入
- spring学习之集合属性注入
- Spring之ioc操作与属性注入
- Spring高级注入之属性值注入
- Spring高级注入之属性值注入
- Spring高级注入之属性值注入
- Continuous-Time LTI System : The Convolution Integral Representation
- Python 小说内容抓取
- SSH与SSM学习之Spring04——Spring属性注入之set方法注入
- Andrew Ng公开课学习笔记——Coordinate ascent(坐标上升法)
- Linux打开虚拟机错误
- SSH与SSM学习之Spring05——Spring属性注入之构造函数注入
- 刻意学习:持续行动让你人生逆袭
- hdu 4974 A simple water problem(14多校8)思维题
- ubuntu16.04安装和配置上网
- SSH与SSM学习之Spring06——Spring属性注入之p名称空间注入
- 栈的应用--马踏棋盘-贪心加栈
- consul的安装和配置
- Java中IO流分类思维图
- SSH与SSM学习之Spring07——Spring属性注入之spel注入