zoukankan      html  css  js  c++  java
  • [Spring] Autowire

    1.  Autowire可以让你隐式地注入依赖.(it internally uses setter or constructor injection)

    2.优点:减少代码量.

    3.缺点:无法被程序员控制. 不能用在基本数据类型和string值.

    4. Autowiring Modes模式

    1) no It is the default autowiring mode. It means no autowiring bydefault.
    2) byName The byName mode injects the object dependency according to name of the bean. In such case, property name and bean name must be same. It internally calls setter method.
    3) byType The byType mode injects the object dependency according to type. So property name and bean name can be different. It internally calls setter method.
    4) constructor The constructor mode injects the dependency by calling the constructor of the class. It calls the constructor having large number of parameters.
    5) autodetect It is deprecated since Spring 3.

    byName和byType在另一片随笔中..

    constructor则是和构造函数中的形参有关.

    根据构造函数中的形参,如果有bean和形参名相同,就会将该bean注入到 autowire类型是constructor的bean中(前提是参数类型相同).

    constructor mode会按照最多参数的构造函数优先注入.

    如果有一个无参,单参,双参构造函数,会先调用双参构造函数.

    autowire="no" 是默认的,等价于autowire="default".

    ===========================Dependency Injection with Factory Method in Spring

    Spring Framework provides facility to inject bean suing factory method. To do so, we can use two attributes of bean element.

    1. factory-method: represents the factory method that will be invoked to inject the bean.

    2.factory-bean: represents the reference of the bean by which factory method will be invoked.

    only used when factory method is non-static.

      一个返回类的实例的方法叫做工厂方法.

    public class A[

    public static A getA(){

      return new A();

    }

    }

    工厂方法有三种类型:

      1.返回自身类的实例的static factory method ,使用单例模式(singleton design pattern)

    <bean id="a" class="com.javatpoint.A" factory-method="getA"></bean>  

      2.返回其他类实例的static factory method , 运行时决定其实例化对象.

    <bean id="b" class="com.javatpoint.A" factory-method="getB"></bean>  

      3.non-static factory方法:返回另一个类的实例.

    <bean id="a" class="com.javatpoint.A"></bean>  

    <bean id="b" class="com.javatpoint.A" factory-method="getB" factory-bean="a"></bean>  

     

    =========

    Attribute : factory-method
    The name of a factory method to use to create this object. Use constructor-arg elements to specify arguments to

    the factory method, if it takes arguments.Autowiring does not apply to factory methods.

    使用constructor-arg元素来为工厂方法设置参数,如果它接收参数,autowiring就不会应用于工厂方法.

    If the "class" attribute is present, the factory method will be a static method on the class specified by the "class" attribute on this bean 

    definition.

    如果"class"属性存在,工厂方法就是class属性指定的类的一个静态方法.

    Often this will be the same class as that of the constructed object - for example, when the factory
    method is used as an alternative to a constructor.

    通常,该类和构造的对象是同一类:比如当工厂方法被用作constructor的替代方法时.

    However, it may be on a different class. In that case, the 

    created object will *not* be of the class specified in the "class" attribute.

    然而,也可能是不同的类.

    在这种情况下.被创建的对象不属于class属性中定义的类.

    This is analogous to FactoryBean behavior.

    这和FactoryBean的行为很相似.

    If the "factory-bean" attribute is present, the "class" attribute is not used, and the factory method will 

    be an instance method on the object returned from a getBean call with the specified bean name.

    如果factory-bean属性存在,而class属性不存在,工厂方法就会是getBean方法返回的对象对应的实例方法.

    The factory
    bean may be defined as a singleton or a prototype.

    factory bean可以被定义成单例子或者多例模型.

    The factory method can have any number of arguments.Autowiring is not supported.

    工厂方法可以有任意数量的参数.但不支持autowiring.

    Use indexed constructor-arg elements in conjunction with the factory-method 

    attribute.

    使用constructor-arg来和工厂方法拼接.

    Setter Injection can be used in conjunction with a factory method.

    Setter Injection可以使用在工厂方法中.

    Method Injection cannot, as the factory method returns an instance, which will be used when the container creates the bean.

    Data Type : string

    宛如智障,暗藏锋芒
  • 相关阅读:
    定义函数的三种形式
    函数的定义
    文件修改的两种方式
    文件的高级应用
    with管理文件操作上下文
    SQL Server 823,824 错误
    SQL Server 无法启动的 4 种原因
    SQL Server 查看正在运行的事务信息的 2 种方法。
    MySQL 指定数据库字符集的 3 种方法。
    MYSQL 注释的 3 方法
  • 原文地址:https://www.cnblogs.com/zienzir/p/9126078.html
Copyright © 2011-2022 走看看