当前位置: 首页 > news >正文

开发游戏需要多少资金china东莞seo

开发游戏需要多少资金,china东莞seo,苍南规划建设局网站,福建宏盛建设集团有限公司网站Spring框架可以通过Spring的配置文件完成两个数据源druid和C3P0的配置(Spring:IOC/DI配置管理第三方bean),但是其中包含了一些问题,我们来分析下: 这两个数据源中都使用到了一些固定的常量如数据库连接四要素&#xf…

Spring框架可以通过Spring的配置文件完成两个数据源druidC3P0的配置(Spring:IOC/DI配置管理第三方bean),但是其中包含了一些问题,我们来分析下:

  • 这两个数据源中都使用到了一些固定的常量如数据库连接四要素,把这些值写在Spring的配置文件中不利于后期维护
  • 需要将这些值提取到一个外部的properties配置文件中

Spring框架如何从配置文件中读取属性值来配置呢?

实现思路

需求:将数据库连接四要素提取到properties配置文件,spring来加载配置信息并使用这些信息来完成属性注入。

1.在resources下创建一个jdbc.properties(文件的名称可以任意)

2.将数据库连接四要素配置到配置文件中

3.在Spring的配置文件中加载properties文件

4.使用加载到的值实现属性注入

步骤1:准备properties配置文件

resources下创建一个jdbc.properties文件,并添加对应的属性键值对

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db
jdbc.username=root
jdbc.password=root
步骤2:开启context命名空间

在applicationContext.xml中开context命名空间

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
</beans>
步骤3:加载properties配置文件

在配置文件中使用context命名空间下的标签来加载properties配置文件

<context:property-placeholder location="jdbc.properties"/>
步骤4:完成属性注入

使用${key}来读取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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd" >
<!--    1.开启context命名空间-->
<!--    2.使用context空间加载properties文件--><!--方式1    <context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>-->
<!--方式2    <context:property-placeholder location="jdbc.properties,jdbc2.properties" system-properties-mode="NEVER"/>-->
<!--    classpath:*.properties  :   设置加载当前工程类路径中的所有properties文件-->
<!--    system-properties-mode属性:是否加载系统属性--><!--方式3    <context:property-placeholder location="*.properties" system-properties-mode="NEVER"/>--><!--方式4 classpath*:*.properties  :  设置加载当前工程类路径和当前工程所依赖的所有jar包中的所有properties文件--><context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/><!--    3.使用属性占位符${}读取properties文件中的属性-->
<!--    说明:idea自动识别${}加载的属性值,需要手工点击才可以查阅原始书写格式--><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><bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"><property name="name" value="${jdbc.url}"/></bean></beans>

说明:

  • 方式一:可以实现,如果配置文件多的话,每个都需要配置
  • 方式二:*.properties代表所有以properties结尾的文件都会被加载,可以解决方式一的问题,但是不标准
  • 方式三:标准的写法,classpath:代表的是从根路径下开始查找,但是只能查询当前项目的根路径
  • 方式四:不仅可以加载当前项目还可以加载当前项目所依赖的所有项目的根路径下的properties配置文件
  • <context:property-placeholder/>标签会加载系统的环境变量,而且环境变量的值会被优先加载,因此,增加system-properties-mode:设置为NEVER,表示不加载系统属性

至此,读取外部properties配置文件中的内容就已经完成,进行测试:

public class App {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext4.xml");DataSource dataSource = (DataSource) ctx.getBean("dataSource");System.out.println(dataSource);}
}

运行结果:
在这里插入图片描述

加载properties文件小结

本节主要讲解的是properties配置文件的加载,需要掌握的内容有:

  • 如何开启context命名空间

    在这里插入图片描述

  • 如何加载properties配置文件

    <context:property-placeholder location="" system-properties-mode="NEVER"/>
    
  • 如何在applicationContext.xml引入properties配置文件中的值

    ${key}
    

[说明]:内容主要来源黑马程序员网上资源学习

http://www.yayakq.cn/news/272841/

相关文章:

  • asp网站开发移动端深圳市龙岗区住房和建设局
  • 基本的网站建设步骤wordpress搜索标题
  • 安阳哪个公司做网站好成都必去的10个景点
  • 河北住房与城乡建设厅网站装修公司网站模板
  • 信息平台 网站的建设wordpress 删除 前缀
  • 网站推广计划书范文500字凡科商城是什么
  • 医疗网站建设渠道重庆的做网站公司
  • 做gif有什么网站电子商务网站开发技术和工具有哪些
  • 学做网站和推广要多久网站服务器用来做啥
  • 成都网站建设四川冠辰科技商务网站建设毕业设计
  • 重庆网站查询学做电商的网站
  • 网站链接推广怎么赚钱重庆市住房和城乡建设岗位证书查询
  • 做文案选图片素材的网站成都网站建设公司思乐科技
  • wordpress三栏博客主题百度seo如何做
  • 网站建设哪公司好宁波seo外包联系方式
  • 对网站开发课程的建议网页制作软件山水
  • 网站的构造wordpress 挣钱
  • 线在科技成都网站推广公司定服装网站建设
  • 做网站运营需要学什么条件网上注册公司申请流程
  • 临沂网站制作费用国内禁用的国外网站
  • 一般网站栏目结构好的做网站架构的书
  • .net 微信网站开发word可以制作网页吗
  • 有经验的大良网站建设做网站服务器 自己电脑还是租
  • 数据库网页制作教程多个网站优化怎么做
  • 贵阳市住房城乡建设局八大员网站做网站需要固定ip
  • 眉山市规划建设局网站工业设计包括哪些方面
  • 小说网站建设方案书ppt在线网站开发培训
  • 辽宁建设厅的证到底在哪个网站查手机网站 域名
  • 企业自己怎么制作网站首页wordpress负载
  • 泰兴市城乡住房建设局网站汕头澄海网站建设