zoukankan      html  css  js  c++  java
  • 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生成的代理类。

  • 相关阅读:
    混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。
    SQL中获取自增长的最大ID
    (inline)内联函数在IOS开发中的使用
    MS SQL SERVER 2005 高可用性之日志传送
    19_toast通知和notify通知 onTouch事件响应
    20 按比例设置 子控件的宽度和高度
    18_SurfaceView 其他线程绘图
    使用Microsoft Media Service实现网络影音多媒体应用系列第三篇技术要点
    使用Microsoft Media Service实现网络影音多媒体应用系列第二篇开发须知
    MVC3WIN7下的IIS7.5部署MVC3应用程序
  • 原文地址:https://www.cnblogs.com/toDjlPersonnalBlog/p/4651975.html
Copyright © 2011-2022 走看看