• Springioc的配置使用


    SpringFrameWork系列3


    Springioc的配置使用

    <?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加载顺序  当一个bean想让另一bean在它之前加载可以设置depends-on 注意是加载顺序 -->
    <!--    <bean class="bobo.User" id="user" depends-on="wife"/>-->
    <!--    <bean class="bobo.Wife" id="wife"/>-->
    <!---->
    
        <!--懒加载
        就不会在spring容器加载的时候 加载该bean
        而是在使用的时候才会加载该bean -->
    <!--    <bean class="bobo.Wife" lazy-init="true"/>-->
    
        <!--作用域scope
       singleton 默认值 同一个id始终只会创建一次bean
       prototype 多例(原型) 每一次使用都会创建一个bean -->
    <!--    <bean class="bobo.Person" id="person3" scope="prototype"/>-->
    
    <!--    使用静态工厂方式实例化bean-->
    <!--    <bean class="bobo.Person" id="person3" factory-method="createPersonFacotry"/>-->
    <!--    使用实例工厂方法实例化-->
    <!--    <bean class="bobo.PersonFacotry" id="personFacotry"/>-->
    <!--    <bean class="bobo.Person" id="person4"-->
    <!--        factory-bean="personFacotry"-->
    <!--        factory-method="createPersonFactory"-->
    <!--    ></bean>-->
    
        <!-- 自动注入
        byType 根据类型去自动匹配 当出现多个类型或者匹配到类型则会报错
        byName 根据set方法的名字去自动匹配
        constructor 根据构造器去匹配
            优先会根据参数名字去匹配,假如参数名字没有匹配到,会根据参数类型去匹配
            会根据构造函数的参数进行完整的匹配注入: 如果构造函数的参数Person(Wife wife3,User user)  ioc容器里面必须要有同时有wife和user
            名字没有匹配到会根据类型匹配   类型假如出现多个会注入失败但是不会报错
    
            当根据类型匹配到多个 可以使用 1.设置某个bean为主要bean primary="true"
             2.设置不需要自动注入的bean autowire-candidate="false" 忽略自动注入
    -->
        <bean class="bobo.Person" id="person3" autowire="byName"/>
        <bean class="bobo.Wife" id="wife3" autowire-candidate="false"/>
    
        <!--配置第三方bean -->
       <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
           <property name="username" value="${mysql.username}"></property>
           <property name="password" value="${mysql.password}"></property>
           <property name="url"  value="${mysql.url}"></property>
           <property name="driverClassName" value="${mysql.driverClassName}"></property>
       </bean>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    每日学习记录,如有错误,感谢指出!
    请添加图片描述

  • 相关阅读:
    更新至2022年ESG评级评分数据合集(含华证、盟浪、wind、彭博、润灵环球、商道融绿、和讯网、富时罗素数据)
    网页大作业代码自取【HTML+CSS制作美味糖果网站】
    USB插座外壳接地的处理和emi,esd考虑
    AI带你省钱旅游!精准预测民宿房源价格!
    C++——类和对象练习(日期类)
    视频剪辑高手的秘诀:如何从视频中提取封面,提高视频点击率
    计算机毕业设计Java宠物交易(源码+系统+mysql数据库+lw文档)
    大数据入门:HDFS API 常规操作
    网课题库接口API—小白专用
    细解“微服务”架构体系——SpringCloud Alibaba!
  • 原文地址:https://blog.csdn.net/weixin_54174102/article/details/126694486