spring学习笔记

做了一部分笔记后面的忘了做笔记了

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

    <!-- 通过bean元素声明需要Spring创建的实例。该实例的类型通过class属性指定,并通过id属性为该实例指定一个名称,以便于访问 -->
    <bean id="helloSpring" class="entity.HelloSpring">
        <!-- property元素用来为实例的属性赋值, 此处实际是调用setWho()方法实现赋值操作 -->
        <property name="who" value="大Spring"></property>
    </bean>

    <!-- 定义彩色墨盒bean,ID是colorInk -->
    <bean id="colorInk" class="UserTset.ColorInk"></bean>
    <!-- 定义灰色墨盒bean,ID是greyInk -->
    <bean id="greyInk" class="UserTset.GreyInk"></bean>
    <!-- 定义A4纸张bean,ID是a4Paper --> 
    <!-- 通过setsetCharPerLine()方法为charPerLine属性注入每行字符数 -->
    <!-- 通过setLinePerPage()方法为linePerPage属性注入每页行数 -->
    <bean id="a4Paper" class="UserTset.TextPaper">
        <property name="charPerLine" value="10" />
        <property name="linePerPage" value="8" />
    </bean>
    <!-- 定义b5纸张ID是b5Paper -->
    <bean id="b5Paper" class="UserTset.TextPaper">
        <property name="charPerLine" value="5" />
        <property name="linePerPage" value="6" />
    </bean>
    <!-- 组装打印机。定义打印机bean,该bean的ID是printer,class指定该bean的实现类 -->
    <bean id="printer" class="impl.Printer">
        <property name="ink" ref="greyInk"></property>
        <property name="paper" ref="b5Paper"></property>
    </bean>
    <!-- AOP面向切面编程基本概念: 切面:一个模块化的横切逻辑(或称横切关注点)可能会横切多个对象 。连接点:程序执行中的某个具体的执行点。 
        增强处理:切面在某个特定连接点上执行的逻辑代码 切入点:对连接点的特征进行描述,可以使用正则表达式。增强处理和一个切入点表达式关联,并在与这个切入点匹配的某个连接点上运行 
        目标对象:被一个或多个切面增强的对象 AOP代理:由AOP框架所创建的对象,实现执行增强处理方法等处理 织入:将增强处理连接到应用程序中的类型或对象的过程 
        增强处理类型: 其他包括后置增强,环绕增强,异常增强,最终增强 ;前置增强:在原方法之前插入的增强处理 ;后置增强:在原方法之后插入的增强处理 ;环绕增强:在原方法周围插入增强处理 
        异常增强:抛出异常后插入的增强处理 ;最终增强:方法执行完后插入的增强处理 -->
    <!-- AOP实现思路 在项目中添加Spring AOP的jar文件。 编写增强类型实现增强处理。 编写Spring配置文件,对业务方法进行增强处理 
        编写代码,获取带有增强处理的业务对象 -->
    <!-- 实例化一个user到impl -->
    <bean id="dao" class="impl.UserDaoImpl"></bean>
    <bean id="service" class="impl.UserServiceImpl">
        <!-- 为userserviceimpl类的dao属性赋值 set方式 -->
        <!-- <property name="dao" ref="dao"></property> -->
        <!-- 通过单参构造方法为userService的dao属性赋值 -->
        <!-- 1. <constructor-arg> 元素表示构造方法的一个参数,且使用时不区分顺序。当构造方法的参数出现混淆,无法区分时可以通过index属性指定该参数的位置索引,位置从0开始 
            <constructor-arg>元素还提供type属性用来指定参数的类型,避免字符串和基本数据类型混淆。 2.构造注入的实效性好,在对象实例化时就得到所依赖的对象,便于在对象的初始化方法中使用依赖对象;但受限于方法重载的形式,使用灵活性不足,设值注入使用灵活,但时效性不足,并且 
            大量的set访问器增加了类的复杂性。所以要看需求选择 -->
        <!-- 多种方式实现ioc注入 -->
        <!-- 构造注入 -->
        <constructor-arg>
            <!-- 引用ID为dao的对象作为userservice的dao属性赋值 -->
            <ref bean="dao" />
        </constructor-arg>
        <!-- p命名空间改造; 原: <bean id="b5Paper" class="UserTset.TextPaper"> <property 
            name="charPerLine" value="5" /> <property name="linePerPage" value="6" /> 
            </bean> 改: <bean id="b5Paper" class="UserTset.TextPaper"> p:charPerLine="5" 
            p:linePerPage="6" </bean> <bean id="service" class="impl.UserServiceImpl"> 
            P:dao-ref="dao" </bean> -->
        <!-- p命名空间语法: 对于直接量(基本数据类型,字符串)属性,使用方式:p:属性名="属性值" 对于引用bean的属性,使用方式:p:属性名-ref="bean的id" -->

    </bean>
    <bean id="theLogger" class="impl.UserServiceLogger"></bean>

    <!-- 与AOP相关配置都放在<aop:config>标签中,如配置切入点就是<aop:pointcut>标签。<aop:pointcut>标签中的expression属性可以配置切入点表达式. 
        expression是切入点指示符,它的括号是一个切入点表达式,可以配置需要切入增强处理的方法特征,切入点表达式支持模糊匹配,下面讲解几种常用的模糊匹配: 
        public * addNewUser(entity.User):"*"表示匹配 所有类型的返回值 ;public void *(entity.User):"*"表示匹配所有方法名; 
        public void addNewUser(..):".."表示匹配所有参数个数和类型 ;* com.service.*.*(..):这个表达式匹配com.service包下所有类的所有方法 ;
        * com.service..*.*(..):这个表达式匹配com.service包及其子包下所有类的所有方法 public void *(entity.User):"*"表示匹配所有 -->

    <aop:config>
        <!-- 定义一个切入点表达式,命名为pointcut,即找到需要增强的方法 -->
        <aop:pointcut id="pointcut"
            expression="execution(public void addNewUser(entity.User))" />
            <!--         抛出异常增强定义切点 -->
        <aop:pointcut expression="execution(impl.UserServiceImpl.*(..))" id="pointcutex"/>
        
        <!-- 引用需要进行增强的方法包的bean并进行增强方法的调用 -->
        <aop:aspect ref="theLogger">
            <!-- 将before()方法定义为前置增强并引用pointcut切入点 method:表示调用增强方法进行增强; pointcut-ref:引用要增强的方法,又叫切入点进行增强; -->
            <aop:before method="before" pointcut-ref="pointcut" />
            <!-- 定义afterReturning为后置增强 并引入切入点 -->
            <!-- 通过 returning属性指定为名为resuit的参数注入返回值 -->
            <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result" />
        <!-- 抛出异常增强 -->
