applicationContext.xml配置详解

7 篇文章 0 订阅

applicationContext.xml配置详解

applicationContext.xml表头配置详解:

xml版本信息:

<?xml version="1.0" encoding="UTF-8"?>

默认命名空间:表示未使用其他命名空间的所有标签的默认命名空间

xmlns="http://www.springframework.org/schema/beans"	

xsi标准命名空间,用于指定自定义命名空间的schema文件,声明就可以使用schemaLocation属性。

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"	

声明xml schema实例,声明后可以使用schemaLocation

xmlns:util :配置集合,常量

xmlns:jee: 处理javeee标准相关 问题

xmlns:lang: 将动态语言中的对象以beans的形式存放在Spring容器中

xmlns:jdbc: 对JDBC的简单封装

xmlns:aop: 使用spring框架的aop 面向切面编程时,在xml文件中引入aop资源

xmlns:tx : spring事务配置

default-lazy-init:此参数表示延时加载,即在项目启动时不会实例化注解的bean,除非启动项目时需要用到,未实例化的注解对象在程序实际访问调用时才注入调用。默认为false。

applicationContext配置文件内容详解:

context:annotation-config: 是用于激活那些已经在spring容器里注册过的bean上面的注解,也就是显示的向Spring注册,只能够在已经注册过的bean上面起作用。

ignore-unresolvable:是否忽略解析不到的属性

testOnBorrow:如果为true(默认false),当应用向连接池申请连接时,连接池会判断这条连接是否是可用的。

testOnReturn

testWhileIdle

classpath:是JVM用到的一个环境变量,指示JVM如何搜索class

classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找

${}: EL表达式,表示从另一个页面传过来的值

ref属性:用来给一个对象的属性设置值

ref标签:用来引用另一个bean:<ref bean="viewResolver"/>

applicationContext.xml配置文件完整代码:

read-only:只读事务

proxy-target-class属性值决定是基于接口的还是基于类的代理被创建。首先说明下proxy-target-class="true"和proxy-target-class="false"的区别,为true则是基于类的代理将起作用(需要cglib库),为false或者省略这个属性,则标准的JDK 基于接口的代理将起作用。

