spring重点

1、首先明白什么叫依赖注入、控制反转,及其作用

控制反转(Inversion of Control ,Ioc)
所谓控制反转就是应用本身不负责依赖对象的创建及维护,依赖对象的创建及维护由外部容器来负责。这样控制权就由应用转移到了外部容器,控制权的转移就是所谓反转。

   依赖注入(Dependency Injection)
所谓依赖注入就是指:在运行期间,有外部容器动态地将依赖对象注入到组件中(构造方法和set方法)

好处:   

1.降低组件之间的耦合度,实现软件各层之间的解耦.
2.可以使容器提供众多服务如事务管理消息服务处理等等。当我们使用容器管理事务时,开发人员就不需要手工 控制事务,也不需要处理复杂的事务传播
3.容器提供单例模式支持,开发人员不需要自己编写实现代码.
4.容器提供了AOP技术,利用它很容易实现如权限拦截,运行期监控等功能
5.容器提供众多的辅佐类,使这些类可以加快应用的开发.如jdbcTemplate HibernateTemplate

个人理解依赖注入完全实现了面向接口编程,只需定义方法,无需实现,首先面向接口编程的好处就是统一编码风格。由容器创建对象,可以说是低内聚,高耦合。增加了代码的可维护性。

2、spring对于编程人员最重要的是写xml配置文件和使用注解那么我这里给几个简单的案例

依赖注入配置xml

  1. <bean id="person" class="com.itheima12.spring.di.xml.setter.Person">
  2. <!--
  3. property就是一个bean的属性
  4. name就是用来描述属性的名称
  5. value就是值,如果是一般类型(基本类型和String)
  6. -->
  7. <property name="pid" value="1"> </property>
  8. <property name="name" value="狗蛋"> </property>
  9. <!--
  10. spring容器内部创建的student对象给Person的student对象赋值了
  11. -->
  12. <property name="student">
  13. <ref bean="student"/>
  14. </property>
  15. <property name="lists">
  16. <list>
  17. <value>list1 </value>
  18. <value>list2 </value>
  19. <ref bean="student"/>
  20. </list>
  21. </property>
  22. <property name="sets">
  23. <set>
  24. <value>set1 </value>
  25. <value>set2 </value>
  26. <ref bean="student"/>
  27. </set>
  28. </property>
  29. <property name="map">
  30. <map>
  31. <entry key="m1">
  32. <value>map1 </value>
  33. </entry>
  34. <entry key="m2">
  35. <ref bean="student"/>
  36. </entry>
  37. </map>
  38. </property>
  39. <property name="properties">
  40. <props>
  41. <prop key="p1">prop1 </prop>
  42. <prop key="p2">prop2 </prop>
  43. </props>
  44. </property>
  45. <property name="objects">
  46. <list>
  47. <value>obj1 </value>
  48. <ref bean="student"/>
  49. </list>
  50. </property>
  51. </bean>
构造器注入配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation= "http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <bean id="person" class="com.itheima12.spring.di.xml.constructor.Person">
  7. <!--
  8. constructor-arg指的是构造器中的参数
  9. index 角标 从0开始
  10. value 如果一般类型,用value赋值
  11. ref 引用类型赋值
  12. -->
  13. <constructor-arg index="0" value="asdfsafd"> </constructor-arg>
  14. <constructor-arg index="1" ref="student"> </constructor-arg>
  15. </bean>
  16. <bean id="student" class="com.itheima12.spring.di.xml.constructor.Student"> </bean>
  17. </beans>
注解配置xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context= "http://www.springframework.org/schema/context"
  5. xsi:schemaLocation= "http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  9. <!--
  10. component:把一个类放入到spring容器中,该类就是一个component
  11. 在base-package指定的包及子包下扫描所有的类
  12. -->
  13. <context:component-scan base-package="com.itheima12.spring.scan">
  14. </context:component-scan>
  15. </beans>

