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
- spring-context入门配置详解
- spring <context:property-placeholder/> 配置详解
- spring <context:property-placeholder/> 配置详解
- spring <context:property-placeholder/> 配置详解
- spring <context:property-placeholder/> 配置详解
- spring mvc Web.xml配置详解之context-param
- 关于Spring的<context:annotation-config/>配置详解
- Spring Context配置
- spring security 基础入门(配置详解)
- 5 Spring 入门 web.xml配置详解
- Spring <context:annotation-config /> 详解
- Spring <context:annotation-config/>详解
- spring 配置context:component-scan
- context:component-scan 配置详解
- Tomcat context.xml配置详解
- Spring架构详解之Context组件详解
- Spring配置中<context:annotation-config> VS <context:component-scan>
- spring注解注入:<context:component-scan>详解
- kubernetes 1.5安装 Redis 3.2.6 Cluster StatefulSet
- 前后端分离的必要性
- java版+支付宝支付和微信支付(二)
- SecureCRT 上传下载文件
- 分数拆分
- spring-context入门配置详解
- Property follows Cocoa naming convention for returning 'owned' objects
- Python基础——变量、对象和引用
- 通过使用spring-data-mongodb操作mongodb
- 5款Python绘图库,为你轻松实现大数据可视化!
- Android WebView回调无法更改控件状态
- mybatis源码学习之执行过程分析(2)——config.xml配置文件和mapper.xml映射文件解析过程
- java_websocket详解(二)
- 正则表达式相关规则