zoukankan      html  css  js  c++  java
  • spring--注入类型--构造方法(不常用)

    3.3.1.1. Constructor Injection

    Constructor-based DI is effected by invoking a constructor with a number of arguments, each representing a dependency. Additionally, calling a static factory method with specific arguments to construct the bean, can be considered almost equivalent, and the rest of this text will consider arguments to a constructor and arguments to a staticfactory method similarly. Find below an example of a class that could only be dependency injected using constructor injection. Notice that there is nothing special about this class.

    public class SimpleMovieLister {
    
        // the SimpleMovieLister has a dependency on a MovieFinder
        private MovieFinder movieFinder;
    
        // a constructor so that the Spring container can 'inject' a MovieFinder
        public SimpleMovieLister(MovieFinder movieFinder) {
            this.movieFinder = movieFinder;
        }
        
        // business logic that actually 'uses' the injected MovieFinder is omitted...
    }



    There is no potential for ambiguity here (assuming of course that Bar and Baz classes are not related in an inheritance hierarchy). Thus the following configuration will work just fine, and you do not need to specify the constructor argument indexes and / or types explicitly.

    <beans>
        <bean name="foo" class="x.y.Foo">
            <constructor-arg>
                <bean class="x.y.Bar"/>
            </constructor-arg>
            <constructor-arg>
                <bean class="x.y.Baz"/>
            </constructor-arg>
        </bean>
    </beans>

    When another bean is referenced, the type is known, and matching can occur (as was the case with the preceding example). When a simple type is used, such as<value>true<value>, Spring cannot determine the type of the value, and so cannot match by type without help. Consider the following class:

    package examples;
    
    public class ExampleBean {
    
        // No. of years to the calculate the Ultimate Answer
        private int years;
    
        // The Answer to Life, the Universe, and Everything
        private String ultimateAnswer;
    
        public ExampleBean(int years, String ultimateAnswer) {
            this.years = years;
            this.ultimateAnswer = ultimateAnswer;
        }
    }

    <?xml version="1.0" encoding="UTF-8"?>
    <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-2.5.xsd">
    
    <!-- udaoimpl is a UserDAOImpl Object-->
      <bean id="udaoimpl" class="com.bjsxt.dao.impl.UserDAOImpl">
      </bean>
    
    <!-- userService is a UserService Object-->
      <bean id="userService" class="com.bjsxt.service.UserService">
      <!-- 最常用的setter方法注入 -->
     	<!--userService's setUserDAO method DI(注入) 之前产生的udaoimpl对象-->
       <!--  <property name="userDAO"><ref bean="udaoimpl"/></property>
       -->
       <!-- 不推荐,可以忘记‘’‘’ userService的构造方法注入 -->
        <constructor-arg>
                <ref bean="udaoimpl"/>
       </constructor-arg>
       <!--<property name="userDAO" ref="udaoimpl"/>  -->
      </bean>
    
    </beans>



    版权声明:本文为博主原创文章,未经博主允许不得转载。

    today lazy . tomorrow die .
  • 相关阅读:
    工作问题:http下载文件,中文文件名在firefox下乱码问题
    Error:不能将"char*"类型的值分配到"LPSTR"类型的实体 也许 "char*"类型的实参与"LPCWSTR"类型的形参不兼容
    Kryonet client disconnects after send a packet to server (java)
    NetBeans Support Weblog
    When Deep Learning Meets Data Alignment: A Review on Deep Registration Networks (DRNs)
    DeepFakes and Beyond: A Survey of Face Manipulation and Fake Detection
    Sketch-to-Art: Synthesizing Stylized Art Images From Sketches
    (转载)除了 MSE loss,也可以试试用它:SSIM 的原理和代码实现
    Face X-ray for More General Face Forgery Detection
    FaceShifter: Towards High Fidelity And Occlusion Aware Face Swapping
  • 原文地址:https://www.cnblogs.com/france/p/4808654.html
Copyright © 2011-2022 走看看