spring相关配置文件
Posted fantastic-code
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring相关配置文件相关的知识,希望对你有一定的参考价值。
1.application-dao.xml配置:
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 引入db.properties --> <context:property-placeholder location="classpath:db.properties" system-properties-mode="FALLBACK"/> <!-- 声明dataSource --> <bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!-- 注入连接属性 --> <property name="driverClassName" value="$driverClassName"></property> <property name="url" value="$url"></property> <property name="username" value="$username"></property> <property name="password" value="$password"></property> </bean> <!-- 使用c3p0的数据源 --> <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- 注入连接属性 --> <property name="driverClass" value="$driverClassName"></property> <property name="jdbcUrl" value="$url"></property> <property name="user" value="$username"></property> <property name="password" value="$password"></property> <!-- 设置初始化连接池大小 --> <property name="initialPoolSize" value="5"></property> <!-- 设置最大连接数 --> <property name="maxPoolSize" value="50"></property> <!-- 设置最小的连接数 --> <property name="minPoolSize" value="10"></property> </bean> <!-- 使用dbcp的数据源 --> <bean id="dataSource3" class="org.apache.commons.dbcp.BasicDataSource" > <!-- 注入连接属性 --> <property name="driverClassName" value="$driverClassName"></property> <property name="url" value="$url"></property> <property name="username" value="$username"></property> <property name="password" value="$password"></property> <!-- 设置初始化连接池大小 --> <property name="initialSize" value="5"></property> <!-- 设置最大连接数 --> <property name="maxIdle" value="50"></property> <!-- 设置最大活动连接数 --> <property name="maxActive" value="10"></property> <!-- 设置等待时间 --> <property name="maxWait" value="5000"></property> </bean> <!-- 使用druid的数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"> <!-- 注入连接属性 --> <property name="driverClassName" value="$driverClassName"></property> <property name="url" value="$url"></property> <property name="username" value="$username"></property> <property name="password" value="$password"></property> <!-- 设置初始化连接池大小 --> <property name="initialSize" value="5"></property> <!-- 最大连接数 --> <property name="maxActive" value="10"></property> <!-- 设置等待时间 --> <property name="maxWait" value="5000"></property> <!-- --> <property name="filters" value="stat"></property> </bean> <!-- 声明sessionFactory 并注入mybatis.cfg.xml--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据源 --> <property name="dataSource" ref="dataSource"></property> <!-- 注入mapper.xml --> <property name="mapperLocations"> <array> <value>classpath:mapper/*/*Mapper.xml</value> </array> </property> <!-- 插件 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"></bean> </array> </property> </bean> <!-- 扫描mapper接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入mapper接口所在的包 注意多个包的情况的配置--> <property name="basePackage" > <value> com.yyj.sys.mapper com.yyj.bus.mapper com.yyj.stat.mapper </value> </property> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
2.application-service.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="com.yyj.sys.service.impl"></context:component-scan> <context:component-scan base-package="com.yyj.bus.service.impl"></context:component-scan> <context:component-scan base-package="com.yyj.stat.service.impl"></context:component-scan> <!-- 1,声明事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 启动注解事务 --> <!-- <tx:annotation-driven/> --> <!-- 2,声明事务的传播特性 也就是通知 --> <tx:advice id="advise" transaction-manager="transactionManager"> <tx:attributes> <!-- 以add开头的方法名需要事务 --> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="change*" propagation="REQUIRED"/> <tx:method name="reset*" propagation="REQUIRED"/> <tx:method name="get*" read-only="true"/> <tx:method name="load*" read-only="true"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 3进行AOP织入 --> <aop:config> <!-- 声明切面 --> <aop:pointcut expression="execution(* com.yyj.sys.service.impl.*.*(..))" id="pc1"/> <aop:pointcut expression="execution(* com.yyj.bus.service.impl.*.*(..))" id="pc2"/> <aop:pointcut expression="execution(* com.yyj.stat.service.impl.*.*(..))" id="pc3"/> <!-- 织入 --> <aop:advisor advice-ref="advise" pointcut-ref="pc1"/> <aop:advisor advice-ref="advise" pointcut-ref="pc2"/> <aop:advisor advice-ref="advise" pointcut-ref="pc3"/> </aop:config> </beans>
3.application-context.xml配置
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <import resource="classpath:application-dao.xml"/> <import resource="classpath:application-service.xml"/> <!-- <import resource="classpath:application-task.xml"/> --> </beans>
4.spring-mvc配置
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 扫描controller --> <context:component-scan base-package="com.yyj.sys.controller"></context:component-scan> <context:component-scan base-package="com.yyj.bus.controller"></context:component-scan> <context:component-scan base-package="com.yyj.stat.controller"></context:component-scan> <!-- 配置映射器和适配器 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 配置前视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 注入前后缀 --> <property name="prefix" value="/WEB-INF/view/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 配置springmvc对文件上传的支持 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置文件名的编码 --> <property name="defaultEncoding" value="utf-8"></property> <!-- 配置最上传文件的支持 20M --> <property name="maxUploadSize" value="20971520"></property> <!-- 设置文件上传的临时目录 --> <property name="uploadTempDir" value="upload/temp" /> </bean> <!-- 配置静态文件放行 --> <mvc:default-servlet-handler /> </beans>
5.log4j.properties文件配置
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
6.db.properties文件配置(数据库)
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/0328carrent?useUnicode=true&characterEncoding=UTF-8 username=root password=123456
<?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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><context:component-scan base-package="com.yyj.sys.service.impl"></context:component-scan><context:component-scan base-package="com.yyj.bus.service.impl"></context:component-scan><context:component-scan base-package="com.yyj.stat.service.impl"></context:component-scan><!-- 1,声明事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property></bean><!-- 启动注解事务 --><!-- <tx:annotation-driven/> --><!-- 2,声明事务的传播特性 也就是通知 --><tx:advice id="advise" transaction-manager="transactionManager"><tx:attributes><!-- 以add开头的方法名需要事务 --><tx:method name="add*" propagation="REQUIRED"/><tx:method name="save*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="change*" propagation="REQUIRED"/><tx:method name="reset*" propagation="REQUIRED"/><tx:method name="get*" read-only="true"/><tx:method name="load*" read-only="true"/><tx:method name="*" read-only="true"/></tx:attributes></tx:advice><!-- 3进行AOP织入 --><aop:config><!-- 声明切面 --><aop:pointcut expression="execution(* com.yyj.sys.service.impl.*.*(..))" id="pc1"/><aop:pointcut expression="execution(* com.yyj.bus.service.impl.*.*(..))" id="pc2"/><aop:pointcut expression="execution(* com.yyj.stat.service.impl.*.*(..))" id="pc3"/><!-- 织入 --><aop:advisor advice-ref="advise" pointcut-ref="pc1"/><aop:advisor advice-ref="advise" pointcut-ref="pc2"/><aop:advisor advice-ref="advise" pointcut-ref="pc3"/></aop:config></beans>
以上是关于spring相关配置文件的主要内容,如果未能解决你的问题,请参考以下文章