SpringFrameworkspring程序员

Spring学习笔记

2018-06-01  本文已影响214人  琅筑

概述

Spring是什么?

为什么是Spring

Spring的作用

适用范围

专题一 IOC

接口

面向接口编程

什么是IoC

Bean容器初始化

Spring注入

专题二 Bean

Bean的配置项

Bean的作用域

Bean的生命周期

初始化:
  1. 实现org.springframework.beans.factory.InitalizingBean接口,配置afterPropertiesSet方法
  2. 配置init-mothod方法
  3. 配置全局默认的初始化销毁方法:在xml文件中的beans中配置default-init-mothod和default-destory-mothod
销毁:
  1. 实现org.springframework.beans.factory.DisposableBean接口,配置destroy方法
  2. 配置destroy-mothod方法--> clearup()方法
内部的,接口的,默认的三种初始化和销毁方法的优先级:
  1. 如果配置了init-mothod/destroy-mothod 或者实现了接口那么默认的default-init-mothod和default-destory-mothod就会变得无效
  2. 如果同时配置了init-mothod/destroy-mothod 和实现了接口,接口的优先级更高,接口的方法先执行

Aware接口

ApplicationContextAware:

when an ApplicationContext creates a class that implements the org.springframework.context.ApplicationContextAware interface, the class is provided with a reference to that ApplicationContext

BeanNameAware

Bean的自动装配(AutoAwaring)

default-autoAwaring:

Bean装配之Resources

Bean的定义以及作用于的注解实现

Classpath 扫描与组件管理
元注解(Meta-annotations)
类的自动检测及bean的注册
<context: annotation-config/>
类的自动检测及Bean的注册
使用过滤器进行自定义扫描
定义Bean
作用域

Spring Bean装配注解说明

@Required

@Autowried

@Qualifier

基于java的容器注解

@Bean
@ImportResource和@Value
@Bean和@Scope

基于泛型的自动装配

Spring的JSR250标准的支持

@Resource
@PostConstruct and @PreDestroy

专题三AOP

什么是AOP

AOP实现方式
AOP相关的概念:
通知(Advice)
Spring框架中AOP的用途
Spring的AOP实现
有接口和无接口的Spring AOP实现区别

配置切面aspect

Schema-based AOP

Spring所有的切面和通知器都必须放在一个<aop:config> 内(可以配置多个包含<aop:config> 元素),每一个<aop:config> 可以包含pointcut,advisor和aspect元素(他们必须按照顺序声明)

<aop:config> 风格的配置大量使用了Spring的自动代理机制

aspect
    <bean id="aspectBiz" class="AspectBiz"></bean>
    <bean id="moocAspect" class="MoocAspect"></bean>
    <aop:config>
        <aop:aspect id="moocAspectAOP" ref="moocAspect">
        </aop:aspect>
    </aop:config>

配置切入点Pointcut

pointcut

下面为仅spring支持的:

Advice应用通知

Before advice

<aop:before pointcut-ref="" mothod=""/> 在匹配到的方法之前进行通知,执行通知方法

After returning advice

<aop:after-returning method="" pointcut-ref=""/> 在匹配的方法返回之后进行通知,执行通知方法

After throw advice

<aop:after-throwing method="" pointcut-ref=""/> 在匹配的方法抛出异常之后进行通知,执行通知方法

after advice

<aop:after method="" pointcut-ref=""/> 在匹配的方法抛出异常或者正常返回之后进行通知,必然执行,并且永远是最后一个执行的

Around advice 环绕通知

通知方法的第一个参数必须是ProceedingJoinPoint类型,在ProceedingJoinPoint的proceed方法前后,都可以执行通知和相应的逻辑。可以传入参数

Introductions

<aop:config>
        <aop:aspect id="moocAspectAOP" ref="moocAspect">
            <aop:declare-parents types-matching="com.xyz.myapp.service.*"
                             implement-interface="com.xyz.myapp.service.tracking.UsageTracked"
                             default-impl="com.xyz.myapp.service.tracking.defaultUsageTracked"/>
            <aop:before method="recordUsage" pointcut="com.xyz.myapp.SystemArchitecture.businessService()
                             and this(usageTracked)"/>
        </aop:aspect>
    </aop:config>

Advisors

    <aop:config>
        <aop:pointcut id="businessService" expression="execution(* com.xyz.myapp.service..(..))"/>
        <aop:advisor advice-ref="tx-advice" pointcut-ref="businessService"/>
    </aop:config>
    <tx:advice id="tx-advice">
        <tx:attrbutes>
            <tx:mothod name = "" propagation = ""/>
        </tx:attrbutes>
    </tx:advice>

专题四 Spring AOP API

Pointcut

<bean id="pointcutBean" class="org.springframework.aop.support.NameMatchMethodPointcut">
        <property name="mappedNames">
            <list>
                <value>sa*</value>
            </list>
        </property>
    </bean>
Before advice
Throws advice
After Returning advice
Interception around advice
Introduction advice
advisor API in Spring

ProxyFactoryBean

定义一个foo的bean id的对象引用,这个引用的对象是ProxyFactoryBean实现里getObject()方法创建的对象

getObject方法将创建一个AOP代理包装一个目标对象

ProxyFactoryBean

Proxying classes

使用global advisors

专题五 Spring 对 AspectJ的支持

AspectJ

Spring中配置AspectJ
@Configuration
@EnableAspectJAutoProxy
public class Appconfig() {
    
}
<aop:aspectj-autoproxy/>
aspect
pointcut
组合切入点表达式

可以使用&& || !来组合切入点

定义良好的pointcuts

Advice

before advice

使用@before("execution(* com.asd.fg.ao...)")

after returning

使用@AfterReturning("execution(* com.asd.fg.ao...)"),可以使用returning来绑定返回值

after throwing advice

使用@AfterThrowing("execution(* com.asd.fg.ao...)"),可以使用returning来绑定返回值

after(finally) advice
around advice
上一篇 下一篇

猜你喜欢

热点阅读