您的位置:首页 > 编程语言 > Java开发

[优质例子存档]Spring的两种依赖注入方式:setter方法注入与构造方法注入

2017-06-28 11:44 941 查看
1.setter方法注入

   setter方法注入即是创建一个普通的JavaBean类,为需要注入的属性通过对应的setter方法即可,如:

(1)创建一个Id类:

[java] view
plain copy

package com.loster.li;  

  

public class Id {  

    private int id;  

    private String name;  

    public int getId() {  

        return id;  

    }  

    public void setId(int id) {  

        this.id = id;  

    }  

    public String getName() {  

        return name;  

    }  

    public void setName(String name) {  

        this.name = name;  

    }  

}  

(2)创建配置文件Id_Bean.xml

[html] view
plain copy

<?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="id" class="com.loster.li.Id">  

    <property name="id" value="123"></property>  

    <property name="name" value="xiaoli"></property>  

    </bean>  

</beans>  

(3)编写测试类IdTest.java,并运行:

[java] view
plain copy

package com.loster.li;  

  

import org.springframework.context.support.ClassPathXmlApplicationContext;  

  

public class IdTest {  

    public static void main(String[] args){  

        ClassPathXmlApplicationContext context = new   

                ClassPathXmlApplicationContext("com/loster/li/Id_Bean.xml");  

          

        Id id = (Id)context.getBean("id");  

        System.out.println(id.getId());  

        System.out.println(id.getName());  

    }  

}  

   运行结果如下:



2.构造方法注入

(1)将上面的Id.class修改为:

[java] view
plain copy

package com.loster.li;  

  

public class Id {  

    private int id;  

    private String name;  

    public Id(int id,String name){  

        this.id = id;  

        this.name = name;  

    }  

    public int getId() {  

        return id;  

    }  

    public void setId(int id) {  

        this.id = id;  

    }  

    public String getName() {  

        return name;  

    }  

    public void setName(String name) {  

        this.name = name;  

    }  

}  

(2)将上面的Id_Bean.xml修改为:

[html] view
plain copy

<?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="id" class="com.loster.li.Id">  

    <constructor-arg index="0" value="456"></constructor-arg>  

    <constructor-arg index="1" value="dawang"></constructor-arg>  

    </bean>  

</beans>  

(3)再次运行IdTest.java,运行结果如下:

                                                                                                                    


第二个帖子的例子

1、构造器注入

       这里以“bean应用”歌手唱歌为例,为singer类添加一歌名属性song,并通过构造器注入将song注入。

修改singer类,添加构造函数,其参数为为song:

 


在application.xml中用<constructor-arg>注入song

 


注:index表示参数的位置,value表示参数值

在Test类中,代码如下

 


此时运行Test类,控制台输出

 


注入成功!!!

这只是值属性的注入,但对象的注入也是类型。比如歌手除了唱歌,他还要弹吉他。

这里建一个tool的包,并在里面接一个工具的接口Tool,接口有一方法perform()

 


建一个Guitar类,实现Tool接口

 


修改singer类,在构造器加一Tool属性

 


在application.xml中注入Guitar类(先实例化Guitar再注入)

 


注:用ref引用Guitar对象

此时运行Test,控制台输出


 

构造注入完成

 

2、setter注入(以上面的例子)

首先删掉singer类构造方法,并未song和guitar生成set方法


 

在application.xml中用<property>注入song值和Guitar类

 


注:属性值用value,对象用ref

运行Test类,控制台输出

 


【注意】代码应该有错,其中setter方式中的singer方法应该输出“i want to sing”+ song ,忘了+ 变量 song。、

第三个例子,有ref讲解

网页链接:http://blog.csdn.net/sunnyyoona/article/details/50631178
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: