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 .
  • 相关阅读:
    STL容器 erase的使用陷井
    转:VC++线程同步-事件对象
    VC线程同步方法
    C/C++四种退出线程的方法
    rabbitMQ 常用命令
    Spring @Configuration
    Spring RabbitMQ 延迟队列
    rabbitmq web管理界面 用户管理
    Linux下tar.gz 安装
    Linux下RPM软件包的安装及卸载
  • 原文地址:https://www.cnblogs.com/france/p/4808654.html
Copyright © 2011-2022 走看看