zoukankan      html  css  js  c++  java
  • Spring导入外部资源

    1. 创建一个数据库连接的 properties
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?serverTimezone=Asia/Shanghai&useSSL=true&useUnicode=true&characterEncoding=utf8
    jdbc.username=root
    jdbc.password=admin888
    
    1. 创建ApplicationContext.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:con="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">
    
    <!--    导入外部资源-->
    <!--    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">-->
    <!--        <property name="location" value="classpath:datasourse.properties"/>-->
    <!--    </bean>-->
    
        <con:property-placeholder location="classpath:datasourse.properties"/>
    
        <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
            <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>
    

    这里导入资源有两种方式,选择一种即可

    • 第一种:已经过时了!
    <!--    导入外部资源-->
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="classpath:datasourse.properties"/>
        </bean>
    
    
    • 第二种:Spring推荐使用的
     <con:property-placeholder location="classpath:datasourse.properties"/>
    
    1. 创建一个测试类
    import com.alibaba.druid.pool.DruidDataSource;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class MyTest {
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    
            DruidDataSource datasource = context.getBean("datasource", DruidDataSource.class);
            System.out.println(datasource);
    
    
        }
    }
    
    
    1. 运行结果
  • 相关阅读:
    指针和引用的区别
    c++空指针 和 野指针
    strcpy源码实现方式
    函数的分文件编写
    哈夫曼编码实现
    错误:The selected wizard could not be started Plug-in com.genuitec.eclipse.j2ee.ui was unable to load class com.genuitec.eclipse.j2ee.ui.wizard.WebProjectWizard
    sql server,mysql 和navicat for mysql的区别
    MySQL 5.7
    sql server 2017
    Download
  • 原文地址:https://www.cnblogs.com/KingTL/p/13019006.html
Copyright © 2011-2022 走看看