SSH与SSM学习之Spring05——Spring属性注入之构造函数注入
2017-10-21 12:30
441 查看
SSH与SSM学习之Spring05Spring属性注入之构造函数注入
一构造函数注入说明
二Car类
三User类
四根据参数默认调用构造函数
1 配置文件
2 测试代码
3 测试结果
五使用index来指定参数在构造函数中的位置
1 配置文件
2 测试代码
3 测试结果
六使用 type 构造函数的参数类型
1 配置文件
2 测试代码
3 测试结果
七通过constructor-arg 中的ref指定引用
1 配置文件
2 测试代码
3 测试结果
八源码下载
如下面的一配置文件
说明如下
constructor-arg 使用构造函数的时候,是用来给参数赋值
name属性: 构造函数的参数名
index属性: 构造函数的参数索引
type属性: 构造函数的参数类型
下面我们来测试。我们使用到 User 类 和 Car 类
一构造函数注入说明
二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 测试代码
/** * 构造方法 方式注入测试 */ @Test public 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 */ @Test public 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 */ @Test public 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指定引用 */ @Test public 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相关文章推荐
- SSH与SSM学习之Spring06——Spring属性注入之p名称空间注入
- SSH与SSM学习之Spring07——Spring属性注入之spel注入
- SSH与SSM学习之Spring08——Spring属性注入之复杂类型注入
- SSH与SSM学习之Spring04——Spring属性注入之set方法注入
- Spring学习_05_Spring中事物属性
- ASP.NET Spring.Net 入门2 简单入门示例--属性与构造函数注入
- SSH学习九 依赖注入及加载Spring配置文件的方法
- SSH与SSM学习之Spring15——cglib代理
- SSH与SSM学习之SSH实现CRM练习05——客户列表05_CustomerAction
- (原)spring学习笔记4.普通属性注入
- Spring依赖属性注入和构造函数注入
- SSH与SSM学习之Spring19——Spring事务之事务基础
- Spring学习笔记(8)----属性注入的方式
- 学习之spring属性文件注入
- 《step3:tiny-Spring-IoC学习三》——为bean注入属性
- Spring笔记之一:依赖注入的方式属性注入和构造函数注入
- Spring学习-10:Bean的集合属性的注入
- Spring学习3——属性注入
- spring in action 学习笔记(六)通过setter注入bean属性
- spring-boot实战【05】:Spring Boo多环境配置及配置属性注入到对象