spring配置文档.docx
《spring配置文档.docx》由会员分享,可在线阅读,更多相关《spring配置文档.docx(11页珍藏版)》请在冰豆网上搜索。
![spring配置文档.docx](https://file1.bdocx.com/fileroot1/2023-1/31/c93efddd-403c-4def-af04-dfc58858e62d/c93efddd-403c-4def-af04-dfc58858e62d1.gif)
spring配置文档
详解Spring的配置文件
1.基本配置:
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="http:
//www.springframework.org/schema/beans"
xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
xmlns:
context="http:
//www.springframework.org/schema/context"
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
">
component-scanbase-package="com.persia">
--开启组件扫描-->
component-scan>
annotation-config>
--开启注解处理器-->
annotation-config>
--使用注解,省去了propertity的xml配置,减少xml文件大小-->
--自动注解-->
--由spring容器去创建和维护,我们只要获取就可以了-->
init-method="init"destroy-method="destory">
--静态工厂获取bean-->
--实例工厂获取bean,先实例化工厂再实例化bean-->
--ref方式注入属性-->
--内部bean方式注入-->
--集合的注入-->
第一个
第二个
第三个
--集合的注入-->
第一个l
第二个l
第三个l
value1
value2
value3
--基本类型可以不写type-->
2.开启AOP:
xmlversion="1.0"encoding="UTF-8"?
>
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"
xsi:
schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-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/context
http:
//www.springframework.org/schema/context/spring-context-2.5.xsd
">
aspectj-autoproxy>
aspectj-autoproxy>
AOP的xml版本
xmlversion="1.0"encoding="UTF-8"?
>
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"
xsi:
schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-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/context
http:
//www.springframework.org/schema/context/spring-context-2.5.xsd
">
aspectj-autoproxy>
aspectj-autoproxy>
config>
aspectid="myaop"ref="aspectBean">
pointcutid="mycut"expression="execution(*com.persia.service.impl.PersonServiceImpl.*(..))"/>
pointcutid="argcut"expression="execution(*com.persia.service.impl.PersonServiceImpl.*(..))andargs(name)"/>
beforepointcut-ref="mycut"method="doAccessCheck"/>
after-returningpointcut-ref="mycut"method="doAfterReturning"/>
after-throwingpointcut-ref="mycut"method="doThrowing"/>
afterpointcut-ref="argcut"method="doAfter"arg-names="name"/>
aroundpointcut-ref="mycut"method="arround"/>
aspect>
config>
3.开启事务和注解:
xmlversion="1.0"encoding="UTF-8"?
>
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"
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/txhttp:
//www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
aspectj-autoproxy>
aspectj-autoproxy>
--配置数据源-->
mysql:
//localhost:
3306/test?
useUnicode=true&characterEncoding=utf-8"/>
--连接池启动时的初始值-->
--连接池的最大值-->
--最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止-->
--最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请-->
--配置事务管理器-->
--配置业务bean-->
--采用@Transactional注解方式来使用事务-->
annotation-driventransaction-manager="txManager"/>
XML版本:
xmlversion="1.0"encoding="UTF-8"?
>
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"
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/txhttp:
//www.springframework.org/schema/tx/spring-tx-2.5.xsd
">
aspectj-autoproxy>
aspectj-autoproxy>
--配置数据源-->
mysql:
//localhost:
3306/test?
useUnicode=true&characterEncoding=utf-8"/>
--连接池启动时的初始值-->
--连接池的最大值-->
--最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止-->
--最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请-->
--配置事务管理器-->
--配置业务bean-->
--使用XML来使用事务管理-->
config>
--配置一个切面,和需要拦截的类和方法-->
pointcutid="transactionPointcut"expression="execution(*com.persia.service..*.*(..))"/>
advisoradvice-ref="txAdvice"pointcut-ref="transactionPointcut"/>
config>
--配置一个事务通知-->
adviceid="txAdvice"transaction-manager="txManager">
attributes>
--方法以get开头的,不使用事务-->
methodname="get*"read-only="true"propagation="NOT_SUPPORTED"/>
--其他方法以默认事务进行-->
methodname="*"/>
attributes>
advice>
4.SSH:
xmlversion="1.0"encoding="UTF-8"?
>
xmlns="http:
//www.springf