zoukankan      html  css  js  c++  java
  • 关于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-orm4.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-dbcp22.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-orm4.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的封装已经越来越偏向注解的形式,因为注解比较清晰明了,而且注解的功能已经越来越强大,博主会在后续就这个注解的方式给大家进行详解,使用后你会觉得注解真的很方便,而且有关注解的方式网上也很少,希望后续对大家有所帮助。

  • 相关阅读:
    基于摸板匹配的目標跟蹤算法
    spoj 2713 Can you answer these queries IV
    zoj 3633 Alice's present
    hdu 3642 Get The Treasury
    poj 1195 Mobile phones
    poj 2760 End of Windless Days
    zoj 3540 Adding New Machine
    spoj 1716 Can you answer these queries III
    spoj 1043 Can you answer these queries I
    spoj 2916 Can you answer these queries V
  • 原文地址:https://www.cnblogs.com/dfys/p/dfye.html
Copyright © 2011-2022 走看看