Spring学习(三)——Spring中的依赖注入的方式
【前面的话】
Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring。不知道为什么对Spring有一种莫名的喜欢感,也许是因为他的名字,有一种希望的感觉。
Spring学习过程中的总结和心得,本文在学习了什么是依赖注入和AOP以后,继续学习依赖注入的方式,请选择性阅读。
本文由牲口TT在博客园首次发表,转载请保持文章的完整性并注明:
作者:牲口TT。
链接:http://www.cnblogs.com/xt0810/p/3628402.html
【相关文章】
【依赖注入方式】
在前面的两篇文章中,学习了什么是依赖注入,什么是AOP,当然,那两篇文章也只是仅仅介绍什么是的问题,我想再通过学习,理解怎么做的问题,这篇文章就是学习在Spring中依赖注入一般是怎么注入的,注入的方式都有哪些,如果你还不理解什么是依赖注入,最好先读读前面两篇文章。
1. 依赖注入的方式:
1) 设值注入:IOC容器使用属性的setter方法来注入被依赖的实例。
2) 构造注入:IOC容器使用构造器来注入被依赖的实例。
3) 静态工厂注入:IOC容器使用静态工厂方法来注入被依赖的实例。
4) 实例工厂注入方式:IOC容器使用实例工厂方法来注入被依赖的实例。
2. setter注入:
主要代码如下——完整代码见【四个demo】 。
- 在Setter这种形式的注入中,Person类并不知道他要实例化的Car类,也就是具体类AuDi类在哪里,他只知道他调用了接口Car,具体的实例化是由Spring来进行的。
- 在测试中,MainTest类分别写了传统方式,也就是不使用Spring的情形,通过比较可以发现其实下面的等价的:
<bean id="person" class="Person">
<property name="car" ref="auDi" />
</bean>
<bean id="auDi" class="AuDi" />
上下等价:
Person person=new Person();
AuDi audi=new AuDi();
person.setCar(audi);
1) Person.java
public class Person { private Car car; public void setCar(Car car){
this.car =car;
} public void driver(){
car.GuaDang();
car.CaiYouMen();
car.DaFangXiang();
}
}
2) MainTest.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainTest {
public static void main(String[] args){
//setter方式进行依赖注入
//使用Spring依赖注入的方式进行注入
ApplicationContext context = new ClassPathXmlApplicationContext("cartest.xml");
Person boy =(Person) context.getBean("person");
boy.driver(); //传统方式进行依赖注入
/*Person person=new Person();
AuDi audi=new AuDi();
person.setCar(audi);
person.driver();*/
}
}
3) cartest.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="person" class="Person">
<property name="car" ref="auDi" />
</bean>
<bean id="auDi" class="AuDi" />
</beans>
3. 构造注入:
主要代码如下——完整代码见【四个demo】 。
- 在构造注入中,Person类并不知道他要实例化的Car类,也就是具体类AuDi类在哪里,他只知道他调用了接口Car,具体的实例化是由Spring来进行的。
- 在测试中,MainTest类分别写了传统方式,也就是不使用Spring的情形,通过比较可以发现其实下面的等价的:
<bean id="person" class="Person">
<constructor-arg ref="Car" />
</bean>
<bean id="Car" class="AuDi" />
上下等价:
AuDi audi=new AuDi();
Person person=new Person(audi);
1) Person.java
public class Person {
private Car car;
public Person(Car car){//构造器注入,传入的是car,也就是一个所有车型都必须实现的接口
this.car =car;//这里可以响应奥迪,宝马等任何一种车的实现。
}//这里Person类没有与任何特定类型的车发生耦合,对于Person来说,任何一种特定的车,只需要实现Car接口即可。具体是哪一种车型,对Person来说无关紧要。 public void driver(){
car.GuaDang();
car.CaiYouMen();
car.DaFangXiang();
}
}
2) MainTest.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainTest {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("cartest.xml");
Person boy =(Person) context.getBean("person");
boy.driver();
}
}
3) cartest.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="person" class="Person">
<constructor-arg ref="Car" />
</bean>
<bean id="Car" class="AuDi" />
</beans>
4. 静态工厂注入:
主要代码如下——完整代码见【四个demo】 。
在MainTest类中就可以看出使用Spring和不使用Spring的区别。
1) CarFactory.java
public class CarFactory {
public static Car getCarInstance(String type){//静态方法,这样做的缺点是如果CarFactory被继承,getCar不能被重写。
Car carInstance = null;
if(type.equals("audi")){
carInstance=new AuDi();
}else if(type.equals("baoma")){
carInstance=new BaoMa();
}
return carInstance;
}
}
2) MainTest.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.xml.XmlBeanFactory; public class MainTest {
public static void main(String[] args){
//这是不使用Spring的情况
//如果是静态方法,直接通过类调用方法即可.
/*Car audicar=CarFactory.getCarInstance("audi");
audicar.Prepare();
audicar.Install();
audicar.Color();
Car baomacar=CarFactory.getCarInstance("baoma");
baomacar.Prepare();
baomacar.Install();
baomacar.Color();*/ //这是使用Spring的情况
//如果是静态方法,直接通过类调用方法即可.
ApplicationContext context = new ClassPathXmlApplicationContext("cartest.xml");
Car caraudi =(Car) context.getBean("audi");
caraudi.Prepare();
caraudi.Install();
caraudi.Color();
Car carbaoma =(Car) context.getBean("baoma");
carbaoma.Prepare();
carbaoma.Install();
carbaoma.Color();
}
}
3) cartest.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="audi" class="CarFactory" factory-method="getCarInstance">
<constructor-arg>
<value>audi</value>
</constructor-arg>
</bean>
<bean id="baoma" class="CarFactory" factory-method="getCarInstance">
<constructor-arg>
<value>baoma</value>
</constructor-arg>
</bean>
</beans>
5. 实例工厂注入:
主要代码如下——完整代码见【四个demo】 。
在MainTest类中就可以看出使用Spring和不使用Spring的区别。
1) CarFactory.java
public class CarFactory {
public Car getCarInstance(String type){//非静态方法
Car carInstance = null;
if(type.equals("audi")){
carInstance=new AuDi();
}else if(type.equals("baoma")){
carInstance=new BaoMa();
}
return carInstance;
}
}
2) MainTest.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.xml.XmlBeanFactory; public class MainTest {
public static void main(String[] args){ //这是不使用Spring的情况
//不是静态方法需要先实例化对象,然后在调用方法。
/*CarFactory carfactory =new CarFactory();
Car audicar=carfactory.getCarInstance("audi");
audicar.Prepare();
audicar.Install();
audicar.Color();
Car baomacar=carfactory.getCarInstance("baoma");
baomacar.Prepare();
baomacar.Install();
baomacar.Color();*/ //这是使用Spring的情况
//不是静态方法需要先实例化对象,然后在调用方法。
ApplicationContext context = new ClassPathXmlApplicationContext("cartest.xml");
Car caraudi =(Car) context.getBean("audi");
caraudi.Prepare();
caraudi.Install();
caraudi.Color();
Car carbaoma =(Car) context.getBean("baoma");
carbaoma.Prepare();
carbaoma.Install();
carbaoma.Color();
}
}
3) cartest.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="carfactory" class="CarFactory"/>
<bean id="audi" factory-bean="carfactory" factory-method="getCarInstance">
<constructor-arg>
<value>audi</value>
</constructor-arg>
</bean>
<bean id="baoma" factory-bean="carfactory" factory-method="getCarInstance">
<constructor-arg>
<value>baoma</value>
</constructor-arg>
</bean>
</beans>
【四个demo】
【命名空间】
《Spring实战》中对于Spring自带了多种XML命名空间,主要如下:
命名空间 |
用途 |
aop |
为声明切面以及将@AspectJ注解的类代理为Spring切面提供了配置元素 |
beans |
支持声明Bean和装配Bean,是Spring最核心也是最原始的命名空间 |
context |
为配置Spring应用上下文提供了配置元素,包括自动检测和自动装配Bean,注入非Springzhujie |
jee |
提供了与Java EE API的集成,例如JNDI和EJB |
jms |
为声明消息驱动的POJO提供了配置元素 |
lang |
支持配置由Groovy, JRuby,货BeanShell等脚本实现的Bean |
mvc |
启用Spring MVC的能力,例如面向注解的控制器,视图控制器和拦截器 |
oxm |
支持Spring的对象到XML映射配置 |
tx |
提供声明试事务配置 |
util |
提供各种各样的工具类元素,包括把集合配置为Bean,支持属性占位符元素 |
【参考资料】
1. 《Spring in action》 Craig Walls著 耿渊 张卫滨译
2. 《轻量级Java EE 企业应用实战》 李刚著
【后面的话】
有时感觉自己学习挺快的,有时会发现自己学习的节奏好慢,希望可以通过好好努力学习更多的技能。
分享:
芭蕉不展丁香结,同向春风各自愁
——TT
Spring学习(三)——Spring中的依赖注入的方式的更多相关文章
- 在WPF中使用依赖注入的方式创建视图
在WPF中使用依赖注入的方式创建视图 0x00 问题的产生 互联网时代桌面开发真是越来越少了,很多应用都转到了浏览器端和移动智能终端,相应的软件开发上的新技术应用到桌面开发的文章也很少.我之前主要做W ...
- Spring学习笔记(8)——依赖注入
spring依赖注入使用构造器注入使用属性setter方法注入使用Field注入(用于注解方式) 注入依赖对象可以采用手工装配或自动装配,在实际应用中建议使用手工装配,因为自动装配会产生未知情况,开发 ...
- spring 配置bean的方法及依赖注入发方式
Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & 实例工厂方法).FactoryBean 这里依据全类名配置bean <bean id="helloWo ...
- Spring学习-理解IOC和依赖注入
最近刚买了一本介绍ssm框架的书,里面主要对Mybatis.spring.springmvc和redis做了很多的讲解,个人觉得虽然有的内容我看不懂,但是整体上还是不错的.最近正在学习中,一边学习一边 ...
- Spring总结三:DI(依赖注入)
简介: 所谓的依赖注入,其实是当一个bean实例引用到了另外一个bean实例时spring容器帮助我们创建依赖bean实例并注入(传递)到另一个bean中,比如你使用Spring容器创建的对象A里面需 ...
- Spring学习:简单实现一个依赖注入和循环依赖的解决
依赖注入 什么是依赖注入 使用一个会创建和查找依赖对象的容器,让它负责供给对象. 当a对象需要b对象时,不再是使用new创建,而是从容器中获取,对象与对象之间是松散耦合的关系,有利于功能复用. 依赖: ...
- spring学习(三) ———— spring事务操作
前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...
- Spring学习02——控制反转、依赖注入
有两个人,张三和李四 package com.su.service; public class ZhangSan implements Tester{ public void test(){ Syst ...
- Spring学习(三)-----Spring自动装配Beans
在Spring框架,可以用 auto-wiring 功能会自动装配Bean.要启用它,只需要在 <bean>定义“autowire”属性. <bean id="custom ...
随机推荐
- PHP递归创建多级目录(一道面试题的解题过程)
今天看到一道面试题,要写出一个可以创建多级目录的函数: 我的第一个感觉就是用递归创建,具体思路如下: function Directory($dir){ if(is_dir($dir) || @mkd ...
- redis状态查看
https://redis.readthedocs.org/en/latest/server/slowlog.html https://redis.readthedocs.org/en/lat ...
- Altium Designer PCB制作入门实例
概要:本章旨在说明如何生成电路原理图.把设计信息更新到PCB文件中以及在PCB中布线和生成器件输出文件.并且介绍了工程和集成库的概念以及提供了3D PCB开发环境的简要说明.欢迎使用Altium De ...
- Class to connect postgres with python in psycopg2
For we need to connect the postgres db in project very frequently, so write the follows class: impor ...
- Android 系统架构
Android 系统从下至上分为四层:Linux 内核.Android 核心库及Android 运行时环境(Android Runtime). 应用程序框架以及应用程序等. Linux 内核(Linu ...
- HBase性能调优
因官方Book Performance Tuning部分章节没有按配置项进行索引,不能达到快速查阅的效果.所以我以配置项驱动,重新整理了原文,并补充一些自己的理解,如有错误,欢迎指正. 配置优化 zo ...
- 两种动态加载JavaScript文件的方法
两种动态加载JavaScript文件的方法 第一种便是利用ajax方式,第二种是,动静创建一个script标签,配置其src属性,经过把script标签拔出到页面head来加载js,感乐趣的网友可以看 ...
- UESTC 901 方老师抢银行 --Tarjan求强连通分量
思路:如果出现了一个强连通分量,那么走到这个点时一定会在强连通分量里的点全部走一遍,这样才能更大.所以我们首先用Tarjan跑一遍求出所有强连通分量,然后将强连通分量缩成点(用到栈)然后就变成了一个D ...
- FZU 2148 Moon Game --判凹包
题意:给一些点,问这些点能够构成多少个凸四边形 做法: 1.直接判凸包 2.逆向思维,判凹包,不是凹包就是凸包了 怎样的四边形才是凹四边形呢?凹四边形总有一点在三个顶点的内部,假如顶点为A,B,C,D ...
- mysql创建表
说明:此文件包含了blog数据库中建立所有的表的Mysql语句. 在sql语句中注意“约束的概念": 1.实体完整性约束(主键--唯一且非空) primary key() 违约处理:No a ...