Spring的配置文件为applicationContext.xml,但是有些常量参数一般不配置在applicationContext.xml中,而是放在单独的properties文件中,如数据库连接信息。
applicationContext.xml配置外部的properties文件时,需要PropertyOverrideConfigurer对象,指定properties文件的位置,然后指定属性。
下面是mysql的配置文件,jdbc.properties:
#配置数据驱动 jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8 jdbc.username=root jdbc.password=toor
下面是在applicationContext.xml中配置该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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName"> <!-- 配置数据库信息源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"></property> </bean> </beans>
程序在运行时,PropertyConfigurer对象会寻找jdbc.properties文件中,寻找相应的变量。
下面是测试代码,SpringTest.java
package com.yeetrack.mavenSpring; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { public static void main(String[] args) { //获取ApplicationContext容器 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //通过Spring代理类ProxyFactoryBean获取ServiceImpl实例 IService hello = (IService) context.getBean("service"); hello.sayHello("Good Day!"); DataSource data = (DataSource) context.getBean("dataSource"); Connection conn = null; ResultSet result = null; try { conn = data.getConnection(); Statement stat = conn.createStatement(); String sql = "select * from user"; result = stat.executeQuery(sql); while(result.next()) { System.out.print(result.getString(1)+" "); System.out.print(result.getString(2)+" "); System.out.println(result.getString(3)); } result.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
版权声明
本站文章、图片、视频等(除转载外),均采用知识共享署名 4.0 国际许可协议(CC BY-NC-SA 4.0),转载请注明出处、非商业性使用、并且以相同协议共享。
© 空空博客,本文链接:https://www.yeetrack.com/?p=11
近期评论