Spring-基于设置函数的依赖注入

Spring 基于设置函数的依赖注入

当容器调用一个无参的构造函数或一个无参的静态factory方法来初始化你的bean后,通过容器在你的bean上调用设值函数,基于设值函数的DI就完成了。

下面是TextEditor.java:

package com.tuorialsponit;

public class TextEditor {
    private SpellChecker spellChecker;
    public void spellCheck() {
        spellChecker.checkSpelling();
    }
    public SpellChecker getSpellChecker() {
        return spellChecker;
    }
    public void setSpellChecker(SpellChecker spellChecker) {
        System.out.println("Inside setSpellChecker");
        this.spellChecker = spellChecker;
    }
    
}

SpellChecker.java

package com.tuorialsponit;

public class SpellChecker {
    public SpellChecker(){
        System.out.println("Inside SpellChecker constructor.");
    }
    
    public void checkSpelling(){
        System.out.println("Inside checkSpelling.");
    }
}

MainApp.java文件的内容:

public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//        HelloWorld obj1 = (HelloWorld) context.getBean("helloworld");
//        System.out.println(obj1.getMessage1());
//        System.out.println(obj1.getMessage2());
//        
//        System.out.println("-----------------------");
//        HelloIndia obj2 = (HelloIndia) context.getBean("helloIndia");
//        System.out.println(obj2.getMessage1());
//        System.out.println(obj2.getMessage2());
//        System.out.println(obj2.getMessage3());
//        String message = obj.getMessage();
//        System.out.println(message);
        TextEditor textEditor = (TextEditor) context.getBean("textEditor");
        textEditor.spellCheck();
    }

配置文件beans.xm的内容:

<bean id="textEditor" class="com.tuorialsponit.TextEditor">
         <!-- <constructor-arg ref="spellChecker"></constructor-arg> -->
         <property name="spellChecker" ref="spellChecker"></property>
     </bean>
     
     <bean id="spellChecker" class="com.tuorialsponit.SpellChecker">
     </bean>

你应该注意定义在基于构造函数注入和基于设置函数注入中beans.xml文件的区别。唯一的区别在于使用的标签元素不同。第二个你需要注意的点是,如果你要把一个引用传递给一个对象,那么你需要使用标签的ref属性,而如果你要直接传递一个值,那么你应该使用value属性。

运行结果:

 使用p-namespace实现XML配置

如果你有很多的设置函数方法,那么在xml配置文件中使用p-namespace是非常方便的。让我们查看一下区别:

 xmlns:p="http://www.springframework.org/schema/p"
<bean id="helloworld" class="com.tuorialsponit.HelloWorld"
     p:message1="Hello world"
         p:message2="Hello Second World">
         <!-- <property name="message1" value="Hello world"/>
         <property name="message2" value="Hello Second World"></property> -->
     </bean>

在这里,你不应该区别指定原始值和带有p-namespace的对象引用。-ref部分表明这不是一个直接的值,而是对另一个bean的引用。

注入内部Beans

正如你所知道的Java内部类是在其他类的范围内被定义的,同理,inner beans是在其他bean的范围内定义的bean。

如下所示:

<bean id="outerBean" class="..."> <property name="target"> <bean id="innerBean" class="..."></bean> </property> </bean>

 beans.xml如下:

<bean id="textEditor" class="com.tuorialsponit.TextEditor">
         <!-- <constructor-arg ref="spellChecker"></constructor-arg> -->
         <property name="spellChecker">
             <bean id="spellChecker" class="com.tuorialsponit.SpellChecker">
         </bean>
         </property>
     </bean>

转载于:https://www.cnblogs.com/fangpengchengbupter/p/7817020.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值