Spring - 基于 Setter 方法的依赖注入

本文详细介绍了Spring框架中依赖注入的概念及其实现方式,包括构造注入和setter注入的区别,并展示了如何利用XML配置文件进行依赖注入,同时引入了p-namespace简化配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当容器调用一个无参构造函数或者一个无参的静态工厂方法初始化你的bean后,容器通过在你的bean上调用setter的方法完成依赖注入的。

Example

SpellChecker.java

package com.soygrow.SetterDependency;

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

TextEditor.java

package com.soygrow.SetterDependency;

public class TextEditor {
    private SpellChecker spellChecker;

    public void setSpellChecker(SpellChecker spellChecker) {
        System.out.println("Inside setSpellChecker ...");
        this.spellChecker = spellChecker;
    }

    public SpellChecker getSpellChecker() {
        return spellChecker;
    }

    public void spellCheck() {
        spellChecker.checkSpelling();
    }
}

MainApp.java

package com.soygrow.SetterDependency;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("SetterBeans.xml");
        TextEditor te = (TextEditor) context.getBean("textEditor");
        te.spellCheck();
    }
}

SetterBeans.xml

<?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.xsd">

    <bean id="textEditor" class="com.soygrow.SetterDependency.TextEditor">
        <property name="spellChecker" ref="spellChecker"/>
    </bean>

    <bean id="spellChecker" class="com.soygrow.SetterDependency.SpellChecker">

    </bean>
</beans>

如果一切正常,那么运行的结果:

Inside SpellChecker constructor ...
Inside setSpellChecker ...
Inside checkSpelling ...
区别

构造注入以及setter方法的注入有几个点需要注意:
- 构造注入中bean的内部使用 <constructor-arg>
- setter方法注入在bean定义的参数使用<property>
- 如果bean中参数需要另外一个bean,那么使用<property>的ref属性,如果你需要传递值,可以使用value属性,这个在两种方式中是一致的。

XML Configuration using p-namespace

如果你存在很多的setter方法,那么可以使用p-namespace来简化配置文件。

举个例子,比如下面的XML配置文件:

<?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-3.0.xsd">

   <bean id = "john-classic" class = "com.example.Person">
      <property name = "name" value = "John Doe"/>
      <property name = "spouse" ref = "jane"/>
   </bean>

   <bean name = "jane" class = "com.example.Person">
      <property name = "name" value = "John Doe"/>
   </bean>

</beans>

上面的XML配置可以使用p-namespace来简化XML配置文件:

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p = "http://www.springframework.org/schema/p"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id = "john-classic" class = "com.example.Person"
      p:name = "John Doe"
      p:spouse-ref = "jane"/>
   </bean>

   <bean name =" jane" class = "com.example.Person"
      p:name = "John Doe"/>
   </bean>

</beans>

上面只要区分出值和引用的两种参数的传递方式:
- 引用其他bean需要再后面添加-ref

Spring教程专栏地址:http://blog.youkuaiyun.com/column/details/19452.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值