zoukankan      html  css  js  c++  java
  • Spring 注入简介

    注入方式有三种,setter,构造方法,接口注入.

     

    常用的是setter注入和构造方法注入.

     

    setter注入:

    <?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">

      <bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl">
      </bean>
       
      <bean id="userService" class="com.bjsxt.service.UserService">
          <property name="userDAO" ref="u" />
             
      </bean>
     

    </beans>

     

     

    <bean>标签表示new一个对象。ref是某对象的引用。

     

     

    构造方法注入:

    <?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">

      <bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl">
      </bean>
       
      <bean id="userService" class="com.bjsxt.service.UserService" init-method="init" destroy-method="destroy" scope="prototype">
          <!--
          <property name="userDAO" ref="u" />
           -->
           <constructor-arg>
               <ref bean="u"/>
           </constructor-arg>
      </bean>
     

    </beans>

     

    被注入类:

    package com.bjsxt.service;
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;

     

    public class UserService {
       
        private UserDAO userDAO; 
        public void add(User user) {
            userDAO.save(user);
        }
        public UserDAO getUserDAO() {
            return userDAO;
        }
        public void setUserDAO(UserDAO userDAO) {
            this.userDAO = userDAO;
        }
       
        public UserService(UserDAO userDAO) {
            super();
            this.userDAO = userDAO;
        }
    }

     

     

     

    另外列举两种setter注入的情况:

     

    注入基本类型:

    <bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
        <property name="daoId" value="8"></property>
        <property name="daoStatus" value="good"></property>
    </bean>

     

     

    注入集合:

     

    <?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">

      <bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
          <property name="sets">
              <set>
                  <value>1</value>
                  <value>2</value>
              </set>
          </property>
          <property name="lists">
              <list>
                  <value>1</value>
                  <value>2</value>
                  <value>3</value>
              </list>
          </property>
          <property name="maps">
              <map>
                  <entry key="1" value="1"></entry>
                  <entry key="2" value="2"></entry>
                  <entry key="3" value="3"></entry>
                  <entry key="4" value="4"></entry>
              </map>
          </property>
      </bean>

      <bean id="userService" class="com.bjsxt.service.UserService">
      <!--
          <property name="userDAO">
              <ref bean="userDAO"/>
          </property>
           -->
           <constructor-arg>
               <ref bean="userDAO"/>
           </constructor-arg>
      </bean>


    </beans>

     

     

     

     

     

     

    更多注入内容参照 spring-framework-referencehtml:5.4.1 Dependency injection

    复制点关于构造方法注入的内容放这备用:

    Constructor argument resolution

    Constructor argument resolution matching occurs using the argument's type. If no potential ambiguity exists in the constructor arguments of a bean definition, then the order in which the constructor arguments are defined in a bean definition is the order in which those arguments are supplied to the appropriate constructor when the bean is being instantiated. Consider the following class:

    package x.y;
    
    public class Foo {
    
      public Foo(Bar bar, Baz baz) {
          // ...
      }
    }

    No potential ambiguity exists, assuming that Bar and Baz classes are not related by inheritance. Thus the following configuration works fine, and you do not need to specify the constructor argument indexes and/or types explicitly in the <constructor-arg/> element.

    <beans>
      <bean id="foo" class="x.y.Foo">
          <constructor-arg ref="bar"/>
          <constructor-arg ref="baz"/>
      </bean>
    
      <bean id="bar" class="x.y.Bar"/>
      <bean id="baz" class="x.y.Baz"/>
    
    </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;
      }
    }
    Constructor argument type matching

    In the preceding scenario, the container can use type matching with simple types if you explicitly specify the type of the constructor argument using the type attribute. For example:

    <bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg type="int" value="7500000"/>
    <constructor-arg type="java.lang.String" value="42"/>
    </bean>
    Constructor argument index

    Use the index attribute to specify explicitly the index of constructor arguments. For example:

    <bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg index="0" value="7500000"/>
    <constructor-arg index="1" value="42"/>
    </bean>

    In addition to resolving the ambiguity of multiple simple values, specifying an index resolves ambiguity where a constructor has two arguments of the same type. Note that theindex is 0 based.

    Constructor argument name

    As of Spring 3.0 you can also use the constructor parameter name for value disambiguation:

    <bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg name="years" value="7500000"/>
    <constructor-arg name="ultimateanswer" value="42"/>
    </bean>

    Keep in mind that to make this work out of the box your code must be compiled with the debug flag enabled so that Spring can look up the parameter name from the constructor. If you can't compile your code with debug flag (or don't want to) you can use @ConstructorProperties JDK annotation to explicitly name your constructor arguments. The sample class would then have to look as follows:

    package examples;
    
    public class ExampleBean {
    
      // Fields omitted
    
      @ConstructorProperties({"years", "ultimateAnswer"})
      public ExampleBean(int years, String ultimateAnswer) {
          this.years = years;
          this.ultimateAnswer = ultimateAnswer;
      }
    }
  • 相关阅读:
    C# DataGridView显示行号的三种方法
    jQuery之前端国际化jQuery.i18n.properties
    大龄程序员的未来在何方
    【翻译】HTML5开发——轻量级Web Database存储库html5sql.js
    HBuilder webApp开发 Websql增删改查操作
    JS 互相调用iframe页面中js方法、VUE里 iframe 互调方法
    工具,百度编辑器 UEditor 使用实例化
    html 表格head头部不动 body部分滚动,每格宽同内容增加
    JS 正则收集(邮箱验证等)
    jQuery,title、仿title功能整理
  • 原文地址:https://www.cnblogs.com/flying607/p/3485095.html
Copyright © 2011-2022 走看看