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. 运行结果
  • 相关阅读:
    mysql索引创建&查看&删除
    linq中不能准确按拼音排序
    Vue的组件的注册,复用以及组件中template多行处理
    Vue的简单使用和部分常用指令
    SpringBootMVC+thymeleaf模板初探
    记一次遗留代码的重构改造:数十万行国家标准坐标文件分析方法的改造与提速
    springBoot 集成Mysql数据库
    C#和Java的对比
    架构学习提炼笔记(三):高性能架构设计技巧——读写分离
    架构学习提炼笔记(二):架构设计的流程是什么?
  • 原文地址:https://www.cnblogs.com/KingTL/p/13019006.html
Copyright © 2011-2022 走看看