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();
            }
        }
    }
  • 相关阅读:
    关于label和span设置width无效问题解决方法
    CSS 去掉点li 的点
    margin标记可以带一个、二个、三个、四个参数,各有不同的含义。
    MyEclipse下打开ftl文件
    创业企业如何定制商业模式:把握不同行业生命周期,9大要素集中进行创新【转】
    WEB缓存初探
    word 2013如何从某一页开始插入页码
    Ubuntu 16.04 安装jdk
    vmware ubuntu安装vmware tools
    JSP学习
  • 原文地址:https://www.cnblogs.com/lxcmyf/p/6439621.html
Copyright © 2011-2022 走看看