Spring-DI

作者: 打死你的小白兔 | 来源:发表于2018-03-31 04:29 被阅读0次
使用构造器注入
使用xml的注入方式
A.  通过参数的顺序
<constructor-arg index="0">
      <value>张三</value>
</constructor-arg>
<constructor-arg index="1">
       <value>56</value>
 </constructor-arg> 

B.  通过参数的类型
<constructor-arg type="java.lang.Integer">
              <value>56</value>
</constructor-arg>

<constructor-arg type="java.lang.String">
              <value>张三</value>
</constructor-arg>
使用属性setting方法进行注入
使用xml的注入方式:
A.  简单Bean的注入
简单Bean包括两种类型:包装类型和String
<bean id="personService"   class="com.itcast.bean.impl.PersonServiceImpl">
<!-- 基本类型,string类型 -->
<property name="age" value="20"></property>
<property name="name" value="张无忌"></property>                        </bean>
B.  引用其他Bean
<bean id="person" class="com.itcast.bean.Person" />
 <bean id="personService"  class="com.itcast.bean.impl.PersonServiceImpl">
 <property name="person" ref="person" />
</bean>

C.<list>
<value>list1</value>
<value>list2</value>
<!-- list中存放一个student对象-->
<ref bean="student"/></list>

D.Object[] 
</property>
<property name="objects">
<list>
<value>obj1</value>
<ref bean="student"/>
</list>
</property>

E.set
<property name="sets">
<set>
<value>set1</value>
<ref bean="student"/>
</set>
</property>

F.map
<property name="map">
<map>
<entry key="m1">
<value>m1</value>
</entry>
<entry key="m2">
<ref bean="student"/>
</entry>
</map>

G.properties
</property>
<property name="properties">
<props>
<prop key="p1">p1</prop>
<prop key="p2">p2</prop>
</props>
</property>
package com.hw.entity;
public class Student {
    public Student(){
        System.out.println("student");
    }
    public void say(){
        System.out.println("student");
    }
}
-----------------------------------
public class Person {
    private Long pid;
    private String name;
    private Student student;
    private List lists;
    private Set sets;
    private Map map;
    private Object[] objects;
    private Properties properties;

setter() getter()省略
---------------------------------
1.都为默认的情况
     * 1、启动spring容器
     * 2、给person创建对象
     * 3、给student创建对象
     * 4、调用person的各个属性的setter方法赋值
     * 5、context.getBean
     * 6、对象调用方法

2.默认情况系Student启动Lazyinit_TRUE()
          * 1、启动spring容器
     * 2、给person创建对象
     * 3、给student创建对象
     * 4、调用person的各个属性的setter方法赋值
     * 5、context.getBean
     * 6、对象调用方法

3.默认情况系都启动Lazyinit_TRUE()
     * 1、启动spring容器
     * 2、创建Student对象
     * 3、context.getBean
     * 4、创建person对象
     * 5、调用setter方法赋值
     * 6、对象调用方法

4.person为prototype,student为默认
     * 1、启动spring容器
     * 2、创建Student对象
     * 3、context.getBean
     * 4、创建person对象
     * 5、调用setter方法赋值
     * 6、对象调用方法

5.student为prototype,person为默认
     * 1、启动spring容器
     * 2、创建person对象
* 3、创建student对象student的scope为"prototype",但是创建对象在spring容器启动的时候
     因为Person中的很多属性都依赖于student,而这些属性的赋值发生在spring容器启动的时候
     * 4、调用setter方法赋值
     * 5、 context.getBean
     * 6、对象调用方法
setter() 与init()方法的执行顺序

setter() 在前,init()在后

     * 1、启动spring容器
     * 2、实例化person,student
     * 3、调用person的setter方法进行装配
     * 4、调用person的init方法
     * 5、context.getBean
     * 6、person调用方法完成任务

相关文章

  • Spring-DI

    使用构造器注入 使用属性setting方法进行注入 setter() 与init()方法的执行顺序 setter(...

  • Spring-DI

    Spring利用依赖注入(DI),完成对IOC容器中中各个对象的依赖关系赋值;具体注入规则如下: 默认优先按照类型...

  • Spring-DI(依赖注入)

    Spring DI依赖注入 setter设置注入(保证类中有提供setter方法) 构造器注入 命名空间注入 1、...

网友评论

    本文标题:Spring-DI

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