一、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. 关于Unity的C#基础学习(五)

    一.get/set访问器 class Person{ int my_age; //默认私有权限 int sex; //属性,类似于函数,但是又不是函数的东西 public int age{ get{ ...

  2. cpio -H newc参数详解

    -H format 其中个format可以是: ‘bin’ The obsolete binary format. (2147483647 bytes) ‘odc’ The old (POSIX.1) ...

  3. 用户控件(ASCX)向网页(ASPX)传值使用反射实现

    用户控件向网页传递值,方法非常之多,此博文尝试使用反射来实现.在站点中,建一个网页以及一个用户控件. 网页切换至设计模式,拉用户控件至网页上. Default.aspx: <%@ Page La ...

  4. stringstream类操作字符串流

     C++ Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...

  5. JavaScript第三天 boolean和json

    布尔值 true:非零数字.非空字符串.非空对象 false:数字零.空字符串.null空对象.undefined  json JSON(JavaScript Object Notation) 是一种 ...

  6. Laravel5.1 搭建博客 --文章的增删改查

    教程源于:Laravel学院 继文件上传后呢,咱来搞一搞文章的事情. 1 更改数据表 我们需要改改数据表的结构 因为涉及到重命名列名 所以咱需要引入一个包:Doctrine: composer req ...

  7. 《jquery权威指南2》学习笔记------ jquery获取复选框的值

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  8. Map<String, String>的数据处理以及ListView的适配器

    Map<String, String> map = new HashMap<String, String>(); map.put("key1", " ...

  9. Animate a custom Dialog,自定义Dialog动画

    Inside res/style.xml <style name="AppTheme" parent="android:Theme.Light" /> ...

  10. Spring 集合注入

    Spring注入是spring框架的核心思想之一.在实际的开发中,我们经常会遇见这样一些类的注入,这些类中包含一些集合作为类的属性,那么要怎样想类中的集合注入数据呢?本文通过一个简单的示例向大家介绍一 ...