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的操作
                    ......
                }
            }

猜你喜欢

转载自blog.csdn.net/xiewz1112/article/details/80499064