• spring学习3_通过注解简单实现AOP


       在上一篇中通过XML配置演示了Spring实际进行AOP的过程,这里简单介绍一下通过注解实现这一功能的过程。

       1、Spring配置

    <?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:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd">
            
      <context:annotation-config/>
      <context:component-scan base-package="com"></context:component-scan>
      <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    </beans>

          其中<context:annotation-config/> 声明将启用Spring自带的BeanPosProcess对添加注解的类进行处理。

                <context:component-scan base-package="com"></context:component-scan> 将对com包下的所有类进行初始化。

                 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>声明为注解为@Apect的切面类生成Proxy代理类。

       2、拦截器类

    package com.handler;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    
    
    @Component
    @Aspect
    public class LogInterceptor {
        @Before("execution(* com.service..*.*(..))")
        public void beforeExcute(){
            System.out.println("aspect execute.");
        }
    
    }

            @Component 注解为该类在容器中进行初始化。

            @Aspect注解声明该类作为切面类,由Spring自动为其生成代理类。

           另外,在方法上声明@before,其表示在executions中对应的方法前面执行。类似的用法还有@After、@Around等。

    3、Service方法中注入Dao层

    package com.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import com.dao.UserDao;
    
    @Component("userService")
    public class UserService {
        
        private UserDao dao;
    
        @Autowired
        public void setDao(UserDao dao) {
            this.dao = dao;
        }
        
        public void save(){
            dao.save();
        }
        
        
    
    }

          @AutoWired自动按类型进行注入

       测试:

    package com.test;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.service.UserService;
    
    public class TestAopAnnotation {
        @Test
        public void testAopAnnotation(){
            
            ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
            
            UserService service = (UserService) context.getBean("userService");
            System.out.println(service.getClass());
            service.save();
            
        }
    }

      执行结果  

       将类打印出来后可以看出,此时从容器中取出的类为由Spring生成的代理类。

  • 相关阅读:
    检查型异常(Checked Exception)与非检查型异常(Unchecked Exception)
    maven跳过单元测试-maven.test.skip和skipTests的区别
    java JFR
    Docker常用命令
    关键字group by 、 Having的 用法
    css特效
    sql
    初识Hibernate之理解持久化类
    Hibernate 搭建
    基本 SQL 之增删改查
  • 原文地址:https://www.cnblogs.com/toDjlPersonnalBlog/p/4651975.html
Copyright © 2020-2023  润新知