Spring配置文件引入外部资源
2015-05-25 10:12
435 查看
UserDao类 public class UserDao { private String jdbcUrl; private String driverClass; private String username; private String password; public String getJdbcUrl() { return jdbcUrl; } public void setJdbcUrl(String jdbcUrl) { this.jdbcUrl = jdbcUrl; } public String getDriverClass() { return driverClass; } public void setDriverClass(String driverClass) { this.driverClass = driverClass; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "UserDao [jdbcUrl=" + jdbcUrl + ", driverClass=" + driverClass + ", username=" + username + ", password=" + password + "]"; }
配置文件 <?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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- <context:property-placeholder location="classpath:cn/com/location/jdbc.properties"/> --> <!--引入多个外部资源--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:cn/com/location/jdbc.properties</value> </list> </property> </bean> <bean id="userDao" class="cn.com.location.UserDao"> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="driverClass" value="${driverClass}"></property> <property name="username" value="${username}"></property> <property name="password" value="${password}"></property> </bean> </beans>
jdbc.properties资源文件 jdbcUrl = jdbc:mysql://localhost:3306/tt_test driverClass = com.mysql.jdbc.Driver username = root password = root
测试类 public class test { @Test public void test1(){ ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml", this.getClass()); UserDao userDao = (UserDao) app.getBean("userDao"); System.out.println(userDao.toString()); } }
相关文章推荐
- spring中PropertyPlaceholderConfigurer的使用,引入外部资源文件
- spring.xml从外部文件引入数据库配置信息
- spring.xml从外部文件引入数据库配置信息
- springboot干货——(八)springboot引入外部xml配置文件
- spring 引入外部配置文件
- spring.xml从外部文件引入数据库配置信息
- spring (三) 加载外部资源文件/配置jdbc数据源
- springboot 读取外部配置文件
- spring载入外部配置文件的方法
- spring引入多个配置文件
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
- spring-boot 加载本地静态资源文件路径配置
- Spring整合JUnit4测试,使用注解引入一个或多个配置文件
- spring 配置文件中 引入properties文件
- Spring配置文件加载外部配置文件
- SpringBoot读取外部的资源配置文件及配置数据库连接池
- Spring引用外部文件生成主键配置
- spring-boot 加载本地静态资源文件路径配置
- Spring整合JUnit4测试,使用注解引入一个或多个配置文件
- Spring多配置文件与国际化,使用资源文件