<!--         将afterThrowing()方法定义为异常抛出增强并引用pointcut切入点 -->
<!-- 通过throwing属性指定为名为e的参数注入异常实例 -->
<aop:after-throwing method="afteThrowing" throwing="e" pointcut-ref="pointcut" />
<!-- 最终增强 -->
<!-- 将afterLogger()定义为最终增强并引用pointcut切入点 -->
<aop:after method="afterLogger" pointcut-ref="pointcut"/>
<!-- 环绕增强 -->
<!-- 将aroundLogger()方法定义为环绕增强并引用pointcut切入点 -->
<aop:around method="aroundLogger" pointcut-ref="pointcut"/>
        </aop:aspect> 
    </aop:config>    
</beans>
<!-- AOP基础小?结: AOP的目的是从系统中分离出切面,独立于业务逻辑实现,在程序执行时织入程序中运行。 面向切面编程主要关心两个问题:在什么位置,执行什么功能。 -->
<!-- 如果属性值中包含了特殊符号(&,<,>,",.),则注入时需要进行特殊处理通常用<![CDATA[值]]>标记或把特殊符号替换为实体引用。 
    xml预定的实体引用 <引用&li; >引用&gt; &引用&amp; -->

<!-- 引用其他bean组件。 Spring中定义的bean可以相互引用 ,从而建立依赖关系,除了使用ref属性,还可以通过<ref>子元素实现。 
    定义一个bean元素: <bean id="ID名" class="类型名"/> 定义一个引用bean元素的bean元素 <bean id="ID名" 
    class="类型名"/> 为其属性赋值,一般引用类型, <property name="属性名"> 引用其他bean的ID为属性赋值 <ref 
    bean="要引用bean的ID"/> <property/> <ref>标签 中的bean属性用来指定要引用的bean的id。除了bean属性。这里再为大家介绍local属性。 
    前面和上面一样 <property name="属性名"> 引用其他bean的ID为属性赋值 <ref local="要引用bean的ID"/>           
    </property> 从代码上看,local属性和bean属性的用法基本上是一样的他们的区别在于,Spring的配置文件是可以拆分的,使用local属性只能在同一配置文件中检索bean的id,而使用bean属性可以在其他配置中检索bean的id -->
<!-- 使用内部bean 如果一个bean组件仅在一处需要使用,可以把它定义为内部bean。如下所示: 定义一个bean并指定id <bean 
    id="xx" class="A类"> 为某个属性赋值 <property name="属性名"> <bean class="B类型"/> </bean> 
    B类的bean只能被A类使用 -->
<!-- 注入集合类型的属性
 list集合: 
 <property name="list">
     <list>
     定义list或数组中的元素
         <value>1</value>
          <value>2</value>
     </list>
 </property>
 
 set集合:
  <property name="list">
     <set>
     定义set中的元素
         <value>1</value>
          <value>2</value>
     </set>
 </property>
 
  map集合:
   <property name="list">
         <map>
         定义map中的键值对
             <entry>
                 <key><value>1</value></key>
                 <value>2</value>
             </entry>
         </map>
 </property>
 
   props类型:
     <property name="prop">
        <props>
            <prop key="football">篮球</prop>
            <prop key="basketball">足球</prop》
        </props>
   </property>
   
           空字符串: 
      <property name="kon"><value></value></property>
      
    null:
    <property name="nu"><null/></property>
     -->

 

转载于:https://www.cnblogs.com/liuduanwu/p/9350688.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值