一、Spring的依赖注入(DI)

  1.1 xml形式注入 

  (1)普通变量的注入

//普通变量的注入,xml配置property,实体类配置set方法注入
<bean id="person" class="com.jyk.spring.simpletest.Person">
<property name="id" value="1"></property>
<property name="name" value="tom"></property>
</bean> public class Person { public String id;
public String name;
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
} public void printInfo(){
System.out.println("id=="+id);
System.out.println("name=="+name);
}
}

  (2)对象的引用注入,该种形式的xml配置灵活,建议使用内部bean的配置形式

//多对象的引用注入,同样可采用常规的xml配置,对象中配置set方法来实现注入
<bean id="dao" class="com.jyk.spring.simpletest.Dao">
</bean> <bean id="service" class="com.jyk.spring.simpletest.Service">
<property name="dao" ref="dao"></property>
</bean> <bean id="action" class="com.jyk.spring.simpletest.Action">
<property name="service" ref="service"></property>
</bean> //内部bean的配置形式   <bean id="action" class="com.jyk.spring.simpletest.Action">

    <property name="service">
      <bean class="com.jyk.spring.simpletest.Service">
        <property name="dao">
          <bean class="com.jyk.spring.simpletest.Dao"></bean>
        </property>
      </bean>
    </property>
  </bean>

public class Action {

    private Service service;

    public void setService(Service service) {
this.service = service;
} public void execute(){
service.execute();
}
} public class Service { private Dao dao; public void setDao(Dao dao) {
this.dao = dao;
} public void execute(){
dao.execute();
}
} public class Dao { public void execute(){
System.out.println("dao执行逻辑");
}
}

    (3)p 名称空间给对象的属性注入值(该功能仅Spring3.0以上支持)

<bean id="person" class="com.jyk.spring.simpletest.Person" p:id="1" p:name="cat"></bean>
<bean id="dao" class="com.jyk.spring.simpletest.Dao"></bean>

<bean id="service" class="com.jyk.spring.simpletest.Service" p:dao-ref="dao"></bean>

<bean id="action" class="com.jyk.spring.simpletest.Action" p:service-ref="service"></bean>

    (4)装配list,set,map,properties集合

 <bean id="person" class="com.jyk.spring.simpletest.Person">
</bean> <bean id="person1" class="com.jyk.spring.simpletest.Person1">
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<ref bean="person"/>
</list>
</property>
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<ref bean="person"/>
</set>
</property>
<property name="map">
<map>
<entry key="key1">
<value>map1</value>
</entry>
<entry key="key2">
<value>map2</value>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="01">prop1</prop>
<prop key="02">prop2</prop>
</props>
</property>
</bean> public class Person1 { private List list;
private Map map;
private Set set;
private Properties properties;
public void setList(List list) {
this.list = list;
}
public void setMap(Map map) {
this.map = map;
}
public void setSet(Set set) {
this.set = set;
}
public void setProperties(Properties properties) {
this.properties = properties;
} public void printInfo(){
System.out.println("list=="+list);
System.out.println("map=="+map);
System.out.println("set=="+set);
System.out.println("properties=="+properties);
}
}

   1.2 自动装配

     什么是自动装配?就是对属性,方法参数,引用的其他bean,无需通过property的形式来配置,而是通过一定的自动装配策略,交由Spring进行自动适配和装载。强烈建议使用自动装配,因为扩展性高,代码可维护性强,比如在java代码中新增一个属性或引用,只需添加set方法,以及配置文件中配置对应的bean即可。而且自动装配也可以显著减少bean配置的繁琐过程。

     (1)byName 根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。

public class Action {

    private Service service;

    public void setService(Service service) {
this.service = service;
} public void execute(){
service.execute();
}
} public class Service { private Dao dao; public void setDao(Dao dao) {
this.dao = dao;
} public void execute(){
dao.execute();
}
} public class Dao { public void execute(){
System.out.println("dao执行逻辑");
}
} <bean id="dao" class="com.jyk.spring.simpletest.Dao"></bean> <bean id="service" class="com.jyk.spring.simpletest.Service" autowire="byName"></bean> <bean id="action" class="com.jyk.spring.simpletest.Action" autowire="byName"></bean>

    (2)byType 如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如没找到匹配的bean需要抛出异常,则可以通过设置通过设置 
                     dependency-check="objects"来使Spring抛出异常。

<bean id="dao" class="com.jyk.spring.simpletest.Dao"></bean>

