zoukankan      html  css  js  c++  java
  • 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主要用来

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

  • 相关阅读:
    Python IDE
    Codeforces Beta Round #69 Div1
    HDU1595 find the longest of the shortest[最短路]
    MFC/OpenGL下的调色板
    ApacheCN 编程/大数据/数据科学/人工智能学习资源 2019.12
    计算机电子书 2016 BiliDrive 备份
    计算机电子书 2017 BiliDrive 备份
    Java项目中常见的异常处理
    从小工到专家第三次读后感
    《梦断代码》读后感1
  • 原文地址:https://www.cnblogs.com/ouzilin/p/4982286.html
Copyright © 2011-2022 走看看