• spring aop


    spring aop是面向切面的编程,是面向对象编程的一个拓展。面向切面是指不侵入源代码的情况下,让主业务暴露一个切点,通过切点实现添加的非主业务。这样实现了解耦,提高代码的灵活性,重用性。实现aop有四种方法

    1.经典的基于代理的AOP
    2.@AspectJ注解驱动的切面
    3.纯POJO切面
    4.注入式AspectJ切面

    这里我们只介绍第二种方法

    1.实现接口UserDao

    package com.bjsxt.dao.impl;
    import org.springframework.stereotype.Component;
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;
    @Component("u") 
    public class UserDAOImpl implements UserDAO {
        public void save(User user) {
            //Hibernate
            //JDBC
            //XML
            //NetWork
            System.out.println("user saved!");
            //throw new RuntimeException("exeption!");
        }
    
    }

    2.service层

    package com.bjsxt.service;
    import javax.annotation.Resource;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Component;
    import com.bjsxt.dao.UserDAO;
    import com.bjsxt.model.User;
    @Component("userService")
    public class UserService {
        private UserDAO userDAO;  
        public void init() {
            System.out.println("init");
        }
        
        public void add(User user) {
            userDAO.save(user);
        }
        public UserDAO getUserDAO() {
            return userDAO;
        }
        
        @Resource(name="u")
        public void setUserDAO( UserDAO userDAO) {
            this.userDAO = userDAO;
        }
        public void destroy() {
            System.out.println("destroy");
        }
    }

    3.LogInterceptor

    package com.bjsxt.aop;
    import org.aspectj.lang.ProceedingJoinPoint;
    //@Aspect
    //@Component
    public class LogInterceptor {
        //@Pointcut("execution(public * com.bjsxt.service..*.add(..))")
        public void myMethod(){};
        
        //@Before("myMethod()")
        public void before() {
            System.out.println("method before");
        }
        public void after(){
            System.out.println("method after");
        }
        
        //@Around("myMethod()")
        public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
            System.out.println("method around start");
            pjp.proceed();
            System.out.println("method around end");
        }
        
    }

    4.applicationContext.xml

    <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-3.0.xsd
               http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
        <context:annotation-config />
        <context:component-scan base-package="com.bjsxt"/>
        <!-- <aop:aspectj-autoproxy></aop:aspectj-autoproxy> -->
        <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
        <aop:config>
            
            <aop:aspect id="logAspect" ref="logInterceptor">
                <aop:before method="before" pointcut="execution(public * com.bjsxt.service..*.add(..))" />
                <aop:after method="after" pointcut="execution(public * com.bjsxt.service..*.add(..))"/>
            </aop:aspect>
        
        </aop:config>
    
    
    </beans>

    5.test

    public class UserServiceTest {
        public static void main(String[] args){
            ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
            UserService service = (UserService)ctx.getBean("userService");
            System.out.println(service.getClass());
            service.add(new User());
            ctx.destroy();
            
        
        }

    上面是使用xml方式

    如果使用annotation方式的话将3.logInterceptor的注释打开

    applicationContext.xml

    <context:annotation-config />
        <context:component-scan base-package="com.bjsxt"/>
         <aop:aspectj-autoproxy />

    这里就实现了aop

    aop主要用来

    日志记录,性能统计,安全控制,事务处理,异常处理 等等

  • 相关阅读:
    SourceForge上发布dedup util 收藏
    IBM快速整合Diligent 触动了谁的神经?
    Data Domain揭开其神秘面纱 加速中国业务发展
    Java NIO原理和使用
    用PDB库调试Python程序
    windows下配置eclipse + Python + PyDev + tornado,并使用pylint进行静态检测
    函数fork与vfork的区别与联系详解
    关于C++中友元类和友元函数的示例讲解
    const成员函数
    pthread_cond_wait()深入解析
  • 原文地址:https://www.cnblogs.com/ouzilin/p/4982286.html
Copyright © 2020-2023  润新知