spring基础配置

核心配置文件内容如下(Spring):

主要来源1.

约束条件编写

<?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"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.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.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">
	</beans>

数据库基本配置


<!-- 配置DataSource -->
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
	<property name="driverClassName" value="${jdbc.driverClassName}" />
	<property name="url" value="${jdbc.url}" />
	<property name="username" value="${jdbc.username}" />
	<property name="password" value="${jdbc.password}" />
	<!--maxActive: 最大连接数量 -->
	<property name="maxActive" value="150" />
	<!--minIdle: 最小空闲连接 -->
	<property name="minIdle" value="5" />
	<!--maxIdle: 最大空闲连接 -->
	<property name="maxIdle" value="20" />
	<!--initialSize: 初始化连接 -->
	<property name="initialSize" value="30" />
	<!-- 用来配置数据库断开后自动连接的 -->
	<!-- 连接被泄露时是否打印 -->
	<property name="logAbandoned" value="true" />
	<!--removeAbandoned: 是否自动回收超时连接 -->
	<property name="removeAbandoned" value="true" />
	<!--removeAbandonedTimeout: 超时时间(以秒数为单位) -->
	<property name="removeAbandonedTimeout" value="10" />
	<!--maxWait: 超时等待时间以毫秒为单位 1000等于60-->
	<property name="maxWait" value="1000" />
	<!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. -->
	<property name="timeBetweenEvictionRunsMillis" value="10000" />
	<!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 -->
	<property name="numTestsPerEvictionRun" value="10" />
	<!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程 -->
	<property name="minEvictableIdleTimeMillis" value="10000" />
	<property name="validationQuery" value="SELECT NOW() FROM DUAL" />
 </bean>

MyBatis基本配置

主要来源2.

<!-- sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 注入连接池 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 配置别名 -->
		<property name="typeAliasesPackage" value="cn.tomdog.crm.domain,cn.tomdog.crm.query"></property>
		<!-- 配置映射文件 *来通配所有映射 -->
		<property name="mapperLocations" value="classpath:cn/tomdog/crm/mapper//I*Mapper.xml"></property>
	</bean>

扫描文件配置

<!-- 配置扫描所有的mapper.xml -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 扫描mapper接口的包路径 -->
		<property name="basePackage" value="cn.tomdog.crm.mapper,cn.tomdog.crm.utils" />
	</bean>

事物配置

主要来源3.

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 注解方式管理事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

MyBatis默认头部

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="全限定名">

核心配置文件(SpringMVC):

约束条件编写

<?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.xsd">
		
</beans>

公共配置

扫描文件

	<!-- 扫描的包,这里只扫描controller -->
	<context:component-scan base-package="cn.itsource.crm.controller" />
	<!-- 静态资源放行 -->
	<mvc:default-servlet-handler />
	<!-- 配置视图解析器:前缀和后缀的配置 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="前缀名" />
		<property name="suffix" value="后缀名" />
	</bean>

上传下载

<!-- 文件的上传和下载 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize">
			<!-- 限制上传文件大小5m -->
			<value>#{1024*1024*5}</value>
		</property>
	</bean>

jdbc配置

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql:/127.0.0.1:端口/crm
db.username=root
db.password=root

日志文件配置

#log4j.properties(\u65E5\u5FD7\u6587\u4EF6:)
log4j.rootLogger=ERROR, stdout
#log4j.rootLogger=NONE 
log4j.logger.cn.itsource.crm=TRACE

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

WEB.xml主要配置

<!-- spring的核心配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<!-- spring启动的监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 配置一个过滤器防止post方式提交中文乱码 -->
	<filter>
		<filter-name>characterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
  • 权限拦截器
<!-- springMVC的核心控制器 -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- springmvc的核心配置文件 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:applicationContext-mvc.xml</param-value>
		</init-param>
		<!-- 容器启动的时候加载springmvc -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<!-- 支持RESTful风格的url -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>

说明

仅是个人笔记方便文档-不喜勿喷


  1. 主要看Spring官方文档3,4,5*. ↩︎

  2. 主要看MyBatis官方文档*. ↩︎

  3. 主要看Spring官方文档*. ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring基础配置主要包括以下几个方面的内容:\[1\]\[2\]\[3\] 1. Spring的类包必须已经放在Spring的类容器下面。这意味着我们需要将Spring的类包放在项目的类路径下,以便Spring容器能够正确加载和管理这些类。 2. 应用程序应当为Spring提供完备的Bean的配置信息。这些配置信息可以通过XML文件或者注解的方式进行定义,用于描述Bean的属性、依赖关系、行为配置等。 3. Bean的类都已经放在Spring的类容器下面。这意味着我们需要将所有需要由Spring管理的Bean的类放在Spring容器能够扫描到的位置,以便Spring能够正确实例化和管理这些Bean。 4. Spring配置文件是Spring容器对Bean进行生产以及关系注入的图纸。这个配置文件是一个或多个标准的XML文档,其中最常见的是ApplicationContext.xml,它是Spring的默认配置文件。在容器启动时,如果找不到其他的配置文件,Spring会尝试加载这个默认的配置文件。 5. Bean的配置信息由Bean的元数据信息组成,包括Bean的实现类、属性信息、依赖关系、行为配置以及创建方式定义等。这些信息用于告诉Spring容器如何实例化和装配Bean,以及如何为上层应用提供准备就绪的运行环境。 综上所述,Spring基础配置包括将Spring的类包放在类路径下、提供完备的Bean的配置信息、将Bean的类放在Spring容器能够扫描到的位置、配置Spring配置文件以及定义Bean的元数据信息。这些配置将帮助Spring容器正确加载和管理Bean,为应用程序提供准备就绪的运行环境。 #### 引用[.reference_title] - *1* *2* *3* [Spring bean配置的六种方式](https://blog.csdn.net/echizao1839/article/details/88063013)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值