【Spring】Spring配置文件简单解析

本文详细介绍了Spring框架配置文件applicationContext.xml的各个组成部分及其作用,包括框架配置、激活配置、扫描配置、代理配置等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个标准的Spring配置文件applicationContext.xml应该包含的基本组成部分如下:

0、框架配置

<?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:security="http://www.springframework.org/schema/security" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">

<bean></bean>
<bean></bean>
<--后面可以有好多bean标签-->

</beans>

其中会有好多配置:

1、激活配置

<context:annotation-config />
【概述】:激活spring的bean;
【详解】:<context:annotation-config/>的作用是向Spring容器注册以下四个BeanPostProcessor:
AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor –>
用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解。每次配置applicationContext.xml文件的时候,可以直接复制到自己的spring配置文件里;

2、 扫描配置

<context:component-scan base-package="com.smf.platform,com.smf.pdms"/>
【概括】:扫面指定目录,注册创建javabean;
【详解】: 除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的package下扫描以及注册Javabean ;说白了,就是扫描你指定项目文件夹目录里,含有@Controller,@Service,@RequestMapping等注解,并注册创建JavaBean;@每次配置applicationContext.xml文件的时候,该标签可以直接复制到自己的spring配置文件里;

3、代理配置

代理

<aop:aspectj-autoproxy/>

【概括】:代理
【详解】: 自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面;在配置自己的applicationContext.xml文件的时候,可以直接复制;

4、读取外部配置文件配置

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
</bean>

【概括】:读取外部配置文件;
【详解】:这里的jdbc.propertie是和src平级的conf文件夹内的配置文件;

5、数据源配置

数据源就是你数据库的一些连接信息,基本配置如下,官网点我

    <bean id="abcDataSource" 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}"/>
        <property name="maxActive" value="150"/>
        <property name="maxIdle" value="10"/>
        <property name="maxWait" value="6000"/>
        <property name="testOnBorrow" value="true"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="validationQuery" value="select 1 from dual"/>
    </bean>

这里的${} 里的内容是jdbc.proporities配置文件里的键值对中的key;
driverClassName:驱动类型名字,例如oracle.jdbc.driver.OracleDriver;
url:数据库地址;
username:数据库登录名;
password:数据库登录密码;
maxActive:数据库连接池中最大活跃线程数目,0为不限制;
maxIdle:表示最大空闲数目;
maxWait:最大等待时间,单位毫秒;
defaultAutoCommit:是否自动提交;
testOnBorrow:
validationQuery:连接数据库成功后,用一个最简单的句子验证查询;
这里写图片描述

这里的数据源配置还有另一种写法,那就是:

<bean id="abcDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName">
             <value>${jdbc.driverClassName}</value>
        </property>
        <property name="url">
             <value>${jdbc.url}</value>
        </property>
        <property name="username">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
        <property name="maxActive">
            <value>150</value>
        </property>
        <property name="maxIdle">
            <value>10</value>
        </property>
        <property name="maxWait">
            <value>6000</value>
        </property>
        <property name="testOnBorrow">
            <value>true</value>
        </property>
        <property name="defaultAutoCommit">
            <value>true</value>
        </property>
        <property name="validationQuery">
            <value>select 1 from dual</value>
        </property>
    </bean>

6、Mybatis配置信息

    <!-- MyBatis配置开始 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="testDataSource" />
        <property name="configLocation" value="classpath:mybatis.xml" />
    </bean>

    <!-- 自动扫描mapper接口 -->
    <bean name="testdao" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.test.**.dao"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>

    <bean id="testTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="testDataSource" />
    </bean>
    <!-- MyBatis配置结束 -->

7、JPA配置

JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。持久层配置文件一般为persistence.xml;持久层一般会对各个类型数据库进行配置,Oracle,SQL Server,MySQL方言(Dialect)的配置

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="abcDataSource" />
        <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
        <property name="persistenceUnitName" value="persistenceUnit" />
        <property name="jpaDialect" ref="jpaDialect"/>
    </bean>

    <bean id="jpaDialect" class="com.smf.platform.framework.dialect.AbcHibernateJpaDialect" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory">
            <ref bean="entityManagerFactory" />
        </property>
        <property name="dataSource" ref="smfDataSource" />
    </bean> 

8、事务配置

<bean name="transactionAttributesSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
        <property name="properties">
            <props>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="import*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="export*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="submit*">PROPAGATION_REQUIRED</prop>
                <prop key="create*">PROPAGATION_REQUIRED</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="modify*">PROPAGATION_REQUIRED</prop>
                <prop key="delete*">PROPAGATION_REQUIRED</prop>
                <prop key="remove*">PROPAGATION_REQUIRED</prop>
                <prop key="restore*">PROPAGATION_REQUIRED</prop>
                <prop key="do*">PROPAGATION_REQUIRED</prop>
                <prop key="execute*">PROPAGATION_REQUIRED</prop>
                <prop key="debug">PROPAGATION_REQUIRES_NEW</prop>
                <prop key="info">PROPAGATION_REQUIRES_NEW</prop>
                <prop key="error">PROPAGATION_REQUIRES_NEW</prop>
                <prop key="fatal">PROPAGATION_REQUIRES_NEW</prop>
                <prop key="warn">PROPAGATION_REQUIRES_NEW</prop>
            </props>
        </property>
    </bean>

9、事务管理

    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager">
            <ref bean="transactionManager"/>
        </property>
        <property name="transactionAttributeSource">
            <ref bean="transactionAttributesSource"/>
        </property>
    </bean>

    <bean id="autoTxProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="interceptorNames">
            <list>
                <idref bean="transactionInterceptor"/>
            </list>
        </property>
        <property name="beanNames">  
              <value>*Service,*Provider</value>
        </property>  
    </bean>

    <bean id="simpleJdbcDaoSupport"
        class="org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport">
        <property name="dataSource" ref="abcDataSource" />
    </bean>

    <bean id="dao" class="com.dao.impl.GenericDaoImpl">
    </bean>

     <bean id="template" class="com.dao.impl.GenericTemplateImpl">
        <property name="daoSupport" ref="simpleJdbcDaoSupport"/>
    </bean>

10、pfLogService配置

<bean id="pfLogService" class="com.smf.platform.log.service.impl.PFLogServiceImpl">
        <property name="appender">
            <bean class="com.smf.platform.log.appender.LogPersistAppender">
                <property name="logOwnerProvider">
                    <bean class="com.smf.platform.system.service.impl.LogOwnerProviderImpl"></bean>
                </property>
            </bean>
        </property>
    </bean>

11、其它配置

    <bean id="springConfigTool" class="com.smf.platform.framework.SpringConfigTool"></bean>

    <bean id="systemConfig" class="com.smf.platform.framework.SystemConfig"></bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陶洲川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值