好了这里就主要介绍了这些,还有很多遇到时在看文档吧,百度吧(包括单例,多例,事务 ,aop,继承,初始化销毁(

  1. <bean id="helloWorld" class="com.itheima12.spring.initdestroy.HelloWorld"
  2. init-method= "init"
  3. destroy-method= "destroy"> </bean>

),懒加载,别名,工厂方法创建对象等等,都可以看案例。

3、我们来学习aop

   切面(Aspect):其实就是共有功能的实现。如日志切面、权限切面、事务切面等。在实际应用中通常是一个存放共有功能实现的普通Java类,之所以能被AOP容器识别成切面,是在配置中指定的。

   通知(Advice):是切面的具体实现。以目标方法为参照点,根据放置的地方不同,可分为前置通知(Before)、后置通知(AfterReturning)、异常通知(AfterThrowing)、最终通知(After)与环绕通知(Around5种。在实际应用中通常是切面类中的一个方法,具体属于哪类通知,同样是在配置中指定的。

   连接点(Joinpoint):就是程序在运行过程中能够插入切面的地点。例如,方法调用、异常抛出或字段修改等,但spring只支持方法级的连接点。

   切入点(Pointcut):用于定义通知应该切入到哪些连接点上。不同的通知通常需要切入到不同的连接点上,这种精准的匹配是由切入点的正则表达式来定义的。

  目标对象(Target):就是那些即将切入切面的对象,也就是那些被通知的对象。这些对象中已经只剩下干干净净的核心业务逻辑代码了,所有的共有功能代码等待AOP容器的切入。

  代理对象(Proxy):将通知应用到目标对象之后被动态创建的对象。可以简单地理解为,代理对象的功能等于目标对象的核心业务逻辑功能加上共有功能。代理对象对于使用者而言是透明的,是程序运行过程中的产物。

  织入(Weaving):将切面应用到目标对象从而创建一个新的代理对象的过程。这个过程可以发生在编译期、类装载期及运行期,当然不同的发生点有着不同的前提条件。譬如发生在编译期的话,就要求有一个支持这种AOP实现的特殊编译器;发生在类装载期,就要求有一个支持AOP实现的特殊类装载器;只有发生在运行期,则可直接通过Java语言的反射机制与动态代理机制来动态实现

  

  意义:在开发的时候,各个切面和目标类是完全松耦合的,但是最终生成的代理对象的方法把这几个代理对象的内容就结合起来了。

通知介绍

拦截环绕通知

             

   

 

  1. /**
  2. * 环绕通知
  3. * joinPoint.proceed();这个代码如果在环绕通知中不写,则目标方法不再执行
  4. * 能控制目标方法的执行
  5. *前置通知和后置通知能在目标方法的前面和后面加一些代码,但是不能控制目标方法的执行
  6. */
  7. public void aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable{
  8. System.out.println( "begin");
  9. joinPoint.proceed(); //调用目标方法
  10. System.out.println( "end");
  11. }


在Spring中最基础的通知类型是拦截环绕通知(interception around advice)。

Spring里使用方法拦截的环绕通知兼容AOP联盟接口。

 前置通知

个更简单的通知类型是前置通知(before advice)。 它不需要MethodInvocation对象,因为它只是在进入方法之前被调用。

前置通知的一个主要优点是它不需要调用proceed()方法,因此就不会发生无意间运行拦截器链失败的情况。

     

  1. /*
  2. 前置通知
  3. 1、在目标方法执行之前
  4. 2、获取不到目标方法的返回
  5. <aop:before method="beginTransaction" pointcut-ref="perform"/>
  6. * 参数:连接点
  7. */
  8. public void beginTransaction(JoinPoint joinPoint){
  9. String methodName = joinPoint.getSignature().getName();
  10. System.out.println( "连接点的名称:"+methodName);
  11. System.out.println( "目标类:"+joinPoint.getTarget().getClass());
  12. System.out.println( "begin transaction");
  13. }

 异常通知
  1. <!--
  2. 异常通知
  3. -->
  4. <aop:after-throwing method= "throwingMethod" throwing= "ex" pointcut-ref= "perform"/>
  5. /**
  6. * 异常通知
  7. * 接受目标方法抛出的异常
  8. */
  9. public void throwingMethod(JoinPoint joinPoint,Throwable ex){
  10. System.out.println(ex.getMessage());
  11. }


 后置通知

 

  1. /**
  2. * 后置通知
  3. * 在目标方法执行之后
  4. 1、后置通知可以获取到目标方法的返回值
  5. 2、当目标方法抛出异常,后置通知将不再执行
  6. <aop:after-returning method="commit" pointcut-ref="perform" returning="val"/>
  7. */
  8. public void commit(JoinPoint joinPoint,Object val){
  9. System.out.println( "目标方法的返回值:"+val);
  10. System.out.println( "commit");
  11. }

后置通知可以访问返回值(但不能进行修改),被调用方法,方法参数以及目标对象。

最终通知

       

  1. /**
  2. * 最终通知
  3. 无论目标方法是否抛出异常都将执行
  4. <aop:after method="finallyMethod" pointcut-ref="perform"/>
  5. */
  6. public void finallyMethod(){
  7. System.out.println( "finally method");
  8. }

引入通知

Spring 把引入通知(introduction advice)作为一种特殊的拦截通知进行处理。

引入通知需要一个IntroductionAdvisor,和一个IntroductionInterceptor,后者实现下面的接口:

Public interface IntroductionInterceptor extends MethodInterceptor {   

                boolean implementsInterface(Class intf);

            }

继承自AOP联盟MethodInterceptor 接口的invoke()方法,必须确保实现引入:也就是说,如果被调用的方法位于一个已经被引入接口里,这个引入拦截器将负责完成对这个方法的调用--因为它不能调用proceed()方法。

引入通知不能和任何切入点一起使用,因为它是应用在类级别而不是方法级别。

xml文件配置

  1. <aop:config>
  2. <!-- 面 -->
  3. <aop:aspect ref="cacheInterceptor">
  4. <!-- 点 -->
  5. <!-- 环绕通知 -->
  6. <aop:around method="doAround" pointcut="execution(* cn.itcast.core.service.*.*.get*(..))"/>
  7. <!-- 变更 -->
  8. <!-- 后知通知 -->
  9. <aop:after method="doAfter" pointcut="execution(* cn.itcast.core.service.*.*.update*(..))"/>
  10. <aop:after method="doAfter" pointcut="execution(* cn.itcast.core.service.*.*.add*(..))"/>
  11. <aop:after method="doAfter" pointcut="execution(* cn.itcast.core.service.*.*.delete*(..))"/>
  12. <!--前置通知-->
  13. <!-- <aop:before method="" pointcut-ref=""/> -->
  14. </aop:aspect>
  15. </aop:config>

切入点表达式一般格式


Spring AOP 用户可能会经常使用 execution切入点指示符。执行表达式的格式如下:

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

          throws-pattern?)

除了返回类型模式(上面代码片断中的ret-type-pattern),名字模式和参数模式以外, 所有的部分都是可选的。

 

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、付费专栏及课程。

余额充值