这篇关于Spring依赖注入你真得好好看看,写的非常详细,安排!

235 篇文章 4 订阅

Spring 基于构造函数的依赖注入

当容器调用带有一组参数的类构造函数时,基于构造函数的 DI 就完成了,其中每个参数代表一个对其他类的依赖。

spellchecker.java
public class spellChecker {
    public spellChecker(){
        System.out.println("spellcheck init");
    }
    public void check(){
        System.out.println(this.getClass().getName());
        System.out.println("spellchecker check");
    }
}

textEditor.java
public class textEditor {
    private spellChecker checker;
    public textEditor(spellChecker checker){
        System.out.println("textEditor init");
        this.checker = checker;
    }
    public void check(){
        System.out.println(this.getClass().getName());
        this.checker.check();
    }
}

springconfig.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="spellChecker" class="spellChecker"></bean>
    <bean id="textEditor" class="textEditor">
        <constructor-arg ref="spellChecker"></constructor-arg>
    </bean>

</beans>

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

spellchecke.java
public class spellChecker {
    public spellChecker(){
        System.out.println("spellcheck init");
    }
    public void check(){
        System.out.println(this.getClass().getName());
        System.out.println("spellchecker check");
    }
}

texteditor.java
public class textEditor {
    private spellChecker checker;

    public textEditor(){
        System.out.println("textEditor init");
    }
    public void setChecker(spellChecker checker){
         this.checker = checker;
    }
    public void check(){
        System.out.println(this.getClass().getName());
        this.checker.check();
    }
}

springconfig.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="spellChecker" class="spellChecker"></bean>
    <bean id="textEditor" class="textEditor">
        <property name="checker" ref="spellChecker"></property>
    </bean>

</beans>

Spring 注入内部 Beans

Java 内部类是在其他类的范围内被定义的,同理,inner beans 是在其他 bean 的范围内定义的 bean。因此在 或 元素内 元素被称为内部bean

spellcheck.java
public class spellChecker {

    public spellChecker(){
        System.out.println("inner spellcheck init");
    }

    public void check(){
        System.out.println(this.getClass().getName());
        System.out.println("inner spellchecker check");
    }
}

textEditor.java
public class textEditor {
    private spellChecker spellChecker;

    public textEditor(){
        System.out.println("textEditor init");
    }

    public void setSpellChecker(spellChecker spellChecker) {
        this.spellChecker = spellChecker;
    }

    public spellChecker getChecker() {
        return spellChecker;
    }

    public void callcheck(){
        System.out.println(this.getClass().getName());
        this.spellChecker.check();
    }
}

springconfig.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="spellChecker" class="spellChecker"></bean>
    <bean id="textEditor" class="textEditor">
        <property name="spellChecker">
            <bean id="spellChecker" class="spellChecker"></bean>
        </property>
    </bean>

</beans>

测试

Spring 注入集合

javacollection.java
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class javacollection {
    private List addlist;
    private Set  addset;
    private Map  addmap;
    private Properties addprop;

    public void setAddlist(List addlist) {
        this.addlist = addlist;
    }
    public List getAddlist() {
        return addlist;
    }

    public void setAddset(Set addset) {
        this.addset = addset;
    }
    public Set getAddset() {
        return addset;
    }

    public void setAddmap(Map addmap) {
        this.addmap = addmap;
    }

    public Map getAddmap() {
        return addmap;
    }

    public void setAddprop(Properties addprop) {
        this.addprop = addprop;
    }

    public Properties getAddprop() {
        return addprop;
    }
}

springconfig.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="javacollection" class="javacollection">
    <property name="addlist">
        <list>
            <value>india</value>
            <value>japan</value>
            <value>china</value>
            <value>pekke</value>
        </list>
    </property>
    <property name="addset">
        <set>
            <value>india</value>
            <value>japan</value>
            <value>china</value>
            <value>pekke</value>
        </set>
    </property>
    <property name="addmap">
        <map>
            <entry key="1" value="india"></entry>
            <entry key="2" value="japan"></entry>
            <entry key="3" value="china"></entry>
            <entry key="4" value="pekke"></entry>
        </map>
    </property>
    <property name="addprop">
        <props>
            <prop key="one">india</prop>
            <prop key="two">japan</prop>
            <prop key="three">china</prop>
            <prop key="four">pekke</prop>
        </props>
    </property>
</bean>

</beans>

Spring 注入引用

springconfig.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="javacollection" class="javacollection">
    <property name="addlist">
        <list>
            <ref bean=".."></ref>
            <ref bean=".."></ref>
            <value>china</value>
            <value>pekke</value>
        </list>
    </property>
    <property name="addset">
        <set>
            <ref bean=".."></ref>
            <ref bean=".."></ref>
            <value>china</value>
            <value>pekke</value>
        </set>
    </property>
    <property name="addmap">
        <map>
            <entry key="1" value-ref=".."></entry>
            <entry key="2" value-ref=".."></entry>
            <entry key="3" value="china"></entry>
            <entry key="4" value="pekke"></entry>
        </map>
    </property>
</bean>

</beans>

最后

感谢你看到这里,看完有什么的不懂的可以在评论区问我,觉得文章对你有帮助的话记得给我点个赞,每天都会分享java相关技术文章或行业资讯,欢迎大家关注和转发文章!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值