<?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:task="http://www.springframework.org/schema/task"
	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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"
	default-lazy-init="true">
	 <!-- 是用于激活那些已经在spring容器里注册过的bean上面的注解,也就是显示的向Spring注册 -->
	<context:annotation-config />
	
	<!-- 扫描service、dao -->
	<context:component-scan base-package="com" />
	
	<!-- 配置数据库连接池 -->
	<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>

	<!-- base dataSource -->
	<bean name="baseDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
		destroy-method="close">
		<!-- 配置初始化大小、最小、最大 --> 
		<property name="initialSize" value="5" />
		<property name="maxActive" value="100" />
		<property name="minIdle" value="10" />
		<!-- 配置获取连接等待超时的时间 --> 
		<property name="maxWait" value="60000" />
		 <!-- 验证连接有效与否的SQL,不同的数据配置不同 --> 
		<property name="validationQuery" value="SELECT 'x'" />
		 <!-- 这里建议配置为TRUE,防止取到的连接不可用 --> 
		<property name="testOnBorrow" value="true" />
		<property name="testOnReturn" value="true" />
		<property name="testWhileIdle" value="true" />
		 <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 
		<property name="minEvictableIdleTimeMillis" value="300000" />
		 <!-- 超过时间限制是否回收 --> 
		<property name="removeAbandoned" value="true" />
		 <!-- 超时时间;单位为秒。180秒=3分钟 --> 
		<property name="removeAbandonedTimeout" value="1800" />
		
		<property name="logAbandoned" value="true" />
		<property name="filters" value="mergeStat" />
	</bean>

 <!--   数据库链接地址 -->
	<bean name="master-dataSource" parent="baseDataSource"
		init-method="init">
		<property name="url" value="${master_driverUrl}" />
		<property name="username" value="${master_username}" />
		<property name="password" value="${master_password}" />
	</bean>


	<!-- spring整合mybatis (mybatis-spring项目提供整合类) -->
	<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
		<!-- 配置数据源 -->
		<property name="dataSource" ref="master-dataSource"></property>
		<!-- 指定mybatis配置文件 -->
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
		<!-- typeAliasesPackage:支持通配符扫描路径  对应的实体路径-->
		<property name="typeAliasesPackage" value="com.*.entity" />
		<!-- classpath:默认的resources路径  对应的xml文件路径-->
		<property name="mapperLocations">
			<list>
				<value>classpath*:mapper/*/*.xml</value>
			</list>
		</property>
		 <!--注入全局mybatis策略配置   在下面配置了globalConfig-->
    	<property name="globalConfig" ref="globalConfig" />
	</bean>

	<!-- MyBatis 动态实现 -->
	<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 对Dao 接口动态实现,需要知道接口在哪 -->
		<property name="basePackage" value="com.*.dao" />
	</bean>
	
	<!-- 定义 MP 全局策略 -->
	<bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration">
	    <!-- 逻辑删除 定义下面3个参数-->
	   <!--  <property name="sqlInjector" ref="logicSqlInjector" />
	    <property name="logicDeleteValue" value="-1" />
	    <property name="logicNotDeleteValue" value="1" /> -->
	    <!-- 全局ID类型: 0, "数据库ID自增", 1, "用户输入ID", 2, "全局唯一ID", 3, "全局唯一ID"-->
	    <property name="idType" value="0" />
	    <property name="dbColumnUnderline" value="false" />
	    
	</bean>

	<!-- 事务管理 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="master-dataSource"></property>
	</bean>

	<!-- 事务管理 属性 -->
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager" >
		<tx:attributes>
		<!--7种事务配置
		    REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 
 			SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。 
			MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。 
			REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。 
			NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 
			NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。 
			NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务 -->
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="select*" propagation="REQUIRED" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- AOP配置切面 -->
	<aop:config proxy-target-class="true">
		<aop:advisor advice-ref="transactionAdvice" 
			pointcut="execution(* *.service..*.*(..))" />
	</aop:config>
	<aop:aspectj-autoproxy proxy-target-class="true" />
	
	
</beans>
  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
applicationContext.xmlSpring Framework 的配置文件,用来定义和配置应用程序中的 Bean 和它们之间的关系。 在 applicationContext.xml 文件中,可以定义各种不同类型的 Bean,包括普通的 Java 类、Spring 框架提供的类,以及第三方库提供的类。通过配置文件,可以指定这些 Bean 的作用域、依赖关系、属性和方法等。 在 applicationContext.xml 文件中,可以使用 `<bean>` 元素来定义一个 Bean。每个 `<bean>` 元素代表着一个对象,通过指定类的全名或者引用已经存在的 Bean,可以创建出一个具体的实例。 同时,可以在 `<bean>` 元素中使用各种不同的属性来定制 Bean 的行为。例如,可以使用 `scope` 属性指定 Bean 的作用域,如 singleton(单例)、prototype(原型)等;可以使用 `autowire` 属性来自动装配相关的 Bean,简化依赖注入的配置;可以使用 `init-method` 和 `destroy-method` 属性指定 Bean 的初始化和销毁方法。 在 applicationContext.xml 文件中,还可以使用 `<property>` 元素来设置 Bean 的属性。通过指定属性名称和值,可以向 Bean 注入相应的值。另外,还可以使用 `<constructor-arg>` 元素设置构造函数的参数。 除了 `<bean>` 元素和相关属性之外,applicationContext.xml 文件中还可以包含一些特殊的元素,如 `<import>`、`<alias>` 等,用来管理配置文件的结构和组织。 总结来说,applicationContext.xmlSpring Framework 的主要配置文件之一,通过它可以灵活地定义和配置应用程序中的 Bean。它提供了丰富的配置选项,方便开发者通过简单的配置实现复杂的功能。同时,它还可以通过注入和自动装配等特性,简化应用程序的开发和维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值