下面编写了两个实例来进行演示:
- 实例一:在第三步中的xml中,通过代码:
<context:property-placeholder location="classpath:database.properties"/>
导入类路径下文件 - 实例二:在第一步中的xml导入另外两个配置文件通过代码:
<import resource="classpath:spring/spring-*.xml"></import>
实例一:导入其它文件
step.1
我们将模拟连接数据库的过程,在resources下新建一个我们要引入的文件database.propertiser如下:
driver =com.mysql.cj.jdbc.Driver
url = mysql:jdbc://localhost:3306//test?userUnicode=true&characterEncoding=utf-8&serverTimezone=UFC;
user =root
password = root
step.2
新建ProviderDao类
package com.zt.dao;
public class ProviderDao {
private String url ;
private String driver;
private String user;
private String password;
//getter setter
@Override
public String toString() {
return "ProviderDao{" +
"url='" + url + '\'' +
", driver='" + driver + '\'' +
", user='" + user + '\'' +
", password='" + password + '\'' +
'}';
}
}
step.3
新建resources.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:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc ">
<!--通过该方式获取路径下的文件-->
<context:property-placeholder location="classpath:database.properties"/>
<bean class="com.zt.dao.ProviderDao" id="providerDao2">
<!--${}表达式可以去应用我们去应用我们引用的这些properties(通过context:引进的)
里面的属性的值通过它的键名来得到值-->
<property name="driver" value="${driver}"/>
<property name="user" value="${user}"/>
<property name="url" value="${url}"/>
<property name="password" value="${password}"/>
</bean>
</beans>
step4
测试:
@Test
public void m1(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("resources.xml");
ProviderDao providerDao = ctx.getBean("providerDao2", ProviderDao.class);
System.out.println(providerDao);
}
输出结果:ProviderDao{url='mysql:jdbc://localhost:3306//test?userUnicode=true&characterEncoding=utf-8&serverTimezone=UFC;', driver='com.mysql.cj.jdbc.Driver', user='root', password='root '}
引入文件成功,测试完毕
实例二:导入多个配置文件
step.1
新建spring文件夹来存放配置文件,在其下新建一个applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="classpath:spring/spring-*.xml"></import>
</beans>
其中 spring-*.xml 表示能够引入所有以spring-开头的配置文件,我们可以对此进行验证:
step.2
再新建两个配置文件,spring-test.xml 和spring-test2xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="pig" class="com.zt.pojo.Pig">
<property name="name" value="小猪"/>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="pig2" class="com.zt.pojo.Pig">
<property name="name" value="大猪"/>
</bean>
</beans>
其中实体类Pig在此省略,此处对name属性进行注入
step.3
测试:
@Test
public void m2(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
Pig pig = ctx.getBean("pig", Pig.class);
Pig pig2 = ctx.getBean("pig2", Pig.class);
System.out.println(pig.getName());
System.out.println(pig2.getName());
}
此处我们使用applicationContext.xml进行获取上下文,并获得在另外两个配置文件中注入的值,输出小猪 大猪