Spring学习--依赖注入的方式
Spring 依赖注入
- 属性注入(最常使用)
- 构造函数注入
- 工厂方法注入(很少使用,不推荐)
属性注入
通过 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} |
构造函数注入
通过构造函数注入 Bean 的属性值或依赖的对象 , 它保证了 Bean 实例在实例化后就可以使用。构造函数注入在 <constructor-arg> 元素里声明属性 , 没有 name 属性。
<?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} |
通过控制台输出我们看到 IOC 容器初始化调用的构造函数出现的匹配问题,匹配不到咱们想要调用的构造函数。这时候我们就得继续给 <constructor-arg> 中添加新的属性。分为:按索引匹配入参(index),按类型匹配入参(type) , 可混合使用,也可同时使用。
<?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} |
Spring学习--依赖注入的方式的更多相关文章
- java开发两年,连Spring的依赖注入的方式都搞不清楚,你工作可能有点悬!
Spring依赖注入 常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的 ...
- Spring学习-依赖注入
Spring是基于IOC与AOP的框架,而其中的IOC(Inversion of Control)即反转控制是Spring的基础. 在以前学过的知识中,一个新的对象全部为自己手动new出来的,而在Sp ...
- Java 系列之spring学习--依赖注入(二)
一.依赖注入的三种方式 接口注入,set注入,构造函数注入 二.构造函数注入 2.1.测试类 package test; public class test01 { public String msg ...
- Spring学习(三)——Spring中的依赖注入的方式
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...
- Spring的依赖注入(DI)三种方式
Spring依赖注入(DI)的三种方式,分别为: 1. 接口注入 2. Setter方法注入 3. 构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...
- Spring.NET依赖注入框架学习--实例化容器常用方法
Spring.NET依赖注入框架学习---实例化容器常用方法 本篇学习实例化Spring.NET容器的俩种方式 1.通过XmlObjectFactory创建一个Spring.NET容器 IResour ...
- Spring.NET依赖注入框架学习--简单对象注入
Spring.NET依赖注入框架学习--简单对象注入 在前面的俩篇中讲解了依赖注入的概念以及Spring.NET框架的核心模块介绍,今天就要看看怎么来使用Spring.NET实现一个简单的对象注入 常 ...
- Spring.NET依赖注入框架学习--简介
Spring.NET依赖注入框架学习--Spring.NET简介 概述 Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序.它提供了很多方面的功能,比如依赖注入. ...
- Spring.NET依赖注入框架学习--入门
Spring.NET依赖注入框架学习--入门 在学些Spring.net框架之前,有必要先脑补一点知识,比如什么是依赖注入?IOC又是什么?控制反转又是什么意思?它们与Spring.net又有什么关系 ...
随机推荐
- [Cracking the Coding Interview] 4.2 Minimal Tree 最小树
Given a sorted(increasing order) array with unique integer elements, write an algorithm to create a ...
- ActivityStream是什么?什么是Feed流?
我先说说feed流吧,它就是社交网站中用户活动信息流,例如用户写了博客.发了照片.评论了什么等等.Facebook叫newsFeed.推特叫TimeLineFeed.ActivityStream是这些 ...
- 1.使用pycharm搭建开发调试环境【转】
感谢 feigamesnb 第一步:安装python2.7环境 去https://www.python.org/downloads/下载windows版本的python,选择2.7版本,按提示安装,并 ...
- 9 udp广播
udp有广播 写信 tcp没有广播· 打电话 #coding=utf-8 import socket, sys dest = ('<broadcast>', 7788) # 创建udp ...
- Linq工具篇(1)——使用LinqPad
学习Linq,有一个非常强大的工具,那就是LinqPad,具体功能有多强大就不说了,网上百度一下就可以知道,百闻不如一见,用用就知道,在网上下载一个绿色版的,无需安装,直接运行,界面如下: 具体功能, ...
- How to Upload multiple files to documentLibrary in one time
In a Sharepoint 2013 website,we can upload one file to the documentlibrary by click "Uploa ...
- XML与Object的范型转换
前段时间做object转换xml想了很多,所有打算整理下 做成以下的通用方法. public static bool ObjectToXml<T>(string filePath, T t ...
- python保留关键字和常用关键字
python保留关键字和常用关键字如下: 上图是python3中的关键字,python2.7中的关键字部分会有区别,具体在自己打印输出查看: import keyword print ' '.join ...
- javac一次性编译多个包下的.java文件
如题是我想要知道的,然后在网上搜了一下 下面是在某些帖子里看到别人说的只言片语 =========================================================== ...
- 简历编写技巧-java开发工程师简历实战
看到一遍简历编写的文章 想到也快找工作了 早晚能够用上 现在摘录如下 640?wx_fmt=jpeg 工欲善其事,必先利其器,这是自古以来的道理.所以如果想找到一份好的工作,一定要先整理一份好的简历. ...