Spring复习

一:Spring 对于Bean的管理:不用让程序员去new出对象了,通过spring的xml配置或者注解,直接在你使用某个对象的时候去自动注入依赖或者自动装配依赖;基于xml的方式则在代码层次上考虑construct或setter注入方式,并且能够实现项目代码的外部部署;而基于注解的方式则不用写多余代码以及大量的xml文件配置了,但不能够在代码外部修改配置。


二. Bean Scope :As a rule, use the prototype scope for all stateful beans and the singleton scope for stateless beans
作为默认规则,用原型范围来表示有状态的beans,用单例范围来表示无状态的beans


三.当单例的beanA要装配非单例的beanB:容器不能够随时提供最新的beanB给beanA.这时,可以让beanA通过实现接口ApplicationContextAware 来让beanA知道容器的变化


四.bean 的init-method 和destroy-method 对应注解@PostConstruct和@PreDestroy


五.应用上下文ApplicationContext ac = new ClassPathXmlApplicationContext();
  ApplictionContext为顶级接口,ClassPathXmlApplicationContext为其实现类,向上转型的意义就在于:利于扩展,
  假如说有一个方法 public void do(ApplicationContext ac){...}和public void do(ClassPathXmlApplicationContect c){...},如果是前面一种方式,他可以根据实现类的不同而做出不同的动作;而用后一种,就只能对应于其实现类的动作。


六.fine-grained control and wider applicability 细粒度的控制和更宽广的应用性


七.@Required:setter方法上的注解,防止Null指针异常


八.@Bean,Indicates that a method produces a bean to be managed by the Spring container. 一旦某个配置类的某个方法被@Bean注解了,该方法返回一个bean对象;
可以用ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class) 来new出ApplicationContext;


九.@Bean(initMethod = "init",destroyMethod = "destroy")通过在Bean对象的对应类中定义的init()方法和destroy方法来初始化或销毁bean对象,达到对bean对象的生命周期的管理。


十.@Bean加@Scope("prototype")来重定义bean的范围


十一.AOP
注:强制使用CGLIB代理
前面说过Spring使用动态代理或是CGLIB生成代理是有规则的,高版本的Spring会自动选择是使用动态代理还是CGLIB生成代理内容,当然我们也可以强制使用CGLIB生成代理,那就是<aop:config>里面有一个"proxy-target-class"属性,这个属性值如果被设置为true,那么基于类的代理将起作用,如果"proxy-target-class"省略(或者写为false)则默认基于接口的代理。


a)AOP核心概念
1、横切关注点
对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点

2、切面(aspect)
类是对物体特征的抽象,切面就是对横切关注点的抽象
3、连接点(joinpoint)
被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
4、切入点(pointcut)
对连接点进行拦截的定义
5、通知(advice)
所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置(before)、后置(after)、异常(after-throwing)、最终(after-returning)、环绕通知(around)五类
6、目标对象
代理的目标对象
7、织入(weave)
将切面应用到目标对象并导致代理对象创建的过程
8、引入(introduction)
在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

b)基础xml配置的AOP:
<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">


   <aop:config><!--申明aop -->
      <aop:aspect id="log" ref="logging"> <!--申明切面 -->
         <aop:pointcut id="selectAll" <!--定义的切点名字 -->
         expression="execution(* com.tutorialspoint.*.*(..))"/>
<!--定义的连接点(join point)-->
         <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
         <aop:after pointcut-ref="selectAll" method="afterAdvice"/>
         <aop:after-returning pointcut-ref="selectAll" 
                              returning="retVal"
                              method="afterReturningAdvice"/>
         <aop:after-throwing pointcut-ref="selectAll" 
                             throwing="ex"
                             method="AfterThrowingAdvice"/>
      </aop:aspect>
   </aop:config>


   <!-- Definition for student bean -->
   <bean id="student" class="com.tutorialspoint.Student">
      <property name="name"  value="Zara" />
      <property name="age"  value="11"/>      
   </bean>


   <!-- Definition for logging aspect -->
   <bean id="logging" class="com.tutorialspoint.Logging"/> 


</beans>


c)基于@AspectJ
但在beans.xml中仍然要加一句 <aop:aspectj-autoproxy/>
参考http://wiki.jikexueyuan.com/project/spring/aop-with-spring-framenwork/aspectj-based-aop-with-spring.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值