<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!-- 对所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> <context:component-scan base-package="com.xinx.own.*" /> <!-- 激活@Controller模式 --> <mvc:annotation-driven /> <!-- 注解式事务 <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> --> <!-- 数据源配置, 使用应用中的DBCP数据库连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- Connection Info --> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/db_xxx?useUnicode=true&characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="xxx" /> <!-- <property name="maxActive" value="8" /> <property name="maxIdle" value="2" /> <property name="defaultAutoCommit" value="false" /> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <property name="minEvictableIdleTimeMillis" value="600000" /> --> </bean> <!-- 自动扫描所有的xxxMapper.xml对应的mapper接口文件 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.xinx.own.dao" /> </bean> <!-- 配置Mybatis的文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:mapper/*.xml"/> <!-- 配置分页拦截器插件 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <value></value> </property> </bean> </array> </property> </bean> <!-- 事务管理器,配置事务管理组件 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> --> </beans>
1、< context:annotation-config/>
用于激活那些已经在spring容器里注册过的bean上面的注解,也就是显示的向Spring注册
- AutowiredAnnotationBeanPostProcessor
- CommonAnnotationBeanPostProcessor
- PersistenceAnnotationBeanPostProcessor
- RequiredAnnotationBeanPostProcessor
这四个Processor,注册这4个BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。BeanPostProcessor就是处理注解的处理器。
比如我们要使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
2、<context:component-scan base-package=”xxx.xxx”/>
该配置项包含了自动注入上述processor的功能,因此当使用 < context:component-scan/> 后,就可以将 < context:annotation-config/> 移除了。
通过对base-package配置,就可以把controller包下 service包下 dao包下的注解全部扫描到!
<!-- 对所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> <context:component-scan base-package="com.xinx.own.*" />
3、<mvc:annotation-driven />
主要就是为了Spring MVC来用的,提供Controller请求转发,json自动转换等功能。
<!-- 激活@Controller模式 --> <mvc:annotation-driven />
4、查找在属性Properties
指定的文件
<context:property-placeholder location="classpath:spring/jdbc.properties"/>
5、专用配置元素配置属性覆盖
<context:property-override location="classpath:override.properties"/>