对于编写SSH程序的程序员来说,Spring的配置文件(applicationContext.xml)是一个非常让人头疼的一个问题,自己的一个不小心会导致,项目的异常或者更为严重的是项目无法启动。如何解决applicationContext.xml 配置文件臃肿的问题,是本篇文章讨论的重点。对于一个拥有丰富编码经验的工程师,一定会想到使用"import"策略,优化applicationContext.xml配置文件。
大家接下来,看到的这段代码是一个非常web简单项目的配置文件.
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 配置数据源 -->
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="mappingResources">
<list>
<value>User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<bean id="UserDaoImpl" class="ssh.Dao.UserDaoImpl">
<property name="factory" ref="mySessionFactory"></property>
</bean>
<bean id="UserServiceImpl" class="ssh.Service.UserServiceImpl">
<property name="personDao" ref="UserDaoImpl"></property>
</bean>
<bean id="UserAction" class="ssh.action.UserAction" scope="prototype">
<property name="userServiceimpl" ref="UserServiceImpl"></property>
</bean>
</beans>
大家看了这段代码是不是感觉,这也没有什么复杂的,但是我们的从spring 配置文件完成了那些功能来说起:数据源配置、Dao层实现、Service层实现、Action控制层和数据库操作sessionFactory,大家有没有感觉是不是这个applicationContext.xml需要完成的事情是不是太多了。我们必须简化好一下。
大家请看第二段代码:是进过简单的优化 代码,在applicationContext.xml 同一级目录,我们建立了一个SpringBean.xml文件(名称可以自己任意定义)。
springBean.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="UserDaoImpl" class="ssh.Dao.UserDaoImpl">
<property name="factory" ref="mySessionFactory"></property>
</bean>
<bean id="UserServiceImpl" class="ssh.Service.UserServiceImpl">
<property name="personDao" ref="UserDaoImpl"></property>
</bean>
<bean id="UserAction" class="ssh.action.UserAction" scope="prototype">
<property name="userServiceimpl" ref="UserServiceImpl"></property>
</bean>
</beans>
applicationContext 通过import策略,引用其他的配置文件信息。
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 配置数据源 -->
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="123456" />
</bean>
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="mappingResources">
<list>
<value>User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<!--引入其他配置文件 -->
<import resource="springBean.xml"></import>
</beans>