美文网首页
Spring依賴注入

Spring依賴注入

作者: 博尔特uncle | 来源:发表于2018-12-05 15:41 被阅读0次

Spring框架的核心功能之一就是通过依赖注入的方式来管理Bean

依赖注入说白了就是对象之间的引用,包含互相持有引用的对象以及基本类型的参数如何去赋值;而在java中这一切过程我们是直接new对象设置值

分别包括构造函数的注入,设置值函数的注入,注入内部Bean ,注入集合


构造函数注入:

  1. 通过constructor-arg标签配置,
  2. 可以配置引用类型,可以配置基本类型
  3. 直接指定值设置value属性
   <bean class="com.ldl.test.newspring.DITextEditor" id="ditexteditor">
         <constructor-arg type="java.lang.String" value="母猪"/>
         <constructor-arg ref="checker"/>
         <!--<property name="n" value="老炮"/>-->
         <!--      <property name="spellChecker">
        <bean scope="prototype" id="spellChecker" class="com.ldl.test.newspring.SpellChecker">
            <property name="msg" value="小鸡炖蘑菇"/>
        </bean>
    </property>-->
    <!--<property name="spellChecker" ref="checker"/>-->
</bean>

** 通过设值函数注入:**

  1. 就是相当于通过set方法设置值
  2. 容器调用一个无参的构造函数或一个无参的静态方法,容器在bean上调用set设置值方法注入
  3. 通过property 标签设置属性,可以引用类型也可以直接设置值
 <bean class="com.ldl.test.newspring.SpellChecker" id="checker" scope="prototype">
 <property name="msg" value="天王蓋地虎"/>
 <property name="spellChecker" ref="checker"/>

** 注入内部bean**

  1. 实际上就是如果给内部类定义和赋值

看看例子,就是在设置参数的内部配置一个bean,对应的就是java内部类

 <bean class="com.ldl.test.newspring.DITextEditor" id="ditexteditor">
        <!--<constructor-arg type="java.lang.String" value="母猪"/>-->
        <!--<constructor-arg ref="checker"/>-->
   <property name="n" value="老炮"/>
   <property name="spellChecker">
       <bean scope="prototype" id="spellChecker" class="com.ldl.test.newspring.SpellChecker">
           <property name="msg" value="小鸡炖蘑菇"/>
       </bean>
   </property>
   <!--<property name="spellChecker" ref="checker"/>-->

</bean>

集合注入

这个也没什么可说的,看看我写的例子就知道了

<bean id="collection" class="com.ldl.test.collectionz.JavaCollection">
<property name="addressList">
   <list>
       <value>INDIA</value>
       <value>Pakistan</value>
       <value>USA</value>
   </list>
</property>
<property name="addressSet">

   <set>
       <value>AAA</value>
       <value>BBB</value>
       <value>CCC</value>
   </set>
</property>
<property name="addressMap">
   <map>
       <entry key="1" value="INDIA"/>
       <entry key="2" value="Pakistan"/>
       <entry key="3" value="USA"/>
       <entry key="a" value-ref="innera">

       </entry>
   </map>
</property>
<property name="addressProp">
   <props>
       <prop key="One">INDIA</prop>
       <prop key="Two">Pakistan</prop>
       <prop key="Three">USA</prop>
   </props>
</property>
</bean>
<bean id="innera" class="com.ldl.test.collectionz.InnerA">
<property name="mes" value="测试一下"/>
</bean>

设置null

  <bean id="..." class="exampleBean">
  <property name="email" value=""></property>
  </bean>
  <bean id="..." class="exampleBean">
     <property name="email"><null/></property>
  </bean>

相关文章

  • Spring依賴注入

    Spring框架的核心功能之一就是通过依赖注入的方式来管理Bean 依赖注入说白了就是对象之间的引用,包含互相持有...

  • 扪心自问——依赖与控制

    1.你一直想依賴他人,依賴外在,為何不去依賴自己,依賴內在的自己,依賴天地,依賴自然? 2.你忙著追求權利,金錢,...

  • 依賴

    我覺得我太依賴我媽了,什麼事都要她幫我拿主意,什麼事都感覺有她支持就是好的,有安全感。 都說人長大了是個獨立的個體...

  • 依賴!

    這段時間我認為自己很主動了,什麼事情都主動去承擔了,不像以前總是等待依賴,於是自信滿滿,覺得自己成長了就很...

  • SpringMVC

    Spring MV 本身是与 Spring 框架结合而成的, 同时拥有 Spring 的优点((例如依注入 CI ...

  • 3.Spring 依赖注入

    1.Spring 依赖注入 Spring框架的核心功能之一就是通过依赖注入的方式来管理Bean之间的依赖关系。 依...

  • 浅谈golang的依赖注入

    浅谈golang的依赖注入 如果是做web开发,对依赖注入肯定不陌生,java程序员早就习惯了spring提供的依...

  • Spring 学习心得(二)

    Spring IOC again 参数值注入 注入基本值 注入Bean对象 注入Spring表达式值 注入nul...

  • JAVA 核心笔记 || [xxx] Spring 之 依赖注入

    Spring 依赖注入 DL Spring 两种注入方式 Setter 方法注入 构造器注入 使用App.java...

  • 自相矛盾,依賴。

    早上起來溫度就一直在變,從11℃,到10℃,到7℃再到現在的1℃,待會兒說不定就-1℃了呢。 好像八點多的時候是有...

网友评论

      本文标题:Spring依賴注入

      本文链接:https://www.haomeiwen.com/subject/jghwcqtx.html