Spring中各种依赖注入的代码实现

DI:Spring称之为依赖注入,就是维护对象和对象之间的关系
通俗的讲,就是 给类的成员赋值。
1、属性赋值:

class A{
String str = “Hello  Word !”
}
注入:
<bean id="A" class="类A的全称">
        <property name="str" value="Hello  Word"></property>
</bean>

2、依赖注入的几种方法:

A. 构造器注入
B. Setter方法注入
C. 方法注入

3、构造器注入:
通过构造方法给属性赋值

 <bean id="stu" class="cn.gson.springmavenTest.pojos.Student">
        <!-- 給构造方法的第一个参数赋值 -->
        <constructor-arg index="0" value="0"></constructor-arg>
          <!-- 給构造方法的第二个参数赋值 -->
        <constructor-arg index="1" value="Hello"> </constructor-arg>
        </bean>

4、Setter方法注入:

<bean id="stu"            class="cn.gson.springmavenTest.pojos.Student">
        <property name="sid" value="10"></property>
        <property name="name" value="laobiao"></property>
 </bean>

强调:name不是属性名,而是Setter方法的后半部分
注意:该类必须要有公共的无参构造方法
属性为private的访问级别,不建议为public
属性必要时通过一组Setter和getter方法来访问

5、注入常量

<property name="index" value="10"/> 
或者是:
<property name="index">
<value>10</index>
<property/>

6、注入List集合,使用list标签

<property name="luckColor">
            <list>
                <value>红色</value>
                <value>黄色</value>
                <value>蓝色</value>
            </list>
</property>

7、注入Set集合,使用Set标签

<property name="luckColor">
            <set>
                <value>红色</value>
                <value>黄色</value>
                <value>蓝色</value>
            </set>
</property>

8、注入Map集合,键和值对象可以是任意类型,使用map标签

<property name="lockGril">
        <map>
        <entry key="1" value="杨幂"></entry>
        <entry key="2" value="赵丽颖"></entry>
        <entry key="3" value="王丽坤"></entry>
        </map>
</property>

9、注入数组

<property name="luckNumber">
            <array>
                <value>3</value>
                <value>19</value>
            </array>
</property>

10、注入其他对象(必须是Spring容器中的对象)
Teacher类是Student类中的一个属性变量

<bean id="stu" class="cn.gson.springmavenTest.pojos.Student">
        <property name="teacher" ref="tea"></property>
</bean>
<bean id="tea" class="cn.gson.springmavenTest.pojos.Teacher">
    <property name="name" value="laobiao"></property>
    <property name="age" value="20"></property>
</bean>

11、使用P命名空间简化Setter注入

<bean id="stu" class="cn.gson.springmavenTest.pojos.Student">
        <property name="teacher" ref="tea"></property>
</bean>
    <bean id="tea"   
    class="cn.gson.springmavenTest.pojos.Teacher"
    p:name="laobiao" p:age="20" 
    p:birthday-ref="date"
    </bean>
    <bean id="date" class="java.util.Date">

    </bean>

12、零配置
@Autowired:按照类型进行自动装配

@Component("stu")
public class Student {

    private long sid;
    private String name;
    @Autowired(required=true)
    private Teacher teacher;
    private List<String> luckColor;
    private Set<String> luckCity;
    private Map<Integer,String> lockGril;
    private int[] luckNumber;
}

@Autowired(required=false)当根据类型去找,找不到时,注入一个空。
@Autowired(required=true)当根据类型去找,找不到时,抛出异常信息。
@Qualifier(“tea”):根据名称进行装配,相当于在配置的时候的stu里面的Tea的id

@Component("stu")
public class Student {

    private long sid;
    private String name;
    @Autowired
    @Qualifier("tea") //根据名称进行装配
    private Teacher teacher;
}

后台测试:首先得到核心配置文件,比如:applicationContext.xml

ApplicationContext ac = 
            new ClassPathXmlApplicationContext("appaicationContext.xml");
    //方式一
    Student student = (Student) ac.getBean("stu");
    //方式二
    //Student student = ac.getBean(Student.class);
    //方式三
   // Student student =  ac.getBean("stu", Student.class);
    System.out.println(student.sayHello("zhagnxusheng"));
    //System.out.println(student.getName()+student.getSid());

就会发现,和以前最大的不同点就是不需要去new 这个Student,就获取它的属性和方法
最后,还可以进:注解和配置混合注入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值