首先假定我们已经有了以下文件,用于配置相关参数等等,文件位置是在"WEB-INF/classes/config/spring/"下
config.properties
applicationContext.xml
在应用程序中只需要以下代码,就可以简单用以上配置文件初始化一个Sping容器了
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("classpath:/config/spring/config.properties"));
PropertyPlaceholderConfigurer config = new PropertyPlaceholderConfigurer();
config.setProperties(properties);
FileSystemXmlApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:/config/sping/applicationContext.xml");
applicationContext.addBeanFactoryPostProcessor(config);
applicationContext.refresh();
给FileSystemXmlAppliicationContext的构造函数传入配置好的xml文件就可以加载相关的sping配置,调用其refresh函数
就可以初始化容器了,FileSystemXmlApplicationContext的构造函数可以传入多个xml配置文件。
然后再看看上边两个文件都是干嘛的。
applicationContext.xml就是spring的配置文件,在其中可以配置数据源、sessionFactory等各种bean,看一个完整的例子
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans default-lazy-init="false">
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apach.tomcat.jdbc.pool.DataSource" destroy-method="close">
<!-- 配置数据库连接池 -->
<property name="poolProperties">
<bean class="org.apach.tomcat.jdbc.pool.PoolProperties">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="$(db.url)" />
<property name="username" value="${db.user}" />
<property name="password" value="${db.password}" />
<property name="testWhileIdel" value="true" />
<property name="testOnReturn" value="true" />
<property name="testOnBorrow" value="true" />
<property name="validationQuery" value="select now()" />
</bean>
</property>
</bean>
<!-- 配置SessionFactory -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations">
<!-- 配置文件列表 -->
<list>
<value>"classpath:test/domain/Test.xml"</value>
</list>
</property>
<!-- 配置hibernate -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.apach.hibernate.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<!-- 配置HibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="SessionFactory" ref= "SessionFactory" />
</bean>
</beans>
然后再看看config.properties文件的内容
db.url=jdbc:mysql://localhost:3306/myDatabase?useUnicode=true&characterEncoding=utf-8
db.user=root
db.password=123
看看两个文件内容有什么关联吗? 其实config.properties的作用就是将一些经常或者容易发生变化的内容提取出来单
独配置的,在代码中将初始化好的PropertyPlaceholderConfigurer配置给Spring容器,然后在Spring的配置文件中通过${}就能
获取对应的参数值,而不用直接去修改applicationContext.xml文件。
如果是web项目的话,就可以直接在web.xml中配置spring了,而不用写初始化的代码
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/config/spring/applicatonContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
那config.properties怎么办呢? 也可以直接在applicationContext.xml中配置,在applicationContext.xml中添加以下配置就行了
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/config/spring/config.properties</value>
</list>
</property>
</bean>
一个spring+hibernate的框架已经可以使用了 =,=