• 关于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..*.*(..))
    ---------------------
    作者:阿木侠
    来源:CSDN
    原文:https://blog.csdn.net/weixin_36380516/article/details/72851690
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    ESP8266简单几步建立服务器
    SVM推导
    标准的最大margin问题
    switch用法
    vecor预分配内存溢出2
    vector预分配空间溢出
    [面试编程题]算法基础-字符移位
    [面试编程题1]构造回文
    一天学完UFLDL
    神经网络中的XOR问题
  • 原文地址:https://www.cnblogs.com/anyiz/p/10712323.html
Copyright © 2020-2023  润新知