一个标准的Spring配置文件applicationContext.xml应该包含的基本组成部分如下:
<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>
其中会有好多配置:
【概述】:激活spring的bean;
【详解】:
的作用是向Spring容器注册以下四个BeanPostProcessor:
AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor –>
用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解。每次配置applicationContext.xml文件的时候,可以直接复制到自己的spring配置文件里;
【概括】:扫面指定目录,注册创建javabean;
【详解】: 除了具有
的功能之外,
还可以在指定的package下扫描以及注册Javabean ;说白了,就是扫描你指定项目文件夹目录里,含有@Controller,@Service,@RequestMapping等注解,并注册创建JavaBean;@每次配置applicationContext.xml文件的时候,该标签可以直接复制到自己的spring配置文件里;
代理
<aop:aspectj-autoproxy/>
【概括】:代理
【详解】: 自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面;在配置自己的applicationContext.xml文件的时候,可以直接复制;
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>
classpath:jdbc.properties
list>
property>
【概括】:读取外部配置文件;
【详解】:这里的jdbc.propertie是和src平级的conf文件夹内的配置文件;
数据源就是你数据库的一些连接信息,基本配置如下,官网点我
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"/>
这里的${}
里的内容是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>150value>
property>
<property name="maxIdle">
<value>10value>
property>
<property name="maxWait">
<value>6000value>
property>
<property name="testOnBorrow">
<value>truevalue>
property>
<property name="defaultAutoCommit">
<value>truevalue>
property>
<property name="validationQuery">
<value>select 1 from dualvalue>
property>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="testDataSource" />
<property name="configLocation" value="classpath:mybatis.xml" />
bean>
<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>
JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。持久层配置文件一般为persistence.xml;持久层一般会对各个类型数据库进行配置,Oracle,SQL Server,MySQL方言(Dialect)的配置
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"/>
id="jpaDialect" class="com.smf.platform.framework.dialect.AbcHibernateJpaDialect" />
id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory">
<ref bean="entityManagerFactory" />
property>
<property name="dataSource" ref="smfDataSource" />
<bean name="transactionAttributesSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnlyprop>
<prop key="list*">PROPAGATION_REQUIRED,readOnlyprop>
<prop key="find*">PROPAGATION_REQUIRED,readOnlyprop>
<prop key="load*">PROPAGATION_REQUIRED,readOnlyprop>
<prop key="query*">PROPAGATION_REQUIRED,readOnlyprop>
<prop key="import*">PROPAGATION_REQUIRED,readOnlyprop>
<prop key="export*">PROPAGATION_REQUIRED,readOnlyprop>
<prop key="save*">PROPAGATION_REQUIREDprop>
<prop key="submit*">PROPAGATION_REQUIREDprop>
<prop key="create*">PROPAGATION_REQUIREDprop>
<prop key="update*">PROPAGATION_REQUIREDprop>
<prop key="modify*">PROPAGATION_REQUIREDprop>
<prop key="delete*">PROPAGATION_REQUIREDprop>
<prop key="remove*">PROPAGATION_REQUIREDprop>
<prop key="restore*">PROPAGATION_REQUIREDprop>
<prop key="do*">PROPAGATION_REQUIREDprop>
<prop key="execute*">PROPAGATION_REQUIREDprop>
<prop key="debug">PROPAGATION_REQUIRES_NEWprop>
<prop key="info">PROPAGATION_REQUIRES_NEWprop>
<prop key="error">PROPAGATION_REQUIRES_NEWprop>
<prop key="fatal">PROPAGATION_REQUIRES_NEWprop>
<prop key="warn">PROPAGATION_REQUIRES_NEWprop>
props>
property>
bean>
id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributesSource"/>
property>
id="autoTxProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
"transactionInterceptor"/>
list>
property>
<property name="beanNames">
*Service,*Provider
property>
id="simpleJdbcDaoSupport"
class="org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport">
<property name="dataSource" ref="abcDataSource" />
id="dao" class="com.dao.impl.GenericDaoImpl">
id="template" class="com.dao.impl.GenericTemplateImpl">
<property name="daoSupport" ref="simpleJdbcDaoSupport"/>
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>