Spring学习--依赖注入的方式
2017-02-28 15:57
309 查看
Spring 依赖注入:
属性注入(最常使用)
构造函数注入
工厂方法注入(很少使用,不推荐)
属性注入:通过 setter 方法注入 Bean 的属性值或依赖的对象 , 使用<property> 元素 , 使用 name 属性指定 Bean 的属性名称 , value 属性或 <value> 子节点指定属性值。
控制台输出:
构造函数注入:通过构造函数注入 Bean 的属性值或依赖的对象 , 它保证了 Bean 实例在实例化后就可以使用。构造函数注入在 <constructor-arg> 元素里声明属性 , 没有 name 属性。
控制台输出:
通过控制台输出我们看到 IOC 容器初始化调用的构造函数出现的匹配问题,匹配不到咱们想要调用的构造函数。这时候我们就得继续给 <constructor-arg> 中添加新的属性。分为:按索引匹配入参(index),按类型匹配入参(type) , 可混合使用,也可同时使用。
控制台输出:
属性注入(最常使用)
构造函数注入
工厂方法注入(很少使用,不推荐)
属性注入:通过 setter 方法注入 Bean 的属性值或依赖的对象 , 使用<property> 元素 , 使用 name 属性指定 Bean 的属性名称 , value 属性或 <value> 子节点指定属性值。
<?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="person" class="com.itdjx.spring.dependency.injection.Person"> <property name="name" value="张三" /> <property name="age" value="23" /> <property name="sex" value="男" /> </bean> </beans>
package com.itdjx.spring.dependency.injection; /** * 属性注入 * * @author Wáng Chéng Dá * @create 2017-02-28 15:14 */ public class Person { private String name; private String sex; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", sex='" + sex + '\'' + ", age=" + age + '}'; } }
package com.itdjx.spring.dependency.injection; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 依赖注入main * * @author Wáng Chéng Dá * @create 2017-02-28 15:16 */ public class MainIOC { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationConfig.xml"); Person person = (Person) app.getBean("person"); System.out.println(person); } }
控制台输出:
Person{name='张三', sex='男', age=23} |
<?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="person" class="com.itdjx.spring.dependency.injection.Person"> <property name="name" value="张三" /> <property name="age" value="23" /> <property name="sex" value="男" /> </bean> <bean id="car" class="com.itdjx.spring.dependency.injection.Car"> <constructor-arg value="BaoMa"/> <constructor-arg value="HuaChen"/> <constructor-arg value="230000"/> </bean> </beans>
package com.itdjx.spring.dependency.injection; /** * 构造器注入 * * @author Wáng Chéng Dá * @create 2017-02-28 15:32 */ public class Car { private String brand; private String address; private double price; private int maxSpeed; public Car(String brand, String address, double price) { this.brand = brand; this.address = address; this.price = price; } public Car(String brand, String address, int maxSpeed) { this.brand = brand; this.address = address; this.maxSpeed = maxSpeed; } public Car(String brand, double price, int maxSpeed) { this.brand = brand; this.price = price; this.maxSpeed = maxSpeed; } public Car() { } @Override public String toString() { return "Car{" + "brand='" + brand + '\'' + ", address='" + address + '\'' + ", price=" + price + ", maxSpeed=" + maxSpeed + '}'; } }
package com.itdjx.spring.dependency.injection; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 依赖注入main * * @author Wáng Chéng Dá * @create 2017-02-28 15:16 */ public class MainIOC { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationConfig.xml"); Person person = (Person) app.getBean("person"); System.out.println(person); Car car = (Car) app.getBean("car"); System.out.println(car); } }
控制台输出:
Person{name='张三', sex='男', age=23} Car{brand='BaoMa', address='HuaChen', price=0.0, maxSpeed=230000} |
<?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="person" class="com.itdjx.spring.dependency.injection.Person"> <property name="name" value="张三" /> <property name="age" value="23" /> <property name="sex" value="男" /> </bean> <bean id="car" class="com.itdjx.spring.dependency.injection.Car"> <constructor-arg value="BaoMa"/> <constructor-arg value="HuaChen"/> <constructor-arg value="230000"/> </bean> <bean id="car2" class="com.itdjx.spring.dependency.injection.Car"> <constructor-arg value="BaoMa" index="0"/> <constructor-arg value="HuaChen" index="1"/> <constructor-arg value="230000" index="2"/> </bean> <bean id="car3" class="com.itdjx.spring.dependency.injection.Car"> <constructor-arg value="BaoMa" index="0"/> <constructor-arg value="HuaChen" index="1"/> <constructor-arg value="230000" index="2" type="double"/> </bean> <bean id="car4" class="com.itdjx.spring.dependency.injection.Car"> <constructor-arg value="BaoMa" index="0"/> <constructor-arg value="HuaChen" index="1"/> <constructor-arg value="230" type="int"/> </bean> </beans>
package com.itdjx.spring.dependency.injection; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 依赖注入main * * @author Wáng Chéng Dá * @create 2017-02-28 15:16 */ public class MainIOC { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationConfig.xml"); Person person = (Person) app.getBean("person"); System.out.println(person); System.out.println("----------------------"); Car car = (Car) app.getBean("car"); System.out.println(car); System.out.println("----------------------"); car = (Car) app.getBean("car2"); System.out.println(car); System.out.println("----------------------"); car = (Car) app.getBean("car3"); System.out.println(car); System.out.println("----------------------"); car = (Car) app.getBean("car4"); System.out.println(car); System.out.println("----------------------"); } }
控制台输出:
Person{name='张三', sex='男', age=23} ---------------------- Car{brand='BaoMa', address='HuaChen', price=0.0, maxSpeed=230000} ---------------------- Car{brand='BaoMa', address='HuaChen', price=0.0, maxSpeed=230000} ---------------------- Car{brand='BaoMa', address='HuaChen', price=230000.0, maxSpeed=0} ---------------------- Car{brand='BaoMa', address='HuaChen', price=0.0, maxSpeed=230} ---------------------- |
相关文章推荐
- Spring 4.0 学习日记(3)--- Spring依赖注入方式
- spring学习笔记(1)-spring依赖注入的几种方式
- spring学习总结(三):IOC & DI 配置 Bean 之配置形式及依赖注入方式
- Spring学习(三)——Spring中的依赖注入的方式
- 【Spring学习笔记三】-依赖注入的两种方式
- Spring学习----------Bean 的三种依赖注入方式介绍
- Spring学习之(四)依赖注入的几种装配方式
- spring学习----依赖注入方式
- Spring学习(十八)Bean 的三种依赖注入方式介绍
- spring2.5 基于注解依赖注入学习--01
- Spring视频学习(一)依赖注入原理
- 框架学习之Spring 第二节 采用Spring管理Bean和依赖注入
- spring依赖注入方式
- spring-依赖注入的方式
- Spring Ioc 之二 -依赖注入的几种方式
- Spring依赖注入原理学习
- Spring Ioc-依赖注入的几种方式
- Spring学习总结1(基础与IOC依赖注入)
- Spring依赖注入的方式
- Spring.net 学习笔记二 依赖注入