Spring 依赖注入

  1. 属性注入(最常使用)
  2. 构造函数注入
  3. 工厂方法注入(很少使用,不推荐)

属性注入

通过 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}
Car{brand='BaoMa', address='HuaChen', price=0.0, maxSpeed=230000}

通过控制台输出我们看到 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}
----------------------
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学习--依赖注入的方式的更多相关文章

  1. java开发两年,连Spring的依赖注入的方式都搞不清楚,你工作可能有点悬!

    Spring依赖注入 常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的 ...

  2. Spring学习-依赖注入

    Spring是基于IOC与AOP的框架,而其中的IOC(Inversion of Control)即反转控制是Spring的基础. 在以前学过的知识中,一个新的对象全部为自己手动new出来的,而在Sp ...

  3. Java 系列之spring学习--依赖注入(二)

    一.依赖注入的三种方式 接口注入,set注入,构造函数注入 二.构造函数注入 2.1.测试类 package test; public class test01 { public String msg ...

  4. Spring学习(三)——Spring中的依赖注入的方式

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  5. Spring的依赖注入(DI)三种方式

    Spring依赖注入(DI)的三种方式,分别为: 1.  接口注入 2.  Setter方法注入 3.  构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...

  6. Spring.NET依赖注入框架学习--实例化容器常用方法

    Spring.NET依赖注入框架学习---实例化容器常用方法 本篇学习实例化Spring.NET容器的俩种方式 1.通过XmlObjectFactory创建一个Spring.NET容器 IResour ...

  7. Spring.NET依赖注入框架学习--简单对象注入

    Spring.NET依赖注入框架学习--简单对象注入 在前面的俩篇中讲解了依赖注入的概念以及Spring.NET框架的核心模块介绍,今天就要看看怎么来使用Spring.NET实现一个简单的对象注入 常 ...

  8. Spring.NET依赖注入框架学习--简介

    Spring.NET依赖注入框架学习--Spring.NET简介 概述 Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序.它提供了很多方面的功能,比如依赖注入. ...

  9. Spring.NET依赖注入框架学习--入门

    Spring.NET依赖注入框架学习--入门 在学些Spring.net框架之前,有必要先脑补一点知识,比如什么是依赖注入?IOC又是什么?控制反转又是什么意思?它们与Spring.net又有什么关系 ...

随机推荐

  1. octave简易操作

    语言以分号;结尾if for while等语句后用,来承接关系if ,elseif ,else ,end;for i=1:10,end;while a>3 ,end;   while true, ...

  2. python操作nosql数据库之memcache

    一.memcache的安装 1.memcache简介 Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象减少读取数据库的次数,从而 ...

  3. 高德API+Python解决租房问题(.NET版)

    源码地址:https://github.com/liguobao/58HouseSearch 在线地址:58公寓高德搜房(全国版):http://codelover.link:8080/ 周末闲着无事 ...

  4. 导入execl到数据库mysql

    GwykhrenyuankuList <body jwcid="$content$"> <span jwcid="@components/AppBord ...

  5. nginx error_page

    1. error_page语法 语法: error_page code [ code... ] [ = | =answer-code ] uri | @named_location 默认值: no 使 ...

  6. L007- linux系统优化进阶课堂小节

    首先把这节课所讲的大概引锁一下,然后下面详细列举. 1.填加普通用户,通过sudo管理. 2.更改默认的SSH服务端口及禁止root用户远程连接. 3.定时自动更新服务器时间 4.关闭防火墙(ipta ...

  7. Redis进阶:数据持久化,安全,在PHP中使用

    一.redis数据持久化 由于redis是一个内存数据库,如果系统遇到致命问题需要关机或重启,内存中的数据就会丢失,这是生产环境所不能允许的.所以redis提供了数据持久化的能力. redis提供了两 ...

  8. Wireshark lua dissector 对TCP消息包合并分析

    应用程序发送的数据报都是流式的,IP不保证同一个一个应用数据包会被抓包后在同一个IP数据包中,因此对于使用自制dissector的时候需要考虑这种情况. Lua Dissector相关资料可以见:ht ...

  9. Win10下Pytorch的安装和使用[斗之力三段]

    简介: 看到paper的代码是用Pytorch实现的,试图理解代码,但是看不懂,只能先学一些基础教程来帮助理解.笔记本电脑配置较低,所以安装一个没有CUDA的版本就可以了.安装完之后,就可以跟着教程边 ...

  10. [译]在SQL查询中如何映射(替换)查询的结果?

    问题来源: https://stackoverflow.com/questions/38567366/mapping-values-in-sql-select 有一个表格,就称它Plant,它有三列: ...