spring-context入门配置详解

来源:互联网 发布:gta5pc男角色捏脸数据 编辑:程序博客网 时间:2024/05/22 15:19
<?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:context="http://www.springframework.org/schema/context"    xmlns:task="http://www.springframework.org/schema/task"    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:lang="http://www.springframework.org/schema/lang"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.2.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd "        >    <description>Spring 基本配置</description>    <!-- 加载配置属性文件,这个标签会读取pscq.properties文件,pscq.properties里面放置了连接数据库的信息 -->    <context:property-placeholder ignore-unresolvable="true" location="classpath:pscq.properties" /><!-- 因为上面已经读取了pscq.properties文件,才能进行数据源配置, 使用 DRUD 数据库连接池 -->    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"        init-method="init" destroy-method="close">        <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->        <property name="driverClassName" value="${jdbc.driver}" />        <!-- 基本属性 url、user、password -->        <property name="url" value="${jdbc.url}" />        <property name="username" value="${jdbc.username}" />        <property name="password" value="${jdbc.password}" />        <!-- 配置初始化大小、最小、最大 -->        <property name="initialSize" value="${jdbc.pool.init}" />        <property name="minIdle" value="${jdbc.pool.minIdle}" />        <property name="maxActive" value="${jdbc.pool.maxActive}" />    </bean><!-- MyBatis配置 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <property name="typeAliasesPackage" value="com.zyyw.pscq,com.cl.runtime" />        <property name="typeAliasesSuperType" value="com.cl.runtime.persistence.BaseEntity" />        <property name="mapperLocations" value="classpath:/mappings/**/*.xml" />        <property name="configLocation" value="classpath:/mybatis-config.xml"></property>    </bean>

mapperLocations:它表示我们的Mapper文件存放的位置,当我们的Mapper文件跟对应的Mapper接口处于同一位置的时候可以不用指定该属性的值。
configLocation:用于指定Mybatis的配置文件位置。如果指定了该属性,那么会以该配置文件的内容作为配置信息构建对应的SqlSessionFactoryBuilder,但是后续属性指定的内容会覆盖该配置文件里面指定的对应内容。
typeAliasesPackage:它一般对应我们的实体类所在的包,这个时候会自动取对应包中不包括包名的简单类名作为包括包名的别名。多个package之间可以用逗号或者分号等来进行分隔。(value的值一定要是包的全名)
typeAliasesSuperType:代表实体类所继承的类的位置

<!-- 扫描basePackage下所有以@MyBatisDao注解的接口 -->    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />        <property name="basePackage" value="com.zyyw.pscq" />        <property name="annotationClass" value="com.cl.runtime.persistence.annotation.MyBatisDao" />    </bean>

@MyBatisDao,是自定义的,作用是扫描Dao中带有@MyBatisDao注解的DAO接口。与上段代码联合使用,springmvc会根据mapper中的方法,自动将DAO接口实现。

<!-- 定义事务 -->    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean>    <!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务 -->    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />    <!-- 缓存配置 -->    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">        <property name="configLocation" value="classpath:${ehcache.configFile}" />    </bean><!-- 加载应用属性实例,可通过 @Value("#{APP_PROP['jdbc.driver']}") String jdbcDriver         方式引用 -->    <util:properties id="APP_PROP" location="classpath:pscq.properties" local-override="true" />

最后一个配置的作用,可以在类里使用${jdbc.pool.minIdle}的方式,获取pscq.properties的值。

0 0
原创粉丝点击