0
点赞
收藏
分享

微信扫一扫

Aspectj AOP实现流程(基于注解)

斗米 2022-05-01 阅读 68
javaspring

文章目录


前言

项目骨架

一、创建目标接口和目标类(包含切点)

public interface TargetInterface {
    public void save();
}
public class Target implements TargetInterface {
    public void save() {
        System.out.println("save running...");
    }
}

二、创建切面类(包含增强方法)

public class MyAspect {
    public void before(){
        System.out.println("前置增强....");
    }
}

三、将目标类和切面类的对象创建权交给spring

@Component("target")
public class Target implements TargetInterface {
    public void save() {
        System.out.println("save running...");
    }
}
@Component("myAspect")
public class MyAspect {
    public void before(){
        System.out.println("前置增强....");
    }
}

四、在切面类中使用注解配置织入关系

@Component("myAspect")
@Aspect //标注当前类是切面类
public class MyAspect {
	//配置切入点
    @Before("execution(* com.hbh.anno.*.*(..))")
    public void before(){
        System.out.println("前置增强....");
    }
}

五、在配置文件中开启组件扫描和AOP的自动代理

<?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"
       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
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
        <!--组件扫描-->
        <context:component-scan base-package="com.hbh.anno"/>
        <!--aop自动代理-->
        <aop:aspectj-autoproxy/>
</beans>

六、测试代码

五月 01, 2022 3:39:21 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
五月 01, 2022 3:39:21 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@3a03464, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@2d3fcdbd, org.springframework.test.context.support.DirtiesContextTestExecutionListener@617c74e5]
五月 01, 2022 3:39:21 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
五月 01, 2022 3:39:21 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@c038203: startup date [Sun May 01 15:39:21 CST 2022]; root of context hierarchy

前置增强....
save running...
举报

相关推荐

0 条评论