import 简化Spring 配置文件
来源:互联网 发布:中国大学生失业数据 编辑:程序博客网 时间:2024/05/22 03:02
对于编写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>
0 0
- import 简化Spring 配置文件
- 使用import简化Spring配置文件
- 使用import简化spring的配置文件
- 使用import简化spring的配置文件
- 使用import简化spring的配置文件
- 使用import简化spring的配置文件
- 使用import简化spring的配置文件
- 使用import简化spring的配置文件
- 简化Spring(1)--配置文件
- 简化Spring(1)--配置文件
- 简化Spring(1)--配置文件
- 简化Spring--配置文件1
- 简化Spring(1)--配置文件
- Spring配置文件中的import
- 多个spring配置文件,简化spring配置
- 简化Spring(1)--配置文件(转载)
- annotation 简化spring的配置文件bean
- applicationContext.xml import spring模块配置文件
- Spring技术内幕——深入解析Spring架构与设计原理(一)IOC实现原理
- UISearchbar 搜索按钮更换 代码实现UISearchbar去除第一响应
- 用apktool反编译的时候出错:Exception in thread "main" brut.androlib.err.UndefinedResObject: resource spec:
- mongoose模块自动生成_id
- Unity_使用NGUI插件点击控件时同时检测到点击地面的解决方案
- import 简化Spring 配置文件
- hdojssworld VS DDD【概率dp求概率】
- 子网掩码与IP地址之间的关系
- Sping总结
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2)
- POJ 3003 解题报告
- Codeforces Round #317 B. Order Book
- 【C语言探索之旅】第三部分第三课:SDL开发游戏之显示图像
- 汇编读书笔记 第一章