spring之aop

手动实现切面类

首先创建接口Star,定义了sing()和dance()方法

package com.loubin;

public interface Star {
    public void sing();
    public void dance();
}

 

然后定义BigStar类,实现了Star接口,并且实现了sing和dance方法

package com.loubin;

public class BigStar implements Star{
    String name;

    public BigStar() {
    }

    public BigStar(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void sing() {
        System.out.println(name + "正在唱歌");
    }

    @Override
    public void dance() {
        System.out.println(name + "正在跳舞");
    }
}

 

然后实现两个切面类,分别是LogBefore和LogAfter,如下,分别实现了MethodBeforeAdvice接口和AfterAdvice接口

package com.loubin.Aspect;

import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor;

import java.lang.reflect.Method;

public class LogBefore implements MethodBeforeAdvice {

    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("舞台搭建好了");
    }
}

 

package com.loubin.Aspect;

import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class LogAfter implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("表演结束");
    }
}

 

下面是在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: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 class="com.loubin.Aspect.LogAfter" id="after"></bean>
    <bean class="com.loubin.Aspect.LogBefore" id="before"></bean>
    <bean class="com.loubin.BigStar" id="bigStar">
        <constructor-arg name="name" value="yangmi"></constructor-arg>
    </bean>
    <aop:config>
        <aop:pointcut id="poc" expression="execution(* com.loubin.BigStar.*(..))"/>
        <aop:advisor advice-ref="before" pointcut-ref="poc"></aop:advisor>
        <aop:advisor advice-ref="after" pointcut-ref="poc"></aop:advisor>
    </aop:config>
</beans>

 

下面是测试代码

package com.loubin;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Star bigStar = context.getBean("bigStar", Star.class);
        bigStar.sing();
        bigStar.dance();

    }
}

 注意!!!!!!测试代码中bigStar的类是Star接口,不是BigStar类哦

配置中定义切面

不在需要上一节的LogBefore和LogAfter类了,只需要定义一个普通的MyLog类即可

package com.loubin.log;

public class MyLog {
    public void before(){
        System.out.println("舞台搭建好了");
    }
    public void after(){
        System.out.println("表演结束");
    }
}

 

然后在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"
       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 class="com.loubin.log.MyLog" name="log"></bean>
    <bean class="com.loubin.BigStar" id="bigStar">
        <constructor-arg name="name" value="yangmi"></constructor-arg>
    </bean>

    <aop:config>
        <aop:pointcut id="poc" expression="execution(* com.loubin.BigStar.*(..))"/>

        <aop:aspect id="aspect1" ref="log">
            <aop:before method="before" pointcut-ref="poc"></aop:before>
            <aop:after-returning method="after" pointcut-ref="poc"></aop:after-returning>
        </aop:aspect>
    </aop:config>
</beans>

很明显,这种方法非常实用,但是log对象不能获取bigStar对象的信息,如果需要获取信息,还是第一种方法实用。

注释实现aop

我懒得写了抱歉。。。反正不常用

posted @ 2025-02-09 13:13  地球上最后一个直男  阅读(6)  评论(0)    收藏  举报