Spring读取外部资源

实际开发中,数据库的资源一般会单独保存起来。一般会保存到后缀为properties的文件中,方便维护和修改,如果Spring加载资源,就需要在spring.xml中读取properties中的资源

xxx.properties

user=root
password=root
url = jdbc:mysql://localhost:3306/library
driverName=com.mysql.cj.jdbc.Driver

spring-xx.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">
<!-- 导入外部资源-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--SpringEL-->
<bean id="dataSource" class="com.southwind.entity.DataSouse ">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="url" value="${url}"></property>
<property name="driverName" value="${driverName}"></property>
</bean>
</beans>

DataSource:

package com.southwind.entity;

import lombok.Data;

@Data
public class DataSouse {
private String user;
private String password;
private String url;
private String driverName;
}

测试类:

package com.southwind.test;

import com.southwind.entity.DataSouse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test4 {
public static void main(String[] args) {
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("spring-property.xml");
DataSouse dataSouse = (DataSouse)applicationContext.getBean("dataSource");
System.out.println(dataSouse);
}
}

Spring p 命名空间

p 命名空间简化Bean的配置

Spring-p.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.southwind.entity.Student" p:id="1" p:name="张三"></bean>
</beans>

测试类:

package com.southwind.test;

import com.southwind.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test5 {
public static void main(String[] args) {
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("spring-p.xml");
Student student = (Student)applicationContext.getBean("student");
System.out.println(student);
}
}

如果和级联一样:也是p:age-ref=""写类

同时也要引入:

xmlns:p="http://www.springframework.org/schema/p"

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

Spring(Spring的读取外部资源- p 命名空间)的更多相关文章

  1. Unity3D移动平台动态读取外部文件全解析

    前言: 一直有个想法,就是把工作中遇到的坑通过自己的深挖,总结成一套相同问题的解决方案供各位同行拍砖探讨.眼瞅着2015年第一个工作日就要来到了,小匹夫也休息的差不多了,寻思着也该写点东西活动活动大脑 ...

  2. (转)Unity3D移动平台动态读取外部文件全解析

    Unity3D移动平台动态读取外部文件全解析 c#语言规范 阅读目录 前言: 假如我想在editor里动态读取文件 移动平台的资源路径问题 移动平台读取外部文件的方法 补充: 回到目录 前言: 一直有 ...

  3. 慕容小匹夫 Unity3D移动平台动态读取外部文件全解析

    Unity3D移动平台动态读取外部文件全解析   c#语言规范 阅读目录 前言: 假如我想在editor里动态读取文件 移动平台的资源路径问题 移动平台读取外部文件的方法 补充: 回到目录 前言: 一 ...

  4. 细说Unity3D(一)——移动平台动态读取外部文件全解析

    前言: 一直有个想法,就是把工作中遇到的坑通过自己的深挖总结成一套相同问题的解决方案供各位同行拍砖探讨.眼瞅着2015年第一个工作日就要来到了,小匹夫也休息的差不多了,寻思着也该写点东西活动活动大脑和 ...

  5. SpringBoot学习(三)-->Spring的Java配置方式之读取外部的资源配置文件并配置数据库连接池

    三.读取外部的资源配置文件并配置数据库连接池 1.读取外部的资源配置文件 通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值,具体用法: @Configuration ...

  6. Spring读取外部的资源配置文件—@PropertySource和@Value实现资源文件配置

    通过@PropertySource可以指定读取的配置文件,通过@Value注解获取值: @PropertySource注解主要是让Spring的Environment接口读取属性配置文件用的,标识在@ ...

  7. spring(读取外部数据库配置信息、基于注解管理bean、DI)

    ###解析外部配置文件在resources文件夹下,新建db.properties(和数据库连接相关的信息) driverClassName=com.mysql.jdbc.Driverurl=jdbc ...

  8. Spring导入外部资源

    创建一个数据库连接的 properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssmbuil ...

  9. [Spring入门学习笔记][静态资源]

    遗留问题 在上一节课的作业中,我们一定遇到了一点问题——虽然将页面内容正确的返回给了浏览器,但是浏览器显示的样式却是不正确的,这是因为在HTML的\标签中我们这样引入了CSS资源: <link ...

  10. Spring Boot实战:静态资源处理

    前两章我们分享了Spring boot对Restful 的支持,不过Restful的接口通常仅仅返回数据.而做web开发的时候,我们往往会有很多静态资源,如html.图片.css等.那如何向前端返回静 ...

随机推荐

  1. 核磁共振成像学习笔记——从FID信号到K空间

    在理想磁场环境下(没有不存在场不均匀性),对于一个没有梯度场的方块. 此时,RF pulse的两路正交信号(相位差为90°)对此方块进行激发,然后收取信号,我们可以得到由此方块产生的FID信号. 设此 ...

  2. 【element】中el-row如何使内容垂直居中

    查阅官方文档,只需要在el-row中设置属性align为middle即可

  3. IDEA git配置

    必备:安装Idea \ git配置git坏境:在环境变量中添加git安装包bin目录即可 1.去git官网申请一个账号 https://github.com/ 创建一个新的项目 2.在快速启动栏或者g ...

  4. DTSE Tech Talk 第13期:Serverless凭什么被誉为未来云计算范式?

    摘要:在未来,云上交付模式会逐步从Serverful为主转向Serverless为主. 本文分享自华为云社区<DTSE Tech Talk 第13期:Serverless凭什么被誉为未来云计算范 ...

  5. 【PostgreSQL】PG通过SQL语句读取二进制bytea类型并进行二进制和十六进制转换

    1.将二进制编码为十六进制 select encode("AUUID_0",'hex'),"AUUID_0" from wxf_test."ABANK ...

  6. 【每日一题】【双指针/栈/reverse】2022年2月19日-判断是否为回文字符串

    给定一个长度为 n 的字符串,请编写一个函数判断该字符串是否回文.如果是回文请返回true,否则返回false.   字符串回文指该字符串正序与其逆序逐字符一致.   数据范围:0 < n \l ...

  7. 【Flume】概述及组成、入门案例、进阶(事务、拓扑结构)、不同拓扑案例、自定义、数据流监控Ganglia

    一.概述 1.定义 日志采集.聚合.传输的系统,基于流式结构 即:读取本地磁盘数据,写入HDFS或kafka 2.架构 Agent:JVM进程,以事件形式将数据送到目的地. Agent由三部分组成:S ...

  8. 想早点下班?试试Aorm库吧,更方便的进行Go数据库操作

    使用go进行项目开发,大多数人会使用gorm,但是gorm有一些缺点,我无法接受.于是开发出了aorm,目前能有满足日常开发需求,并且完善了使用文档,希望能够帮助到大家. Aorm Golang操作数 ...

  9. 动态SQL遇到的问题

    看图 查不出来任何数据 因为判断有问题 修改方法如下:

  10. Golang Gorm 封装 分页查询 Where Order 查询

    说说为什么写Gorm,因为公司新项目需要,研究了下Go下的gorm.对于一个项目首先考虑的问题,就是封装一些常用的工具方法,例如多参数查询 where or Like 还有order by Limit ...