spring学习--14 总结

关键概念:
1、依赖注入:通过组合,而非继承关系,由Spring IoC container(ApplicationContext)管理注入所需Bean,实现Bean直接的解耦合。
2、Java配置:通过@Configuration和@Bean实现,取代复杂的xml配置方式。如果配置类有@ComponentScan注解,并且被扫描的类具有@Component、@Repository、@Service、@Controller注解,则在配置类不需要@Bean进行Bean的装配。
3、AOP:通过@Aspect(切面)和切面上的@Pointcut(切点),以及与切点相对应的Advice(建言)实现面向切面编程。其中切点拦截有两种实现形式:
A、一种使用注解进行拦截,首先自定义注解,在需要拦截的方法上使用注解,并用@Pointcut(“@annotation(注解类的绝对路径)”)定义切点,进而在建言中使用切点。具体示例如下:
注解:

    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface Action {
        String value() default "";
    }
被拦截的方法:
    @Service
    public class DemoAnnotationService {
        @Action //用注解标明所需拦截的方法
        public void add() {}
    }
切面、切点以及建言
        @AspectJ
        @Component
        public class LogAspectJ {
            @Pointcut("@annotation(com.annotation.Action)") //用注解定义切点
            public void annotationPointcut() {}

            // 以下为建言
            @After("annotationPointcut()") // 注解形式切点的建言
            public void after(JoinPoint joinPoint) {
                // 对接入点joinPoint的操作
                ......
            } 
     }
B、另外一种使用方法规则进行拦截,即直接在建言中标明所要拦截的方法,形如:@Advice("execution(* com.test.DemoMethodService.*(..))")。
            @AspectJ
            @Component
            public class LogAspectJ {
                //方法规则的建言
                @Before("execution(* com.test.DemoMethodService.*(..))")        
                public void before(JoinPoint joinPoint) {
                    // 对接入点joinPoint的操作
                    ......
                }
            }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值