一、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. 关于Unity5.5中固定刚体旋转的方法

    给对象增加刚体后进行碰撞,会使得对象不自主地旋转 为了不让对象+刚体旋转,我们可以选择那个对象的Inspector视图--Rigidbody 2D--Constraints--Freeze Posit ...

  2. 【BZOJ】1044: [HAOI2008]木棍分割(二分+dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1044 如果只求最大的最小,,直接二分就行了...可是要求方案.. 好神! 我竟然想不到! 因为我们得 ...

  3. ssh免密码登录的几个注意事项

    1, authorized_keys文件中每个公钥占一行,不能分成多行. 2,文件夹默认权限为600 3,如果遇到奇怪的问题,可以把.ssh/文件全部删掉,重新用ssh-keygen生成.

  4. json转datatable(正则表达式的方法)

    /// <summary> /// 将json转换为DataTable /// </summary> /// <param name="strJson" ...

  5. bootstrap基础学习一篇

    官网:http://www.bootcss.com/ 这里,主要讲解bootstrap3.关于他的介绍就不用复述了. 1.示例 <!doctype html> <html lang= ...

  6. hdu 2105:The Center of Gravity(计算几何,求三角形重心)

    The Center of Gravity Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  7. JavaScript 二、eval 和 with 函数

    /* * ========================================================= * * JavaScript 词法欺骗 * * 1.欺骗词法作用域,会导致 ...

  8. Activity 5秒 Broadcast 10秒 Service 20秒

    第一:什么会引发ANR? 在Android里,应用程序的响应性是由Activity Manager和WindowManager系统服务监视的 .当它监测到以下情况中的一个时,Android就会针对特定 ...

  9. smarty 总结和分析

    虽然smarty现在已经废弃不用,但是它的原理我们需要了解一下,这也是TP框架的一部分原理,它把前后端分离开,这样前端只需要写静态网页,后端只需要处理数据库和php文件就可以了,phpcms的思路也大 ...

  10. 一句css代码让网站变灰

    <style> html{ -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grays ...