<bean id="service" class="com.jyk.spring.simpletest.Service" autowire="byType"></bean>

<bean id="action" class="com.jyk.spring.simpletest.Action" autowire="byType"></bean>

      (3)constructor 与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。并且 constructor会优先查找参数比较多的构造函数

      (4) autodetect 通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式

   1.3 注解形式注入

      Spring提供了一系列注解,通过在Java类中添加各种注解,同样可以达到依赖注入的效果。

     @Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。

     @Resource注解默认按名称装配,如果没有指定name属性,并且按照默认的名称找不到依赖对象时, @Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。

     区别:@Resource注解和@Autowired一样,都可以标注在字段或属性的setter方法上,不同的是当@Resource注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当@Resource注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。而@Autowired则是以bean类型为基准。

     @Qualifier注解按名称装配。

@PostConstruct 指定Bean的初始化方法

       @PreDestroy 指定Bean的销毁方法

Spring基本功能-依赖注入的更多相关文章

  1. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework的依赖注入和控制反转

    Dependency Injection and Inversion of Control 1.概述: 1.1相关概念 bean:由IoC容器所管理的对象,也即各个类实例化所得对象都叫做bean 控制 ...

  2. Spring学习(一)——Spring中的依赖注入简介【转】

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

  3. JavaEE开发之Spring中的依赖注入与AOP

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  4. JavaEE开发之Spring中的依赖注入与AOP编程

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  5. Spring学习(一)——Spring中的依赖注入简介

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

  6. Spring Boot2(007):关于Spring beans、依赖注入 和 @SpringBootApplication 注解

    一.关于Spring beans 和 依赖注入(Dependency Injection) spring boot 和 Spring 全家桶无缝衔接,开发过程中可以很轻松地使用 Spring 全家桶的 ...

  7. 谈谈自己了解的spring.NET的依赖注入

         spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection),以达到解耦的目的,实现模块的组件化.程序 ...

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

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

  9. spring六种种依赖注入方式

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

随机推荐

  1. Spring Boot可以离开Spring Cloud独立使用开发项目,但是Spring Cloud离不开Spring Boot,属于依赖的关系。

    Spring Boot 是 Spring 的一套快速配置脚手架,可以基于Spring Boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具:Spr ...

  2. OAuth2.0 介绍

    一.基本协议流程: (1) Client请求RO(Resource Owner)的授权:请求中一般包含:要访问的资源路径,操作类型,Client的身份等信息.(2) RO批准授权:并将“授权证据”发送 ...

  3. 【BZOJ】1025: [SCOI2009]游戏(置换群+dp+特殊的技巧+lcm)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1025 首先根据置换群可得 $$排数=lcm\{A_i, A_i表示循环节长度\}, \sum_{i= ...

  4. 【BZOJ】1699: [Usaco2007 Jan]Balanced Lineup排队(rmq/树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1699 我是用树状数组做的..rmq的st的话我就不敲了.. #include <cstdio& ...

  5. 使用spring + ActiveMQ 总结

    使用spring + ActiveMQ 总结   摘要 Spring 整合JMS 基于ActiveMQ 实现消息的发送接收 目录[-] Spring 整合JMS 基于ActiveMQ 实现消息的发送接 ...

  6. sql server函数(isnull,charindex,cast,自定义函数)

    SELECT charindex( CAST ( dbo.ufn_IsNullOrEmpty ('109722A3-622D-4FD4-A060-0287C933A89E', a.OUID) AS V ...

  7. HashMap实现原理、核心概念、关键问题的总结

    简单罗列一下较为重要的点: 同步的问题 碰撞处理问题 rehash的过程 put和get的处理过程 HashMap基础: HashMap的理论基础:维基百科哈希表 JDK中HashMap的描述:Has ...

  8. 如何使用 awk 复合表达式

    导读 一直以来在查对条件是否匹配时,我们使用的都是简单的表达式.那如果你想用超过一个表达式来查对特定的条件呢?本文中,我们将看看如何在过滤文本和字符串时,结合多个表达式,即复合表达式,用以查对条件. ...

  9. Json与数组

    今天趁着看源代码的同时,记录学习的小知识. 一.String.Split 方法有6个重载函数: 1) public string[] Split(params char[] separator)2) ...

  10. hdu4525

    可以发现天的操作相当于*(k1+k2) 然后就很好判断了. 威威猫系列故事——吃鸡腿 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 6 ...