zoukankan      html  css  js  c++  java
  • Spring 自动装配

    自动装配就是会根据 数据的类型 与 数据的名字  来进行自动注入,减少xml代码数量。

    byName  :   autowier="byName"

    <?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-3.0.xsd">
    
       <!-- Definition for textEditor bean -->
       <bean id="textEditor" class="com.tutorialspoint.TextEditor">
           <property name="spellChecker" ref="spellChecker" />
           <property name="name" value="Generic Text Editor" />
       </bean>
    
       <!-- Definition for spellChecker bean -->
       <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
       </bean>
    
    </beans>

    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

    <?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-3.0.xsd">
    
       <!-- Definition for textEditor bean -->
       <bean id="textEditor" class="com.tutorialspoint.TextEditor" 
          autowire="byName">
          <property name="name" value="Generic Text Editor" />
       </bean>
    
       <!-- Definition for spellChecker bean -->
       <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
       </bean>
    
    </beans>

    byType     autowire="byType"

    <?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-3.0.xsd">
    
       <!-- Definition for textEditor bean -->
       <bean id="textEditor" class="com.tutorialspoint.TextEditor">
          <property name="spellChecker" ref="spellChecker" />
          <property name="name" value="Generic Text Editor" />
       </bean>
    
       <!-- Definition for spellChecker bean -->
       <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
       </bean>
    
    </beans>

    ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

    <?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-3.0.xsd">
    
       <!-- Definition for textEditor bean -->
       <bean id="textEditor" class="com.tutorialspoint.TextEditor" 
          autowire="byType">
          <property name="name" value="Generic Text Editor" />
       </bean>
    
       <!-- Definition for spellChecker bean -->
       <bean id="SpellChecker" class="com.tutorialspoint.SpellChecker">
       </bean>
    
    </beans>

    构造函数自动注入       autowire="constructor"

    <?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-3.0.xsd">
    
       <!-- Definition for textEditor bean -->
       <bean id="textEditor" class="com.tutorialspoint.TextEditor">
          <constructor-arg  ref="spellChecker" />
          <constructor-arg  value="Generic Text Editor"/>
       </bean>
    
       <!-- Definition for spellChecker bean -->
       <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
       </bean>
    
    </beans>

     ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

    <?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-3.0.xsd">
    
       <!-- Definition for textEditor bean -->
       <bean id="textEditor" class="com.tutorialspoint.TextEditor" 
          autowire="constructor">
          <constructor-arg value="Generic Text Editor"/>
       </bean>
    
       <!-- Definition for spellChecker bean -->
       <bean id="SpellChecker" class="com.tutorialspoint.SpellChecker">
       </bean>
    
    </beans>
  • 相关阅读:
    2019-5-24-WPF-源代码-从零开始写一个-UI-框架
    2019-8-31-dotnet-通过-WMI-获取系统安装的驱动
    2018-12-18-WPF-一个空的-WPF-程序有多少个窗口
    2018-11-20-UWP-开发中,需要知道的1000个问题
    2019-8-31-C#-已知点和向量,求距离的点
    2018-10-31-C#-7.0-使用下划线忽略使用的变量
    2019-3-16-win10-uwp-鼠标移动到图片上切换图片
    MSP432 BSL流程(UART)
    UART串口简介
    C++ STL容器
  • 原文地址:https://www.cnblogs.com/qie-zi/p/8854338.html
Copyright © 2011-2022 走看看