spring比较庞大,很多功能实现依赖配置文件,比较繁琐的配置文件确实比较头疼,这里通过查阅,上网等方法总结了关于spring配置文件的内容,如果有不全或者失误之处希望大家多多指正。
<beans 这里是配置文件的根节点,所有配置在beans中,内可以包含多个bean
xmlns=http://www.springframework.org/schema/beans
xmlns:是XML NameSpace的缩写,因为XML文件的标签名称都是自定义的,自己写的和其他人定义的标签很有可能会重复命名,而功能却不一样,所以需要加上一个namespace来区分这个xml文件和其他的xml文件,类似于Java中的package。
————————————————————————————————————————
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:xsi: 是指xml文件遵守xml规范,xsi全名:xml schema instance,是指具体用到的schema资源文件里定义的元素所准守的规范。即http://www.w3.org/2001/XMLSchema-instance这个文件里定义的元素遵守什么标准
上面两个是最基本的命名空间,必不可少的
————————————————————————————————————————
xmlns:p=http://www.springframework.org/schema/p
xmlns:p:工程中为了简化配置,使用p标签时,就需要开启这个命名空间,开启后才能识别p标签的配置
如:使用p标签前的配置:
[html] view plain copy
1. <span style="font-size:18px;"><bean name=" classicBean"class="com.example.TestBean">
2. <property name="email" value="577@qq.com"/>
3. </bean></span>
使用p标签简化后:
[html] view plain copy
1. <span style="font-size:18px;"><bean name=" p-namespaceBean"class="com.example.TestBean" p:email="577@qq.com"/> </span>
————————————————————————————————————————
xmlns:aop=http://www.springframework.org/schema/aop
xmlns:aop:启用AOP功能时的命名空间,使用切面等都要用到,spring的核心之一,一般情况都会启动的命名空间
常用到的spring的声明通知:
前置通知:<aop:before>
后置通知:<aop:after-returning>
异常通知:<aop:after-throwing>
最终通知:<aop:after>
环绕通知:<aop:around>
关于前后置常用通知的具体细节,以前博客中有介绍:
————————————————————————————————————————
xmlns:tx=http://www.springframework.org/schema/tx
xmlns:tx:启动声明式事务时的命名空间
————————————————————————————————————————
xmlns:c=http://www.springframework.org/schema/c
xmlns:c:和p标签一样,都是为了简化spring的配置的,使用c标签时要开启这个空间,使c标签能够识别
比如:传统配置方法:
[html] view plain copy
1. <span style="font-size:18px;"><bean id="bar"class="x.y.Bar"/>
2. <bean id="baz"class="x.y.Baz"/>
3. <bean id="foo"class="x.y.Foo">
4. <constructor-arg ref="bar"/>
5. <constructor-arg ref="baz"/>
6. <constructor-arg value="577@qq.com />
7. </bean></span>
使用c标签后:
[html] view plain copy
1. <span style="font-size:18px;"><bean id="bar"class="x.y.Bar"/>
2. <bean id="baz"class="x.y.Baz"/>
3. <bean id="foo"class="x.y.Foo" c:bar-ref="bar" c:baz-ref="baz"c:email="577@qq.com "/></span>
————————————————————————————————————————
xmlns:cache=http://www.springframework.org/schema/cache
xmlns:cache:开启缓存标注空间,Spring框架提供了大量的缓存相关的标注,在应用中通过使用这些缓存标注实现缓存。要使用缓存标注,首先需要配置开启缓存标注空间。
开启后在应用中就可以引用Spring框架的缓存标注,如:
@Cacheable("a_cache_name"),作用于被缓冲的方法,对方法执行结果的缓存
@CacheEvict,作用于被缓冲的方法,将方法执行的结果从缓存中移除
@CachePut,更新缓存
@Caching,将作用于一个方法的多个缓存操作打包为一个整体
@CacheConfig,作用于Java类,设置通用的缓存相关参数
————————————————————————————————————————
xmlns:util=http://www.springframework.org/schema/util
xmlns:util:意思是开启util空间,util标签可以进行简化操作,使用<util:list>、<util:map>、<util:set>、<util:properties>等标签,用它来取代ListFactoryBean、MapFactoryBean、SetFactoryBean、PropertiesFactoryBean。
用它来取代ListFactoryBean、MapFactoryBean、SetFactoryBean、PropertiesFactoryBean。
如:使用<util:property-path>标签为某个Bean的属性成员设置id属性,使之在容器管理中,而不必设置org.springframework.beans.factory.config.PropertyPathFactoryBean
[java] view plain copy
1. <span style="font-size:18px;"><util:property-path id="PI"
2. path="user.pi"/></span>
id值设置为PI的Bean,其值将会是user.pi
————————————————————————————————————————
xmlns:task=http://www.springframework.org/schema/task
xmlns:task:开启配置定时器空间,spring框架提供了对定时器的支持,通过配置文件就可以很好的实现定时器,只需要应用启动,就自动启动定时器。
关于spring中定时器的使用,上一篇博客有介绍:
————————————————————————————————————————
xmlns:oxm=http://www.springframework.org/schema/oxm
xmlns:oxm:开启OXM进行对象XML映射的空间,Spring OXM对主流O/X Mapping框架做了一个统一的抽象和封装,Marshaller和Unmarshaller是Spring OXM两个核心接口。Marshaller用于将对象转成XML,Unmarshaller用于将XML转成对象。
————————————————————————————————————————
xmlns:mvc=http://www.springframework.org/schema/mvc
xmlns:mvc:当使用springMVC开发Web项目时,就要开启这个命名空间
————————————————————————————————————————
xmlns:lang=http://www.springframework.org/schema/lang
xmlns:lang :lang用来将那些已经被定义在一些动态语言(例如Jruby和Groovy)中的对象作为beans中的对象存放到spring容器中。展示还没有用到过。。
————————————————————————————————————————
xmlns:jms=http://www.springframework.org/schema/jms
xmlns:jms:spring集成jms时需要开启的空间,是spring对jms的封装,用来简化异步接收消息的代码。官方文档:
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jms/core/JmsTemplate.html
————————————————————————————————————————
xmlns:jee=http://www.springframework.org/schema/jee
xmlns:jee :开启jee标签空间,jee标签用来处理javaee标准相关的问题,例如查询一个jndi对象以及定义一个ejb的引用等。
一般使用org.springframework.jndi.JndiObjectFactoryBean方法配置datasource数据源的时候开启这个命名空间。
————————————————————————————————————————
xmlns:jdbc=http://www.springframework.org/schema/jdbc
xmlns:jdbc:开启jdbc的命名空间,之后配置文件中可以使用jdbc标签了,例如:
配置<jdbc:initialize-database>标签,在spring工程启动时,去执行一些sql,也就是初始化数据库。比如向数据库中建立一些表及插入一些初始数据等。sql的路径需要在其子标签jdbc:script中去指定。
————————————————————————————————————————
xmlns:context=http://www.springframework.org/schema/context
xmlns:context:开启上下文标签,可以用来为spring配置文件引入其他地方的配置,如,最常见的调用数据库配置db.properties文件:
<context:property-placeholderlocation="classpath:db.properties"/>
————————————————————————————————————————
xsi:schemaLocation="http://www.springframework.org/schema/oxm
xsi:schemaLocation:是指本文档里的xml元素所遵守的规范,这些规范都是由官方制定的,可以进你写的网址里面看版本的变动。xsd的网址还可以帮助你判断使用的代码是否合法。
下面根据一份applicationContext.xml文件,说明每一步配置的作用:
[html] view plain copy
1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
2. <beans
3. xmlns="http://www.springframework.org/schema/beans"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xmlns:p="http://www.springframework.org/schema/p"
6. xmlns:aop="http://www.springframework.org/schema/aop"
7. xmlns:tx="http://www.springframework.org/schema/tx"
8. xmlns:c="http://www.springframework.org/schema/c"
9. xmlns:cache="http://www.springframework.org/schema/cache"
10. xmlns:util="http://www.springframework.org/schema/util"
11. xmlns:task="http://www.springframework.org/schema/task"
12. xmlns:oxm="http://www.springframework.org/schema/oxm"
13. xmlns:mvc="http://www.springframework.org/schema/mvc"
14. xmlns:lang="http://www.springframework.org/schema/lang"
15. xmlns:jms="http://www.springframework.org/schema/jms"
16. xmlns:jee="http://www.springframework.org/schema/jee"
17. xmlns:jdbc="http://www.springframework.org/schema/jdbc"
18. xmlns:context="http://www.springframework.org/schema/context"
19. xsi:schemaLocation="http://www.springframework.org/schema/oxm
20. http://www.springframework.org/schema/oxm/spring-oxm-4.1.xsd
21. http://www.springframework.org/schema/jms
22. http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
23. http://www.springframework.org/schema/mvc
24. http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
25. http://www.springframework.org/schema/jee
26. http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
27. http://www.springframework.org/schema/context
28. http://www.springframework.org/schema/context/spring-context-4.1.xsd
29. http://www.springframework.org/schema/aop
30. http://www.springframework.org/schema/aop/spring-aop.xsd
31. http://www.springframework.org/schema/util
32. http://www.springframework.org/schema/util/spring-util-4.1.xsd
33. http://www.springframework.org/schema/jdbc
34. http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
35. http://www.springframework.org/schema/cache
36. http://www.springframework.org/schema/cache/spring-cache-4.1.xsd
37. http://www.springframework.org/schema/task
38. http://www.springframework.org/schema/task/spring-task-4.1.xsd
39. http://www.springframework.org/schema/beans
40. http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
41. http://www.springframework.org/schema/lang
42. http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
43. http://www.springframework.org/schema/tx
44. >
45.
46. <!-- 数据库信息数据源 -->
47. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
48.
49. <!-- 指定连接数据库的驱动 -->
50. <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
51. </property>
52.
53. <!-- 连接数据库的url -->
54. <property name="url" value="jdbc:oracle:thin:@localhost:1521:dalin">
55. </property>
56.
57. <!-- 用户名密码 -->
58. <property name="username" value="ssh"></property>
59. <property name="password" value="123"></property>
60.
61. <!-- 设置数据库连接池的最大连接数 -->
62. <property name="maxPoolSize">
63. <value>20</value>
64. </property>
65.
66. <!-- 设置数据库连接池的最小连接数 -->
67. <property name="minPoolSize">
68. <value>2</value>
69. </property>
70.
71. <!-- 设置数据库连接池的初始化连接数 -->
72. <property name="initialPoolSize">
73. <value>2</value>
74. </property>
75.
76. <!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->
77. <property name="maxIdleTime">
78. <value>10</value>
79. </property>
80. </bean>
81.
82. <!-- 定义sessionFactory -->
83. <bean id="sessionFactory"
84. class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
85. <!-- 注入数据源 -->
86. <property name="dataSource">
87. <ref bean="dataSource" />
88. </property>
89. <property name="hibernateProperties">
90. <props>
91. <prop key="hibernate.dialect">
92. org.hibernate.dialect.Oracle9Dialect
93. </prop>
94. <!-- <prop key="hibernate.show_sql">true</prop> -->
95. </props>
96. </property>
97. <!-- hibernate的映射文件,这个自动生成,一般不改动 -->
98. <property name="mappingResources">
99. <list>
100. <value>org/jvsun/pojo/Buy.hbm.xml</value>
101. <value>org/jvsun/pojo/BuyDetail.hbm.xml</value>
102. </list>
103. </property></bean>
104. <!-- 采购单配置开始 -->
105. <bean id="BuyAction" class="org.jvsun.action.BuyAction">
106. <property name="services" ref="BuyServices"></property>
107. </bean>
108. <bean id="BuyServices" class="org.jvsun.services.impl.BuyServicesImpl">
109. <property name="dao" ref="BuyDAO"></property>
110. </bean>
111. <bean id="BuyDAO" class="org.jvsun.dao.impl.BuyDAOImpl">
112. <property name="sessionFactoy" ref="sessionFactory"></property>
113. </bean>
114. <!-- 采购单配置结束 -->
115.
116. <!-- 定义使用的事务管理器 -->
117. <bean id="transactionManager"
118. class="org.springframework.orm.hibernate4.HibernateTransactionManager">
119. <property name="sessionFactory" ref="sessionFactory" />
120. </bean>
121.
122. <!-- 定义需要进行事务拦截的方法及所采用的事务控制类型 -->
123. <!--propagation="REQUIRED",事务的衍生方式为必需,即事务的传播方式。有则用现成事务无则创建新的-->
124. <tx:advice id="txAdvice" transaction-manager="transactionManager">
125. <tx:attributes>
126. <tx:method name="do*" propagation="REQUIRED" />
127. <tx:method name="find*" propagation="REQUIRED" />
128. <!-- 这表明仅对do和find开头的方法进行事务控制,REQUIRED声明这些如果当前没有事务就去创建一个新的,如果有的话就用当前事务 -->
129. </tx:attributes>
130. </tx:advice>
131.
132.
133.
134. <!-- 声明一个切入点 -->
135. <aop:config>
136. <aop:pointcut id="productServiceMethods"
137. expression="execution(* org.jvsun.services.impl.*.*(..))" />
138. <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
139. </aop:config>
140. <!--
141. execution切入点指示符 ,用法稍后说
142. -->
143.
144.
145. <!-- 声明支持事务注解的(@Transactional) -->
146. <tx:annotation-driven transaction-manager="transactionManager" />
147. </beans></span>
切入点表达式说明:
execution切入点指示符
[java] view plain copy
1. <span style="font-size:18px;">execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)</span>
除了返回类型模式(上面代码片断中的ret-type-pattern),名字模式和参数模式以外, 所有的部分都是可选的
参数模式稍微有点复杂:
1,()匹配了一个不接受任何参数的方法
2,(..)匹配了一个接受任意数量参数的方法(零或者更多)
3,模式(*)匹配了一个接受一个任何类型的参数的方法
4,模式(*,String)匹配了一个接受两个参数的方法,第一个可以是任意类型, 第二个则必须是String类型
例如:
任意公共方法的执行:
execution(public * *(..))
任何一个名字以“set”开始的方法的执行:
execution(* set*(..))
AccountService接口定义的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
在service包中定义的任意方法的执行:
execution(* com.xyz.service.*.*(..))
在service包或其子包中定义的任意方法的执行:
execution(* com.xyz.service..*.*(..))