关于Spring的xml文档的简单实用配置
Spring的spring.xml文档的配置
最近在写Spring的配置文件时,发现Spring文档的配置其实没必要那么繁琐记忆,网上的很多文章都写得很繁琐,如果所有的东西按照路径去查找,可以很快的帮我们完成文档的配置,根本不用我们去记忆,同时配置项也不用那么繁琐,博主按照自己的思路整理出自己的一套文档的配置顺序,算是自己的小心得,仅供参考。
首先我们需要导入如下jar包:(至于导入的路径,会在下面为大家介绍)
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
这些jar的路径可以在Spring的官网进行配置,路径如下:
Spring官网——PROJECTS——SPRING FRAMEWORK(第三个图案)——Spring Framework中的4.3.8版本 reference
然后就是官方的详细介绍了
首先打开搜索,快捷键位Ctrl+F,然后通过“<?xml”头部进行搜索,即可找到官方范本
<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">
</beans>
然后按照如下步骤进行配置:
注:这其中我们分别会用到<tx></tx> <aop></aop> <context></context>三个标签,所以将上面的步骤补充如下:
<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: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.xsd
<!-- 下面的tx aop context三者可以在Spring的官网中看到,但是也可以结合上面的来写,只是部分名字不同,分别用相应的名字代替即可 -->
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
然后在按照如下步骤进行
<!-- 支持Spring的注解方式 -->
<context:annotation-config></context:annotation-config>
<!-- 如果有context:component-scan,则不需要context:annotation-config -->
<context:component-scan base-package="com.dfys.spring.hibernate.dao.impl(这个是自己的包名)"></context:component-scan>
<!-- 1、配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean(可在Maven Dependencies目录下spring-orm\4.3.8.RELEASE.jar中进行查看)">
<!-- 注意:!!!!!一下两个属性的名字一定要通过上面 sessionFactory的class类点进去查看,要保证和里面的名字相同,否则会造成其它错误-->
<!-- 配置连接连接池 -->
<property name="dataSource" ref="datasource"></property>
<property name="mappingResources">
<!-- 因为mappingResources是一个数组,使用list标签 -->
<list>
<value>com/dfys/spring/bean/TbUser.hbm.xml</value>
<value>com/dfys/spring/bean/TbAccount.hbm.xml</value>
<value>com/dfys/spring/bean/TbBaojian.hbm.xml</value>
<value>com/dfys/spring/bean/TbImage.hbm.xml</value>
<value>com/dfys/spring/bean/TbTaocan.hbm.xml</value>
<value>com/dfys/spring/bean/TbHotel.hbm.xml</value>
<value>com/dfys/spring/bean/TbOrder.hbm.xml</value>
<value>com/dfys/spring/bean/TbShoppingCart.hbm.xml</value>
</list>
</property>
<!-- 此处多增加一个属性,便于我们在测试的时候可以通过sql语句来查看是否执行,以及状态是否为懒加载,在事务发生错误的时候,需要回滚时,可以查看是否执行语句回滚,都可以有一个直观的感受 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<!-- 如果Session交给Spring进行管理,则不需要此句 -->
<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->(这句话写出来时注释掉的,不能加上去,因为Spring已经帮我们完成了配置,我们不需要多此一举)
</props>
</property>
</bean>
<!-- 2、配置连接池 -->
<bean id="datasource" class="org.apache.commons.dbcp2.BasicDataSource"(可在Maven Dependencies目录下commons-dbcp2\2.1.1.jar中进行查看)>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/mydb?useSSL=true"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 3、sessionFactory 创建session对象,用来连接数据库,所以增设一个property属性,然后再来连接我们所获取的表格对应的bean对象,所以在增加一个property属性-->
<!-- 然后回到1中进行补充完成 -->
<!-- 4、事务管理器 -->
<!-- 配置Spring的事务管理的类 -->
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"(可在Maven Dependencies目录下spring-orm\4.3.8.RELEASE.jar中进行查看)>
<!-- 事务是通过Session进行开启和提交,Session需要使用Session Factory创建,所以需要sessionFactory对象 -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 5、事务管理的具体方法的说明和配置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 对所有以get开头方法进行事务优化为只读 -->
<!-- <tx:method name="get*" read-only="true"/> -->
<!-- 对所有的方法进行事务的开启和提交 -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 6、使用AOP将事务的管理HIbernateTransactionManager植入到dao层 -->
<aop:config>
<aop:pointcut id="dao_point_cut" expression="execution(* com.dfys.spring.hibernate.dao.*.*(..))"/>(这里的*都代表这泛指,括号中的“..”也泛指参数,有或者没有都可以)
<!-- 配置事务的开始、提交、回滚的一个建议,,, 类似aop:before aop:after -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="dao_point_cut"/>
</aop:config>
</beans>
配置完成。
后记:关于配置,主要是按照自己的思路配置即可,方便自己的记忆优先,整理出自己的思路就行了。
现在Spring的封装已经越来越偏向注解的形式,因为注解比较清晰明了,而且注解的功能已经越来越强大,博主会在后续就这个注解的方式给大家进行详解,使用后你会觉得注解真的很方便,而且有关注解的方式网上也很少,希望后续对大家有所帮助。
关于Spring的xml文档的简单实用配置的更多相关文章
- Spring学习----- Spring配置文件xml文档的schema约束
1.配置文件示例. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...
- 关于Spring配置文件xml文档的schema约束
最开始使用spring框架的时候,对于其配置文件xml,只是网上得知其使用方法,而不明其意.最近想着寻根问底的探究一下.以下是本文主要内容: 1.配置文件示例. <?xml version=&q ...
- Spring中xml文档的schema约束
最开始使用Spring框架的时候,对于其配置文件xml,只是网上得知其使用方法,而不明其意.最近想着寻根问底的探究一下.以下是本文主要内容: 1.配置文件示例. <?xml version= ...
- 使用JDom解析XML文档模拟Spring的配置文件解析
在J2EE项目中可能会涉及到一些框架的使用,最近接触到了SSH,拿Spring来说配置文件的使用是相当重要的,Spring的配置文件是一个xml文件,Spring是如何读取到配置文件并进行依赖注入的呢 ...
- 用ORM的思想操作XML文档,一个对象就搞定不要太简单。滚蛋吧!XmlDocument、XmlNode、Xml***……
大家有没有这样的感受,一涉及XML文档操作就得百度一遍.是不是非!常!烦!.各种类型,各种方法,更别提为了找到一个节点多费劲.本来想写个XML操作的工具方法,写了两行一想既然XML文档是有规律的,如果 ...
- 网络电视精灵~分析~~~~~~简单工厂模式,继承和多态,解析XML文档,视频项目
小总结: 所用技术: 01.C/S架构,数据存储在XML文件中 02.简单工厂模式 03.继承和多态 04.解析XML文档技术 05.深入剖析内存中数据的走向 06.TreeView控件的使用 核心: ...
- Java解析XML文档(简单实例)——dom解析xml
一.前言 用Java解析XML文档,最常用的有两种方法:使用基于事件的XML简单API(Simple API for XML)称为SAX和基于树和节点的文档对象模型(Document Object ...
- 用python批量生成简单的xml文档
最近生成训练数据时,给一批无效的背景图片生成对应的xml文档,我用python写了一个简单的批量生成xml文档的demo,遇见了意外的小问题,记录一下. 报错问题为:ImportError: No m ...
- QT XML文档的解析 QXmlStreamReader, DOM,SAX 三种解析方法 简单示例
0. xml文档如下 <?xml version="1.0"?> <bookindex> <entry term="sidebearings ...
随机推荐
- tinymce 富文本编辑器 编写资料
tinymce官方文档: 粘贴图片插件 博客搬运地址 使用Blob获取图片并二进制显示实例页面 tinymce自动调整插件 是时候掌握一个富文本编辑器了——TinyMCE(1) XMLHttpRequ ...
- spring基础学习---简单配置文件
- 判断IOS静态库(.a文件)是否支持模拟器和真机运行
判断IOS静态库(.a文件)是否支持模拟器和真机运行 在mac终端下,进入到.a文件目录下,然后输入: lipo -info libMyAlertView.a Architectures in the ...
- 【知识总结】卡特兰数 (Catalan Number) 公式的推导
卡特兰数的英文维基讲得非常全面,强烈建议阅读! Catalan number - Wikipedia (本文中图片也来源于这个页面) 由于本人太菜,这里只选取其中两个公式进行总结. (似乎就是这两个比 ...
- 题解报告:hihoCoder #1174:拓扑排序·一
题目链接:https://hihocoder.com/problemset/problem/1174 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 由于今天上课的老师讲 ...
- day03_12/13/2016_bean的管理之初始化和销毁
- 【转】Linux GCC常用命令
转自:http://www.cnblogs.com/ggjucheng/archive/2011/12/14/2287738.html 1简介 2简单编译 2.1预处理 2.2编译为汇编代码(Comp ...
- 函数 out 传值 分割
public void Jia(int a ,int b) { a = a + b; Console.WriteLine(a); } public void Jia1(int a,out int b) ...
- C#中 分层 显示数据库中多表的数据信息
如下图,要实现将三个表中的内容加载到同一个窗体中,该怎么来实现呢? 要实现上面的查询结果,我们就要从Student表中拿到学生姓名,从Subject表中拿到科目名称,从StudentResult表中拿 ...
- python--11、数据库及SQL基础
常用命令记录 查看库中所有表的引擎 SHOW TABLE STATUS FROM `center_main_db`; 还有一个更简洁,查询cmol_system_db库所有表的存储引擎\ SELECT ...