spring配置

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标签前的配置:

  1. <bean name=" classicBean"class="com.example.TestBean">
  2. <property name="email" value="577@qq.com"/>
  3. </bean>

使用p标签简化后:

<bean name=" p-namespaceBean"class="com.example.TestBean" p:email="577@qq.com"/> 

————————————————————————————————————————

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>

关于前后置常用通知的具体细节,以前博客中有介绍:

http://blog.csdn.net/weixin_36380516/article/details/72551678

————————————————————————————————————————

xmlns:tx=http://www.springframework.org/schema/tx

xmlns:tx启动声明式事务时的命名空间

————————————————————————————————————————

         xmlns:c=http://www.springframework.org/schema/c

xmlns:cp标签一样,都是为了简化spring的配置的,使用c标签时要开启这个空间,使c标签能够识别

比如:传统配置方法:

  1. <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>

使用c标签后:

  1. <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 "/>

————————————————————————————————————————

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>等标签,用它来取代ListFactoryBeanMapFactoryBeanSetFactoryBeanPropertiesFactoryBean

用它来取代ListFactoryBeanMapFactoryBeanSetFactoryBeanPropertiesFactoryBean

         如:使用<util:property-path>标签为某个Bean的属性成员设置id属性,使之在容器管理中,而不必设置org.springframework.beans.factory.config.PropertyPathFactoryBean

  1. <util:property-path id="PI"
  2. path= "user.pi"/>

id值设置为PI的Bean,其值将会是user.pi

————————————————————————————————————————

xmlns:task=http://www.springframework.org/schema/task

xmlns:task开启配置定时器空间,spring框架提供了对定时器的支持,通过配置文件就可以很好的实现定时器,只需要应用启动,就自动启动定时器。

关于spring中定时器的使用,上一篇博客有介绍:

http://blog.csdn.net/weixin_36380516/article/details/72596834

————————————————————————————————————————

         xmlns:oxm=http://www.springframework.org/schema/oxm

xmlns:oxm开启OXM进行对象XML映射的空间,Spring OXM对主流O/X Mapping框架做了一个统一的抽象和封装,MarshallerUnmarshallerSpring OXM两个核心接口。Marshaller用于将对象转成XMLUnmarshaller用于将XML转成对象。

————————————————————————————————————————

         xmlns:mvc=http://www.springframework.org/schema/mvc

xmlns:mvc当使用springMVC开发Web项目时,就要开启这个命名空间

————————————————————————————————————————

         xmlns:lang=http://www.springframework.org/schema/lang

xmlns:lang lang用来将那些已经被定义在一些动态语言(例如JrubyGroovy)中的对象作为beans中的对象存放到spring容器中。展示还没有用到过。。

————————————————————————————————————————

         xmlns:jms=http://www.springframework.org/schema/jms

xmlns:jmsspring集成jms时需要开启的空间,是springjms的封装,用来简化异步接收消息的代码。官方文档:

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文件,说明每一步配置的作用:

  1. <?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. http://www.springframework.org/schema/tx/spring-tx.xsd">
  45. <!-- 数据库信息数据源 -->
  46. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  47. <!-- 指定连接数据库的驱动 -->
  48. <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
  49. </property>
  50. <!-- 连接数据库的url -->
  51. <property name="url" value="jdbc:oracle:thin:@localhost:1521:dalin">
  52. </property>
  53. <!-- 用户名密码 -->
  54. <property name="username" value="ssh"> </property>
  55. <property name="password" value="123"> </property>
  56. <!-- 设置数据库连接池的最大连接数 -->
  57. <property name="maxPoolSize">
  58. <value>20 </value>
  59. </property>
  60. <!-- 设置数据库连接池的最小连接数 -->
  61. <property name="minPoolSize">
  62. <value>2 </value>
  63. </property>
  64. <!-- 设置数据库连接池的初始化连接数 -->
  65. <property name="initialPoolSize">
  66. <value>2 </value>
  67. </property>
  68. <!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->
  69. <property name="maxIdleTime">
  70. <value>10 </value>
  71. </property>
  72. </bean>
  73. <!-- 定义sessionFactory -->
  74. <bean id="sessionFactory"
  75. class= "org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  76. <!-- 注入数据源 -->
  77. <property name="dataSource">
  78. <ref bean="dataSource" />
  79. </property>
  80. <property name="hibernateProperties">
  81. <props>
  82. <prop key="hibernate.dialect">
  83. org.hibernate.dialect.Oracle9Dialect
  84. </prop>
  85. <!-- <prop key="hibernate.show_sql">true</prop> -->
  86. </props>
  87. </property>
  88. <!-- hibernate的映射文件,这个自动生成,一般不改动 -->
  89. <property name="mappingResources">
  90. <list>
  91. <value>org/jvsun/pojo/Buy.hbm.xml </value>
  92. <value>org/jvsun/pojo/BuyDetail.hbm.xml </value>
  93. </list>
  94. </property> </bean>
  95. <!-- 采购单配置开始 -->
  96. <bean id="BuyAction" class="org.jvsun.action.BuyAction">
  97. <property name="services" ref="BuyServices"> </property>
  98. </bean>
  99. <bean id="BuyServices" class="org.jvsun.services.impl.BuyServicesImpl">
  100. <property name="dao" ref="BuyDAO"> </property>
  101. </bean>
  102. <bean id="BuyDAO" class="org.jvsun.dao.impl.BuyDAOImpl">
  103. <property name="sessionFactoy" ref="sessionFactory"> </property>
  104. </bean>
  105. <!-- 采购单配置结束 -->
  106. <!-- 定义使用的事务管理器 -->
  107. <bean id="transactionManager"
  108. class= "org.springframework.orm.hibernate4.HibernateTransactionManager">
  109. <property name="sessionFactory" ref="sessionFactory" />
  110. </bean>
  111. <!-- 定义需要进行事务拦截的方法及所采用的事务控制类型 -->
  112. <!--propagation="REQUIRED",事务的衍生方式为必需,即事务的传播方式。有则用现成事务无则创建新的-->
  113. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  114. <tx:attributes>
  115. <tx:method name="do*" propagation="REQUIRED" />
  116. <tx:method name="find*" propagation="REQUIRED" />
  117. <!-- 这表明仅对do和find开头的方法进行事务控制,REQUIRED声明这些如果当前没有事务就去创建一个新的,如果有的话就用当前事务 -->
  118. </tx:attributes>
  119. </tx:advice>
  120. <!-- 声明一个切入点 -->
  121. <aop:config>
  122. <aop:pointcut id="productServiceMethods"
  123. expression= "execution(* org.jvsun.services.impl.*.*(..))" />
  124. <aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
  125. </aop:config>
  126. <!--
  127. execution切入点指示符 ,用法稍后说
  128. -->
  129. <!-- 声明支持事务注解的(@Transactional) -->
  130. <tx:annotation-driven transaction-manager="transactionManager" />
  131. </beans>

切入点表达式说明:

execution切入点指示符

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)

除了返回类型模式(上面代码片断中的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..*.*(..))

大概就这么多了,欢迎大家补充。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值