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. 树莓派(香橙派)通过.NET IoT 操作SPI编写屏幕驱动 顺手做个四足机器人(一)

    摘要 这片文章主要是记录自己的整活过程,涉及到的技术包括.NET IoT, .NET Web, .NET MAUI,框架采用的也是最新的.NET 7. 本人是用的树莓派Zero 2 W(ubuntu- ...

  2. [排序算法] 树形选择排序 (C++)

    树形选择排序解释 树形选择排序 又称为锦标赛排序,其实理解起来很简单. 数组的 n 个元素就好像在进行锦标赛一样,每一轮小比赛每两个一组决出胜负(比较谁更小). 再将每一轮的胜者每两个一组进行小比赛, ...

  3. js判断数组中是否有重复数据

    var arr=[1,3,5,7,9,9,10,10,11,12,34,3,6,92,1]; var tempbool = false; //默认无重复 for (let index = 0; ind ...

  4. DevExpress中GridControl控件焦点改变时触发事件

    FocusedRowObjectChanged 事件.可以在焦点改变一行的时候触发对应的事件. 做一个记录 大家如果有问题可以 Console.WriteLine("加群"+&qu ...

  5. FP6397S5 高效、高频同步DC-DC降压变频器IC

    FP6397是一种高效.高频同步DC-DC降压变频器.100%占空比功能提供了低退出操作,延长了便携式系统的电池寿命. 内部同步开关提高了效率,并消除了对外部肖特基二极管的需要.在停机模式下,输入电源 ...

  6. ArcObjects SDK开发 011 RasterLayer

    1.RasterLayer的结构 图层的话,除了FeatureLayer外,用的最多的就是RasterLayer了.较FeatureLayer而言,RasterLayer比较简单,这点可以从栅格图层的 ...

  7. Jmeter 之吞吐量控制器

    作用: 吞吐量控制器可用来模拟混合场景的压测业务,即一部分用户执行场景A,一部分用户执行场景B 字段说明: Total Excutions:执行请求总数 Percent Excutions:执行线程数 ...

  8. “喜提”一个P2级故障—CMSGC太频繁,你知道这是什么鬼?

    大家好,我是陶朱公Boy. 背景 今天跟大家分享一个前几天在线上碰到的一个GC故障- "CMSGC太频繁". 不知道大家看到这条告警内容后,是什么感触?我当时是一脸懵逼的,一万个为 ...

  9. 推荐给Amy的书单

    目录 皮囊 推荐等级 ※ ※ ※ ※ ※ 白夜行 推荐等级 ※ ※ ※ ※ ※ 人生 推荐等级 ※ ※ ※ ※ 活着 推荐等级 ※ ※ ※ ※ 许三观卖血记 推荐等级 ※ ※ ※ ※ 皮囊 推荐等级 ...

  10. CF1779 Least Prefix Sum

    url:Problem - C - Codeforces 题意: 给n个数字和一个m 给一个操作:每次使得其中一个下标的数字 *= -1 要求最后在所有前缀和中前m个数字是最小的 思路: 在所有前缀和 ...