关于spring核心配置文件中的各项主要配置

本文详细介绍了Spring框架的核心配置过程,包括配置文件applicationContext.xml的设置、数据库连接配置、连接池配置、与Hibernate及MyBatis整合的方法、事务管理配置以及AOP切面配置等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:spring的核心配置文件中的各种配置。      


spring的核心配置文件的名字 叫做 applicationContext.xml,后期也可以通过配置文件中的配置修改名称,在web.xml中进行如下配置:

   
[html] view plain copy
  1. <context-param>  
  2.        <param-name>contextConfigLocation</param-name>  
  3.        <param-value>classpath:spring/applicationContext*.xml</param-value>  
  4.    </context-param>  





2:核心配置文件中关于dao层的配置。(1):首先准备db.properties 配置文件,最简单的配置如下。

[html] view plain copy
  1. jdbc.driver=com.mysql.jdbc.Driver  
  2. jdbc.url=jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf-8  
  3. jdbc.username=root  
  4. jdbc.password=123456  




(2):然后加载在核心配置文件中加载数据库文件.

[html] view plain copy
  1. <context:property-placeholder location="classpath:resource/db.properties" />  




(3):配置数据库连接池,配置类可以用BasicDatasource,也可以用阿里巴巴的配置核心类 DruidDataSource。

[html] view plain copy
  1. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  
  2.         destroy-method="close">  
  3.         <property name="url" value="${jdbc.url}" />  
  4.         <property name="username" value="${jdbc.username}" />  
  5.         <property name="password" value="${jdbc.password}" />  
  6.         <property name="driverClassName" value="${jdbc.driver}" />  
  7.         <property name="maxActive" value="10" />  
  8.         <property name="minIdle" value="5" />  
  9.     </bean>  




后期需要可以在其中添加多个属性配置。

(4):spring和hibernate,和mybatis的整合主要是整合sessionFactory.
     和hibernate的一个整合。

[html] view plain copy
  1. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  2.           <!-- 与dataSource -->  
  3.           <property name="dataSource">  
  4.               <ref bean="dataSource"/>  
  5.          </property>  
  6. </bean>  




  和mybatis的一个整合.

[html] view plain copy
  1. <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->  
  2.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  3.         <!-- 数据库连接池 -->  
  4.         <property name="dataSource" ref="dataSource" />  
  5.         <!-- 加载mybatis的全局配置文件 -->  
  6.         <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />  
  7.     </bean>  



(5):配置文件中关于事务的配置。<

[html] view plain copy
  1. !-- 事务管理器 -->  
  2.     <bean id="transactionManager"  
  3.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  4.         <!-- 数据源 -->  
  5.         <property name="dataSource" ref="dataSource" />  
  6.     </bean>  




配置通知。

[html] view plain copy
  1. <!-- 通知 -->  
  2.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  3.         <tx:attributes>  
  4.             <!-- 传播行为 -->  
  5.             <tx:method name="save*" propagation="REQUIRED" />  
  6.             <tx:method name="insert*" propagation="REQUIRED" />  
  7.             <tx:method name="add*" propagation="REQUIRED" />  
  8.             <tx:method name="create*" propagation="REQUIRED" />  
  9.             <tx:method name="delete*" propagation="REQUIRED" />  
  10.             <tx:method name="update*" propagation="REQUIRED" />  
  11.             <tx:method name="find*" propagation="SUPPORTS" read-only="true" />  
  12.             <tx:method name="select*" propagation="SUPPORTS" read-only="true" />  
  13.             <tx:method name="get*" propagation="SUPPORTS" read-only="true" />  
  14.         </tx:attributes>  
  15.     </tx:advice>  




关于切面的配置。

[html] view plain copy
  1. <!-- 切面 -->  
  2.     <aop:config>  
  3.         <aop:advisor advice-ref="txAdvice"  
  4.             pointcut="execution(* com.store.service.*.*(..))" />  
  5.     </aop:config>  




关于配置文件中的service层的配置。     扫描包下面所有的service层。

[html] view plain copy
  1. <context:component-scan base-package="com.xiaoao.service"/>   




关于注解注入的配置

[html] view plain copy
  1. <mvc:annotation-driven />  




(6):在进行配置的时候所需要引入的命名空间。

[html] view plain copy
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"  
  3.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  6.     http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-4.0.xsd  
  7.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
  8.     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">  




   jar包导入的话,可以使用maven管理,非常方便。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值