内容简介:彻底搞懂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标签前的配置:
<bean name=" classicBean"class="com.example.TestBean"> <property name="email" value="577@qq.com"/> </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:c : 和 p 标签一样,都是为了简化 spring 的配置的,使用 c 标签时要开启这个空间,使 c 标签能够识别
比如:传统配置方法:
<bean id="bar"class="x.y.Bar"/> <bean id="baz"class="x.y.Baz"/> <bean id="foo"class="x.y.Foo"> <constructor-arg ref="bar"/> <constructor-arg ref="baz"/> <constructor-arg value="577@qq.com /> </bean>
使用c标签后:
<bean id="bar"class="x.y.Bar"/> <bean id="baz"class="x.y.Baz"/> <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> 等标签,用它来取代 ListFactoryBean 、 MapFactoryBean 、 SetFactoryBean 、 PropertiesFactoryBean 。
用它来取代 ListFactoryBean 、 MapFactoryBean 、 SetFactoryBean 、 PropertiesFactoryBean 。
如:使用<util:property-path>标签为某个Bean的属性成员设置id属性,使之在容器管理中,而不必设置org.springframework.beans.factory.config.PropertyPathFactoryBean
<util:property-path id="PI" 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 框架做了一个统一的抽象和封装, 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文件,说明每一步配置的作用:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-4.1.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 数据库信息数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!-- 指定连接数据库的驱动 -->
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver">
</property>
<!-- 连接数据库的url -->
<property name="url" value="jdbc:oracle:thin:@localhost:1521:dalin">
</property>
<!-- 用户名密码 -->
<property name="username" value="ssh"></property>
<property name="password" value="123"></property>
<!-- 设置数据库连接池的最大连接数 -->
<property name="maxPoolSize">
<value>20</value>
</property>
<!-- 设置数据库连接池的最小连接数 -->
<property name="minPoolSize">
<value>2</value>
</property>
<!-- 设置数据库连接池的初始化连接数 -->
<property name="initialPoolSize">
<value>2</value>
</property>
<!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->
<property name="maxIdleTime">
<value>10</value>
</property>
</bean>
<!-- 定义sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<!-- <prop key="hibernate.show_sql">true</prop> -->
</props>
</property>
<!-- hibernate的映射文件,这个自动生成,一般不改动 -->
<property name="mappingResources">
<list>
<value>org/jvsun/pojo/Buy.hbm.xml</value>
<value>org/jvsun/pojo/BuyDetail.hbm.xml</value>
</list>
</property></bean>
<!-- 采购单配置开始 -->
<bean id="BuyAction" class="org.jvsun.action.BuyAction">
<property name="services" ref="BuyServices"></property>
</bean>
<bean id="BuyServices" class="org.jvsun.services.impl.BuyServicesImpl">
<property name="dao" ref="BuyDAO"></property>
</bean>
<bean id="BuyDAO" class="org.jvsun.dao.impl.BuyDAOImpl">
<property name="sessionFactoy" ref="sessionFactory"></property>
</bean>
<!-- 采购单配置结束 -->
<!-- 定义使用的事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 定义需要进行事务拦截的方法及所采用的事务控制类型 -->
<!--propagation="REQUIRED",事务的衍生方式为必需,即事务的传播方式。有则用现成事务无则创建新的-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="find*" propagation="REQUIRED" />
<!-- 这表明仅对do和find开头的方法进行事务控制,REQUIRED声明这些如果当前没有事务就去创建一个新的,如果有的话就用当前事务 -->
</tx:attributes>
</tx:advice>
<!-- 声明一个切入点 -->
<aop:config>
<aop:pointcut id="productServiceMethods"
expression="execution(* org.jvsun.services.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
</aop:config>
<!--
execution切入点指示符 ,用法稍后说
-->
<!-- 声明支持事务注解的(@Transactional) -->
<tx:annotation-driven transaction-manager="transactionManager" />
</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..*.*(..))
大概就这么多了,欢迎大家补充。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
图片转BASE64编码
在线图片转Base64编码工具
SHA 加密
SHA 加密工具