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;
      }
    }
  • 相关阅读:
    《TCP/IP 详解 卷1:协议》第 10 章:用户数据报协议
    《TCP/IP 详解 卷1:协议》第 9 章:广播和本地组播(IGMP 和 MLD)
    在新的电脑上部署 Hexo,保留原有博客的方法
    当你不知道变量类型的完整定义时可以采取的操作
    python learning GUI
    python learning Network Programming.py
    python learning Process and Thread.py
    【2017级面向对象程序设计】第2次成绩排行
    python learning IO.py
    python learning Exception & Debug.py
  • 原文地址:https://www.cnblogs.com/flying607/p/3485095.html
Copyright © 2011-2022 走看看