Spring XML配置文件详解

本文详细介绍了Spring框架的XML配置文件,包括等核心标签的使用。讨论了bean的属性如lazy-init、init-method、destroy-method以及抽象bean和parent属性。还提到了factory-bean和factory-method的工厂模式应用,以及ref属性在bean之间的引用。此外,文章还引导读者查阅相关链接以深入理解AOP和bean的值注入。
摘要由CSDN通过智能技术生成

​ spring配置文件是用于指导Spring工厂进行Bean生产、依赖关系注入(装配)及Bean实例分发的"图纸"。Spring框架的配置文件是基于xml的,Spring强大的功能依赖于类型繁多的配置项,这些配置项纷繁复杂难以记忆,下面将常用的配置项示例记录下来,以备后续查看使用。

一、Spring配置文件示例
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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.test.fx.service"></context:component-scan>
	<!-- 引入DB配置文件 -->
	<context:property-placeholder location="classpath:oracleDriver.properties"/>	
	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driver}"></property>
		<property name="url" value="${url}"></property>
		<property name="username" value="${name}"></property>
		<property name="password" value="${password}"></property>
	</bean>
	<!-- 创建SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="mapperLocations" value="classpath:com/baizhi/fx/dao/*DaoImpl.xml"></property>
	</bean>
	<!-- 扫描Mapper文件并生成dao对象 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
		<property name="basePackage" value="com.test.fx.dao"></property>
	</bean>
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 事务管理器注解 -->	
	<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

二、配置文件详解
  • 上面给出了Spring最基本的配置文件示例,下面就其标签元素进行详细说明
1.XML结构体概述

这是一个最基本的Spring XML配置文件结构体,这些格式基本都是固定的,可以直接复用

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" <!-- 默认bean命名空间 -->
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <!-- xml约束命名空间定义 -->
xmlns:aop="http://www.springframework.org/schema/aop"<!-- AOP命名空间的scheme约束 -->
xmlns:context="http://www.springframework.org/schema/context" <!-- context命名空间的scheme约束 -->

xsi:schemaLocation=" <!-- 上面各个scheme的location信息(网络地址) -->
     http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-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/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd ">

</beans>
2.<Bean>标签
<!-- 一个最基本的bean标签 -->
<bean id="bean名称" class="bean的类全名(类全限定路径)" scope="作用域">

上面bean配置了3条最常见的属性,实际上bean的属性有以下内容:

属性 说明
id bean的名称,可以随便起名但在spring定义中,一般为类名的驼峰命名。例如<bean id=“u
  • 40
    点赞
  • 192
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值