• Spring中的创建对象的三种方式、第三方资源配置管理详细描述及使用(XML版完结篇)



    💨更多相关知识👇

    💖Java版本新零售小程序saas商城全开源系统

    💖Spring中的bean的配置、作用范围、生命周期详细描述及使用(XML版上篇)

    💖Spring中的依赖注入、setter与构造器注入、自动装配与集合注入详细描述及使用(XML版中篇)

    💖异常处理与解决方案详解上篇

    💖异常处理与解决方案详解下篇

    💖Math类与System类的常用方法使用

    💖JavaEE中的静态方法定义、方法重载要求、return作用详解

    💖List接口的常用方法,精华总结

    💖JavaEE中的Stream流的常用方法

    💖JavaEE中的Stream流知识点使用,精华总结,一文直接上手


    🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈
     
    🍂个人博客首页: KJ.JK
     
    欢迎大家点赞👍收藏💖评论💬关注🔒
     
    💖源码获取 | 💻学习交流 | 🤝商务合作 | 💨私信作者
     
    🍂项目实战gitee开源地址: weiit/weiit-saas
     
    🍂项目实战github开源地址: weiit/weiit-saas
     
    ⭐希望大家可以在gitee和github点个star呀⭐


    一、创建对象的三种方式


    (1)无参构造方法创建对象


              * 例子: <bean class="com.itheima.dao.impl.BookDaoImpl" id="bookDao"></bean>
    /*
         注意:
            一定要有无参的构造方法,如果创建了有参的构造方法,会导致无参的构造方法失效,出现异常
            出现错误: No default constructor found
    
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    ⚡注意事项


    请添加图片描述


    请添加图片描述


    (2)静态工厂方法创建对象


                    "通过工厂的静态方法获取对象"
    ------------------------------------------------------------------------------------------------------------                
                      class:指定工厂类
                      
                      id:要获取的对象id
                      
                      factory-method:指定工厂类中静态方法名字                  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    ⭐代码演示

    //工厂类
    public class StaticBookFactory {
        // 静态工厂方法
        public static BookDao getBean() {
    
            //返回我们想要的对象
            return new BookDaoImpl();
        }
    }
    
    ------------------------------------------------------------------------------------------------------------
    
    //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">
    
    
        <!--静态工厂创建对象放到IoC容器中
    本质是调用StaticBookFactory类getBean方法,把它的返回值放到容器中
    
                   class:指定工厂类
                   id:要获取的对象id
                   factory-method:指定工厂类中静态方法名字-->
        <bean class="com.itheima.util.StaticBookFactory" factory-method="getBean" id="bookDao2"/>
    
    
    </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

    请添加图片描述


    (3)实例工厂方法创建对象


               "通过工厂对象中的实例创建对象"
    ------------------------------------------------------------------------------------------------------------
                   
                   id: bean中的id
                   factory-bean:实例工厂的id
                   factory-method: 工厂中方法名
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    ⭐代码演示

    //工厂类
    public class InstanceBookFactory {
        // 实例工厂方法
        public BookDao getBean() {
    
            //返回我们想要的对象
            return new BookDaoImpl();
        }
    }
    
    ------------------------------------------------------------------------------------------------------------
    
    //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">
    
    
    
        <!--实例工厂创建对象放到IoC容器中-->
        <!--1.创建实例工厂 约等于 instanceBookFactory = new InstanceBookFactory()-->
        <bean class="com.itheima.util.InstanceBookFactory" id="instanceBookFactory"/>
        <!--2.调用方法,把方法的返回值放到容器中
            调用instanceBookFactory对象的getBean方法,把返回getBean方法的返回值放到容器中
        -->
        <bean factory-bean="instanceBookFactory" factory-method="getBean" id="bookDao3"/>
    
    </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

    请添加图片描述


    二、第三方资源配置管理


    (1)测试Spring管理Druid连接池


                               1.在pom.xml导入druid连接池坐标
                               
                                  <dependency>
                                      <groupId>com.alibaba</groupId>
                                      <artifactId>druid</artifactId>
                                      <version>1.2.8</version>
                                  </dependency>
                     
                                   
                               2.配置数据源对象作为spring管理的bean
                                 
                                 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
                                    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
                                    <property name="url" value="jdbc:mysql://127.0.0.1:3306/spring_db"/>
                                    <property name="username" value="root"/>
                                    <property name="password" value="123456"/>
                                 </bean>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    ⭐代码演示图1

    //测试类
    import com.alibaba.druid.pool.DruidDataSource;
    import org.junit.Test;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    
    
    // 测试Spring管理Druid连接池
    public class DataSourceTest {
        @Test
        public void testDataSource() throws SQLException {
            // 纯Java代码创建Druid连接池, 我们的目标是在Spring配置文件中配置完成
             DruidDataSource dataSource = new DruidDataSource();
             dataSource.setDriverClassName("com.mysql.jdbc.Driver");
             dataSource.setUrl("jdbc:mysql://localhost:3306/spring_db?useSSL=false");
             dataSource.setUsername("root");
             dataSource.setPassword("123456");
    
        }
    }
    
    ------------------------------------------------------------------------------------------------------------
    
    //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:context="http://www.springframework.org/schema/context"
    
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
    ">
    
    
        <!--创建DruidDataSource放到IoC容器中-->
        <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
    
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/spring_db?useSSL=false"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
        </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

    请添加图片描述


    ⭐代码演示图2

    import com.alibaba.druid.pool.DruidDataSource;
    import org.junit.Test;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    
    
    // 测试Spring管理Druid连接池
    public class DataSourceTest {
        @Test
        public void testDataSource() throws SQLException {
            // 纯Java代码创建Druid连接池, 我们的目标是在Spring配置文件中配置完成
    //         DruidDataSource dataSource = new DruidDataSource();
    //         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    //         dataSource.setUrl("jdbc:mysql://localhost:3306/spring_db?useSSL=false");
    //         dataSource.setUsername("root");
    //         dataSource.setPassword("123456");
    
    
    
            // 1.根据配置文件创建IoC容器
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-DS.xml");
    
            // 2.从IoC容器中获取对象
            DataSource dataSource = (DataSource) context.getBean("dataSource");
    
            // 3.使用对象
            Connection conn = dataSource.getConnection();
            System.out.println("conn = " + conn);
    
            // 4.关闭容器
            context.close();
    
        }
    }
    
    ------------------------------------------------------------------------------------------------------------
    
    //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:context="http://www.springframework.org/schema/context"
    
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
    ">
    
    
        <!--创建DruidDataSource放到IoC容器中-->
        <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
    
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://localhost:3306/spring_db?useSSL=false"/>
            <property name="username" value="root"/>
            <property name="password" value="123456"/>
        </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
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    请添加图片描述


    (2)加载properties属性文件管理Druid连接池


                          1.使用context命名空间,加载指定properties文件
                          
                             <context:property-placeholder location="jdbc.properties"/>
                             
                          2.使用${}读取加载的属性值
                          
                              <property name="username" value="${jdbc.username}"/>
       
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    ⭐加载指定properties文件演示图


    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
           xmlns:context="http://www.springframework.org/schema/context"
    
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
    
           http://www.springframework.org/schema/context
    
           https://www.springframework.org/schema/context/spring-context.xsd">
    
    
        
    <context:property-placeholder location="jdbc.properties"/>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    请添加图片描述


    ⭐完整代码演示图

    //properties文件
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db?useSSL=false
    jdbc.username=root
    jdbc.password=123456
    
    ------------------------------------------------------------------------------------------------------------
    
    //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:context="http://www.springframework.org/schema/context"
    
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
    
           http://www.springframework.org/schema/context
    
           https://www.springframework.org/schema/context/spring-context.xsd">
    
    
        <!--property-placeholder标签:引入外部的properties文件
          location属性: 指定外部properties文件的位置
    
           -->
           
    <context:property-placeholder location="jdbc.properties"/>
    
    
      <!--创建DruidDataSource放到IoC容器中-->
        <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
    
    
    </beans>
    
    ------------------------------------------------------------------------------------------------------------
    
    //测试类
    import com.alibaba.druid.pool.DruidDataSource;
    import org.junit.Test;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    
    // 测试Spring管理Druid连接池
    public class DataSourceTest {
        @Test
        public void testDataSource() throws SQLException {
    
    
            // 1.根据配置文件创建IoC容器
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-DS.xml");
    
            // 2.从IoC容器中获取对象
            DataSource dataSource = (DataSource) context.getBean("dataSource");
    
            // 3.使用对象
            Connection conn = dataSource.getConnection();
            System.out.println("conn = " + conn);
    
            // 4.关闭容器
            context.close();
    
        }
    }
    
    
    • 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
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    请添加图片描述


    ⭕当配置文件内容和系统属性造成冲突解决演示


          通过设置以下代码即可:
             
                 * system-properties-mode="NEVER"  : 不加载系统的属性
                 
                如:
                   <context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    请添加图片描述


    请添加图片描述


    ⭕加载多个properties文件演示


                    * <context:property-placeholder location="jdbc.properties,msg.properties"/>
                        
    /*
    
    location="jdbc.properties,msg.properties": 加载多个配置文件中间用,分割
    
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    //properties文件1
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db?useSSL=false
    jdbc.username=root
    jdbc.password=123456
    
    ------------------------------------------------------------------------------------------------------------
    
    //properties文件2
    msg.driver=com.mysql.jdbc.Driver
    msg.url=jdbc:mysql://127.0.0.1:3306/spring_db?useSSL=false
    msg.username=root
    msg.password=123456
    
    ------------------------------------------------------------------------------------------------------------
    
    //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:context="http://www.springframework.org/schema/context"
    
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
    
           http://www.springframework.org/schema/context
    
           https://www.springframework.org/schema/context/spring-context.xsd">
    
    
        <!--property-placeholder标签:引入外部的properties文件
          location属性: 指定外部properties文件的位置
    
    
    location="jdbc.properties,msg.properties": 加载多个配置文件中间用,分割
    
           -->
           
    <context:property-placeholder location="jdbc.properties,msg.properties"/>
    
    
      <!--创建DruidDataSource放到IoC容器中-->
        <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
    
    
    </beans>
    
    ------------------------------------------------------------------------------------------------------------
    
    //测试类
    import com.alibaba.druid.pool.DruidDataSource;
    import org.junit.Test;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    
    // 测试Spring管理Druid连接池
    public class DataSourceTest {
        @Test
        public void testDataSource() throws SQLException {
    
    
            // 1.根据配置文件创建IoC容器
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-DS.xml");
    
            // 2.从IoC容器中获取对象
            DataSource dataSource = (DataSource) context.getBean("dataSource");
    
            // 3.使用对象
            Connection conn = dataSource.getConnection();
            System.out.println("conn = " + conn);
    
            // 4.关闭容器
            context.close();
    
        }
    }
    
    
    • 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
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87

    请添加图片描述


    ⭕加载所有properties文件演示


                     * <context:property-placeholder location="*.properties"/>
                    
    /*
    
    location="*.properties":
        加载所有的properties文件, 单元测试找编译后test-classes里面的配置,
        main方法运行找classes里面的配置(类路径)
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    //properties文件1
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db?useSSL=false
    jdbc.username=root
    jdbc.password=123456
    
    ------------------------------------------------------------------------------------------------------------
    
    //properties文件2
    msg.driver=com.mysql.jdbc.Driver
    msg.url=jdbc:mysql://127.0.0.1:3306/spring_db?useSSL=false
    msg.username=root
    msg.password=123456
    
    ------------------------------------------------------------------------------------------------------------
    
    //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:context="http://www.springframework.org/schema/context"
    
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
    
           http://www.springframework.org/schema/context
    
           https://www.springframework.org/schema/context/spring-context.xsd">
    
    
        <!--property-placeholder标签:引入外部的properties文件
          location属性: 指定外部properties文件的位置
    
    
    location="*.properties":
        加载所有的properties文件, 单元测试找编译后test-classes里面的配置,
        main方法运行找classes里面的配置(类路径)
    
           -->
    <context:property-placeholder location="*.properties"/>
    
    
      <!--创建DruidDataSource放到IoC容器中-->
        <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
    
    
    </beans>
    
    ------------------------------------------------------------------------------------------------------------
    
    //测试类
    import com.alibaba.druid.pool.DruidDataSource;
    import org.junit.Test;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    
    // 测试Spring管理Druid连接池
    public class DataSourceTest {
        @Test
        public void testDataSource() throws SQLException {
    
    
            // 1.根据配置文件创建IoC容器
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-DS.xml");
    
            // 2.从IoC容器中获取对象
            DataSource dataSource = (DataSource) context.getBean("dataSource");
    
            // 3.使用对象
            Connection conn = dataSource.getConnection();
            System.out.println("conn = " + conn);
    
            // 4.关闭容器
            context.close();
    
        }
    }
    
    
    • 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
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88

    请添加图片描述


    ⭕加载properties文件标准格式 (常用)


                  * <context:property-placeholder location="classpath:*.properties"/>
    
    /*
    
     location="classpath:*.properties":   (和 location="*.properties"效果一样)
        加载所有的properties文件,
        单元测试找编译后test-classes里面的配置,
        main方法运行找classes里面的配置(类路径)
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    //xml类
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
           xmlns:context="http://www.springframework.org/schema/context"
    
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
    
           http://www.springframework.org/schema/context
    
           https://www.springframework.org/schema/context/spring-context.xsd">
    
    
        
           
    <context:property-placeholder   location="classpath:*.properties"/>
    
    
      
        <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        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

    请添加图片描述


    ⭕从类路径或jar包中搜索并加载properties文件


                 * <context:property-placeholder location="classpath*:*.properties"/>
                 
    /*
       location="classpath*:*.properties": 会去依赖的jar中找properties文件
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5

    💨项目综合实战文章👇

    💖基于Spring+SpringMVC+Mybatis在线weiit-saas微信小程序电商系统

    🍂项目实战gitee开源地址: weiit/weiit-saas
     
    🍂项目实战github开源地址: weiit/weiit-saas
     
    ⭐希望大家可以在gitee和github点个star呀⭐


    作者:KJ.JK

    文章对你有所帮助的话,欢迎给个赞或者 star,你的支持是对作者最大的鼓励,不足之处可以在评论区多多指正,交流学习

  • 相关阅读:
    代码随想录 | Day 51 - LeetCode 309.最佳买卖股票时机含冷冻期、LeetCode 714.买卖股票的最佳时机含手续费
    HIFI测序揭示拟南芥MSH1参与介导的细胞器基因组重组与变异积累规律
    日志(四)
    GEE数据融合——Landsat (collection 2,level 2 )4、5、7、8、9长时间序列影像数据融合和视频导出分析
    智能营销机器人具备哪些能力?
    力扣(LeetCode)808. 分汤(C++)
    如何在手机或平板上编写代码?
    vue2项目从0搭建(二):配置代理,登录功能和菜单权限
    VM关闭虚拟机之后,连接不上前一天设置的静态ip
    SpringBoot二手车管理系统
  • 原文地址:https://blog.csdn.net/m0_47384542/article/details/126197853