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

2年前 (2022) 程序员胖胖胖虎阿
216 0 0

文章目录

  • 💨更多相关知识👇
    • 一、创建对象的三种方式
      • (1)无参构造方法创建对象
        • ⚡注意事项
      • (2)静态工厂方法创建对象
        • ⭐代码演示
      • (3)实例工厂方法创建对象
        • ⭐代码演示
    • 二、第三方资源配置管理
      • (1)测试Spring管理Druid连接池
        • ⭐代码演示图1
        • ⭐代码演示图2
      • (2)加载properties属性文件管理Druid连接池
        • ⭐加载指定properties文件演示图
        • ⭐完整代码演示图
        • ⭕当配置文件内容和系统属性造成冲突解决演示
        • ⭕加载多个properties文件演示
        • ⭕加载所有properties文件演示
        • ⭕加载properties文件标准格式 (常用)
        • ⭕从类路径或jar包中搜索并加载properties文件
  • 作者:KJ.JK

💨更多相关知识👇

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

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

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

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

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

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

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

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

💖JavaEE中的Stream流的常用方法

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


🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈 🌈
 
🍂个人博客首页: KJ.JK
 
欢迎大家点赞👍收藏💖评论💬关注🔒
 
💖源码获取 | 💻学习交流 | 🤝商务合作 | 💨私信作者
 
💨推荐一款实用的模拟面试、刷题练习算法的神器、适用于所有的程序猿👉点击开始免费刷题,跟着博主走上巅峰💪


一、创建对象的三种方式


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


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

*/

⚡注意事项


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


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


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


                "通过工厂的静态方法获取对象"
------------------------------------------------------------------------------------------------------------                
                  class:指定工厂类
                  
                  id:要获取的对象id
                  
                  factory-method:指定工厂类中静态方法名字                  

⭐代码演示

//工厂类
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>



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


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


           "通过工厂对象中的实例创建对象"
------------------------------------------------------------------------------------------------------------
               
               id: bean中的id
               factory-bean:实例工厂的id
               factory-method: 工厂中方法名

⭐代码演示

//工厂类
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>


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


二、第三方资源配置管理


(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

//测试类
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>


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


⭐代码演示图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>


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


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


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

⭐加载指定properties文件演示图


<?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"/>

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


⭐完整代码演示图

//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();

    }
}


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


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


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

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


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


⭕加载多个properties文件演示


                * <context:property-placeholder location="jdbc.properties,msg.properties"/>
                    
/*

location="jdbc.properties,msg.properties": 加载多个配置文件中间用,分割

*/

//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();

    }
}


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


⭕加载所有properties文件演示


                 * <context:property-placeholder location="*.properties"/>
                
/*

location="*.properties":
    加载所有的properties文件, 单元测试找编译后test-classes里面的配置,
    main方法运行找classes里面的配置(类路径)
*/

//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();

    }
}


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


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


              * <context:property-placeholder location="classpath:*.properties"/>

/*

 location="classpath:*.properties":   (和 location="*.properties"效果一样)
    加载所有的properties文件,
    单元测试找编译后test-classes里面的配置,
    main方法运行找classes里面的配置(类路径)
*/

//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="classpath:*.properties":   (和 location="*.properties"效果一样)
    加载所有的properties文件,
    单元测试找编译后test-classes里面的配置,
    main方法运行找classes里面的配置(类路径)


       -->
       
<context:property-placeholder   location="classpath:*.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>

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


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


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

作者:KJ.JK

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

相关文章

暂无评论

暂无评论...