配置bean之你使用外部属性
来源:互联网 发布:linux常用命令kill 编辑:程序博客网 时间:2024/05/20 20:01
beans-properties.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 http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 导入属性文件 --><context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 使用外部化文件的属性 有点像el表达式--> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <property name="driverClass" value="${driverclass}"></property> <property name="jdbcUrl" value="${jdbcurl}"></property> </bean></beans>
在上面代码中,属性的赋值用到了.properties文件中的内容。
db.properties:
user=rootpassword=1230driverclass=com.mysql.jdbc.Driverjdbcurl=jdbc:mysql///test
.properties文件都是以键值对的形式存在。
main.java:
package com.atguigu.spring.beans.properties;import java.sql.SQLException;import javax.sql.DataSource;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main { public static void main(String[] args) throws SQLException { ApplicationContext c=new ClassPathXmlApplicationContext("beans-properties.xml"); DataSource dataSource=(DataSource)c.getBean("dataSource"); System.out.println(dataSource.getConnection()); }}
0 0
- 配置bean之你使用外部属性
- Spring-Bean配置-使用外部属性文件
- Spring4笔记----使用外部属性文件配置 bean
- Spring中配置bean时使用外部属性文件
- spring学习总结(六):IOC & DI 配置Bean之作用域、加载外部属性文件、SPEL
- Xml配置里的bean自动装配与使用外部属性文件
- 通过@ConfigurationProperties使用外部配置填充Bean属性的几种方法
- Spring4深入理解IOC&DI02----Bean配置--自动装配、bean之间的继承与依赖、使用外部属性文件
- spring bean配置之集合属性
- Spring4----使用注解方式配置bean和bean的属性
- spring ioc之使用外部属性文件
- Spring配置bean属性
- bean属性配置解析
- J2EE之Spring中Bean的作用域、外部属性文件、SpEL
- Spring4.0学习笔记-bean之间的关系,作用域以及使用外部属性文件
- Spring4学习笔记(七):bean的作用域和使用外部属性文件
- 12.笔记JAVA Spring框架学习————Bean使用外部属性文件
- (三)Spring XML 配置 之Bean 属性
- 你不知道的Kettle学习笔记5--日志
- MySQL安装问题小结
- 【OpenCV入门指南】第四篇 图像的二值化
- 指定ubuntu下的Python的运行版本
- MapReduce实例之PageRank
- 配置bean之你使用外部属性
- Java工程师裸辞之后的面试!!!
- 【OpenCV入门指南】第五篇 轮廓检测 上
- 高德定位
- 【OpenCV入门指南】第六篇 轮廓检测 下
- Servlet练习--第一个servlet程序
- 对CSDN的建议(二)
- 【OpenCV入门指南】第七篇 线段检测与圆检测
- i.mx6 yocto 找不到 SDL