zoukankan      html  css  js  c++  java
  • springAOP实现(含实例)

    需要用到的jar包:

    1.XML方式实现:

    package cn.lxc.post;
    
    public class Intermediary {
        
        public void post(){
            System.out.println("该房源已发布!");
        }
    }
    package cn.lxc.service;
    
    public interface Rent {
        public void rent();
    }
    package cn.lxc.service.impl;
    
    import cn.lxc.service.Rent;
    
    public class Landlord implements Rent{
    
        @Override
        public void rent() {
            System.out.println("房东:我要出租房子了!");
        }
    }
    package cn.lxc.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import cn.lxc.service.Rent;
    
    
    public class Test {
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
            Rent r = ac.getBean(Rent.class);
            try {
                r.rent();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    beans.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:p="http://www.springframework.org/schema/p"
        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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!-- 目标对象 -->
        <bean id="rent" class="cn.lxc.service.impl.Landlord"/>
        <!-- 切面 -->
        <bean id="intermediary" class="cn.lxc.post.Intermediary"/>
        <aop:config>
            <aop:aspect ref="intermediary">
                <aop:before method="post" pointcut-ref="pointcut"/>
                <aop:pointcut id="pointcut" expression="execution(* cn.lxc.service.impl.*.*(..))" />
            </aop:aspect>
        </aop:config>
    </beans>

     2.注解方式:

    package cn.lxc.post;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    
    @Aspect
    public class Intermediary {
        @Before("execution(* cn.lxc.service.impl.*.*(..))")
        public void post(){
            System.out.println("该房源已发布!");
        }
    }

    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:p="http://www.springframework.org/schema/p"
        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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
        <!-- 目标对象 -->
        <bean id="rent" class="cn.lxc.service.impl.Landlord"/>
        <!-- 切面 -->
        <bean id="intermediary" class="cn.lxc.post.Intermediary"/>
        <!-- 自动代理 -->
        <aop:aspectj-autoproxy/>
    </beans>

    测试类:

    package cn.lxc.test;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import cn.lxc.service.Rent;
    
    
    public class Test {
        public static void main(String[] args) {
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
            Rent r = ac.getBean(Rent.class);
            try {
                r.rent();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    跟我一起了解koa(四)
    快速定位隐蔽的sql性能问题及调优【转载】
    PV,UV,IP
    ActiveMQ的安全机制使用及其源代码分析 [转]
    ActiveMQ中的安全机制 [转]
    ESB、SOA、EAI异同【转】
    磁盘 I/O 性能监控指标和调优方法
    PLS-00306:错误解决思路
    浅谈PetShop之使用存储过程与PLSQL批量处理(附案例)
    关于SQLSQL Server的三值逻辑简析
  • 原文地址:https://www.cnblogs.com/lxcmyf/p/6439621.html
Copyright © 2011-2022 走看看