美文网首页
spring_注入方式笔记2

spring_注入方式笔记2

作者: 回去生气0101 | 来源:发表于2018-10-13 10:40 被阅读0次

Spring中,支持 5 种自动装配模式

  • no – 缺省情况下,自动配置是通过“ref”属性手动设定
  • byName – 根据属性名称自动装配。如果一个bean的名称和其他bean属性的名称是一 样的,将会自装配它。
  • byType – 按数据类型自动装配。如果一个bean的数据类型是用其它bean属性的数据类型,兼容并自动装配它。
  • constructor – 在构造函数参数的byType方式。
  • autodetect – 如果找到默认的构造函数,使用“自动装配用构造”; 否则,使用“按类型自动装配”。【在Spring3.0以后的版本被废弃,已经不再合法了】

1. 第一种自动装配【根据属性名称自动装配】

package com.hebeu.model; 

public class Customer { 
 
  private Address address; 
   
  public Customer() { 
     
  } 
   
  public Customer(int id, Address address) { 
    super(); 
    this.address = address; 
  } 
 
  public Address getAddress() { 
    return address; 
  } 
 
  public void setAddress(Address address) { 
    this.address = address; 
  } 
   
} //customer类 

package com.hebeu.model; 
 
public class Address { 
 
  private String fulladdress; 
   
  public Address(){ 
     
  } 
   
  public Address(String addr){ 
    this.fulladdress = addr; 
  } 
 
  public String getFulladdress() { 
    return fulladdress; 
  } 
 
  public void setFulladdress(String fulladdress) { 
    this.fulladdress = fulladdress; 
  } 
   
} //address类

<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-3.0.xsd"> 
   
  //bean的id必须和Customer中声明的变量名相同
  <bean id="customer" class="com.hebeu.model.Customer" autowire="byName"></bean>
   
  <bean id="address" class="com.hebeu.model.Address"> 
    <property name="fulladdress" value="YiLong Road, CA 188"></property> 
  </bean> 
   
</beans>  

这样就将address注入到Customer中。这就是自动注入ByName.在Customer bean中公开了一个属性address,Spring容器会找到address bean,并且装配。这里必须要注意,Customer中要被注入的bean的set方法要求必须是public的,否则会报空指针异常。还有配置的bean的id必须和Customer中声明的变量名相同。

2. 第二种自动装配【根据数据类型自动装配】
声明的俩个bean同样为Customer以及Address,将applicationContext.xml转换为这样的就是实现根据数据类型进行自动装配。

<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-3.0.xsd"> 
   
  <bean id="customer" class="com.hebeu.model.Customer" <strong><span style="color:#FF0000;">autowire="byType"</span></strong>></bean> 
   
  <bean id="bean1" class="com.hebeu.model.Address"> 
    <property name="fulladdress" value="YiLong Road, CA 188"></property> 
  </bean> 
</beans>  

类型自动装配的意思是如果一个bean的数据类型与其他的bean属性的数据类型相同,将会自动兼容装配它。当然要求只能配置一个某一个类型的bean.如果配置成这样,那么是会出错的。

<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-3.0.xsd"> 
   
  <bean id="customer" class="com.hebeu.model.Customer" autowire="byType"></bean> 
   
  <bean id="bean1" class="com.hebeu.model.Address"> 
    <property name="fulladdress" value="YiLong Road, CA 188"></property> 
  </bean> 
  <bean id="bean2" class="com.hebeu.model.Address"> 
    <property name="fulladdress" value="YiLong Road, CA 188"></property> 
  </bean> 
</beans>  

3. 第三种自动装配【根据构造方法自动装配】
将applicationContext.xml转换为如下,就实现了按照构造方法自动装配:

<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-3.0.xsd"> 
   
  <bean id="customer" class="com.hebeu.model.Customer"> 
    <!-- 构造方法的参数 --> 
    <constructor-arg> 
      <ref bean="bean1"/> 
    </constructor-arg> 
  </bean> 
   
  <bean id="bean1" class="com.hebeu.model.Address"> 
    <property name="fulladdress" value="YiLong Road, CA 188"></property> 
  </bean> 
   
</beans> 

它实际上是构造函数的参数类型自动装配。这意味着如果一个bean的数据类型与其他bean的构造器参数的数据类型是相同的,那么就自动装配。

相关文章

  • spring_注入方式笔记2

    Spring中,支持 5 种自动装配模式 no – 缺省情况下,自动配置是通过“ref”属性手动设定 byName...

  • spring_注入方式笔记

    spring容器负责生成实例 Set注入这是最简单的注入方式,假设有一个SpringAction,类中需要实例化一...

  • DI

    0.常见2种注入方式 1.构造方法注入 2.setter方法注入

  • 01-struts属性注入

    继承属性 复合属性注入 复合属性注入:接口方式 属性注入: struts.xml方式 struts2利用反射技术将...

  • 20170725

    4. 尚硅谷_佟刚_Spring_属性配置细节 2种命名空间 尚硅谷spring-ppt ============...

  • spring泛形bean注入的2种方式

    spring泛形bean注入的2种方式 通过类继承的方式自动依赖注入extends 通过调用构造方法注入@Serv...

  • b05-2 Spring依赖注入(精通SPring 4.x)

    本篇内容: 1、属性注入 2、构造方法注入(注入方式:参数名称,参数类型,参数索引,反射注入) 3、循环注入 4、...

  • Spring依赖注入(IoC)的四种方式

    Spring中四种依赖注入的方式(推荐注解注入) 注解注入(1) 注解注入 (2) 构造函数注入 代码如下(如果不...

  • Spring学习笔记(二)-依赖注入详解

    1.依赖注入 有两种注入方式: (1).bean.xml中的配置:(这是方式一的注入) (2).对应的java代码...

  • Spring入门(一)

    一,XML注入的两种方式 1,设值注入 2,构造注入 二,IOC 三,单元测试

网友评论

      本文标题:spring_注入方式笔记2

      本文链接:https://www.haomeiwen.com/subject/fmfoaftx.html