Spring入门系列 小白跟着一起学 (5)

文章介绍了Spring框架中的自动装配特性,包括byType和byName两种方式,以及如何通过配置文件实现集合如数组、列表、集合、映射和Properties的注入。示例代码展示了如何创建和配置Bean,以及如何在主程序中获取和使用这些Bean。文章强调了自动装配在减少耦合和简化开发中的作用。
摘要由CSDN通过智能技术生成

学前要求: 已学完本系列前四个

自动装配

Spring的自动装配是一种IoC(Inversion of Control,控制反转)的实现方式,可以大大简化应用程序开发和维护的工作,同时减少了耦合度和依赖性。通过在配置文件中声明Bean的信息,Spring容器可以自动检测并装配所需的对象

先讲autowire=“byType”
新建文件TestDemo13

public class TestDemo13 implements Test13Demo {
    private TestDemo14 testDemo14;
    @Override
    public void say() {
        System.out.println("i am nothing to say!");
    }
    public void setTestDemo14(TestDemo14 testDemo14) { // 这个会使用到 毕竟不让访问的话就出问题了
        this.testDemo14 = testDemo14;
    }
    public void save() {
        System.out.println("TestDemo13 save!");
        testDemo14.save();
    }
}

再建Test13Demo

public interface Test13Demo {
    void say();
}

再建TestDemo14

public class TestDemo14 {
    public void save() {
        System.out.println("TestDemo14 save!");
    }
}

配置文件中applicationContext.xml

    <!--  自动装配  -->
    <bean id="testDemo14" class="com.javala.service.TestDemo14"/>

    <bean id="testDemo13" class="com.javala.service.TestDemo13" autowire="byType"/>
    <!-- 下面为原本的写法   -->
<!--    <bean id="testDemo13" class="com.javala.service.TestDemo13">-->
<!--        <property name="testDemo14" ref="testDemo14"/>-->
<!--    </bean>-->

最后运行部分

public class App9 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        TestDemo13 testDemo13 = (TestDemo13) ctx.getBean("testDemo13");
        testDemo13.save();
    }
}

autowire=“byType” 是一种 Spring 自动装配的方式,表示 Spring 容器会根据属性的数据类型自动在容器中匹配同类对象,并将其注入到属性中。

autowire=“byName”

autowire=“byName” 是一种 Spring 自动装配的方式,表示 Spring 容器会根据属性的名称自动在容器中匹配同名对象,并将其注入到属性中。

现在尝试将

<bean id="testDemo13" class="com.javala.service.TestDemo13" autowire="byType"/>

更改为

<bean id="testDemo13" class="com.javala.service.TestDemo13" autowire="byName"/>

其余不需要更改,直接运行即可,主要此时的
在这里插入图片描述
id属性匹配的是
在这里插入图片描述
但是实际使用中还是使用:autowire=“byType” 毕竟不能让一个随意可变名称给卡住不是!

集和注入

这里就要和我们的系列(1)完美回味了
新建一个TestDemo15的java源文件

public class TestDemo15 {
    private int[] array;
    private List<String> list;
    // Set是 不重复 元素唯一 无序的 集和
    private Set<String> set;
    private Map<String, String> map;
    // Properties表示一个持久的属性集 是唯一和IO流结合的集和
    private Properties properties;

    public void setArray(int[] array) {
        this.array = array;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void say() {
        System.out.println("array:"+ Arrays.toString(array));
        System.out.println("list:"+list);
        System.out.println("set:"+set);
        System.out.println("map:"+map);
        System.out.println("properties:"+properties);
    }
}

再在配置文件中applicationContext.xml中

<!--  集和注入  -->
    <bean id="testDemo15" class="com.javala.dao.TestDemo15">
        <property name="array">
            <array>
                <value>100</value>
                <value>200</value>
                <value>300</value>
                <!--       引用注入       -->
                <!-- <ref id="beanId"/> -->
            </array>
        </property>

        <property name="list">
            <list>
                <value>周勋</value>
                <value>王蕾</value>
                <value>主坝接</value>
            </list>
        </property>

        <property name="set">
            <set>
                <value>流的话</value>
                <value>州新城</value>
                <value>王德海</value>
            </set>
        </property>

        <property name="map">
            <map>
                <entry key="中国" value="北京"/>
                <entry key="美国" value="纽约"/>
                <entry key="英国" value="伦敦"/>
            </map>
        </property>

        <property name="properties">
            <props>
                <prop key="中国">北京</prop>
                <prop key="美国">纽约</prop>
                <prop key="英国">伦敦</prop>
            </props>
        </property>
    </bean>

最后就是案例运行文件

public class App10 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        TestDemo15 testDemo15 = (TestDemo15) ctx.getBean("testDemo15");
        testDemo15.say();
    }
}

总结

那么学到这我们就已经掌握了自动装配的两种方式和初步理解集和注入

我是哈利巴多先生,如果觉得不错,还望多多鼓励(文章不定时更新)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值