spring 项目注解实现aop无效果

问题描述

使用@Aspect@Before()注解,运行无报错,但是没有相关注入效果

相关文件

beans.xml

<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 https://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="personService" class="DefaultPersonService"/>

<bean id="profiler" class="SimpleProfiler"/>

<aop:aspectj-autoproxy/>

</beans>

相应类

public class DefaultPersonService {

public Person getPerson() {

System.out.println("bbb");

return new Person();

}

}

public class Person {

public Person() {

}

}

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

@Aspect

public class SimpleProfiler {

@Before("execution(* DefaultPersonService.*(String)) && args(name)")

public void profile(String name) throws Throwable {

System.out.println("aaaa");

}

}

运行类

import org.springframework.beans.factory.BeanFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Boot {

public static void main(final String[] args) throws Exception {

BeanFactory ctx = new ClassPathXmlApplicationContext("beans.xml");

DefaultPersonService person = (DefaultPersonService) ctx.getBean("personService");

person.getPerson();

}

}

运行截图&我的期望

运行截图

期望运行结果:
aaaa
bbb

运行环境

interllij开发版2021.3
jdk1.8
spring 5
该项目为基于maven的spring项目

相应文件下载

【为方便排错,项目已放在和彩云,链接: https://caiyun.139.com/m/i?0d... 提取码:ikFA 】


回答:

DefaultPersonService.getPerson() 并没有参数。

public class DefaultPersonService {

public Person getPerson(String arg) {

System.out.println("bbb");

return new Person();

}

}

    public static void main(String[] args) {

BeanFactory ctx = new ClassPathXmlApplicationContext("beans.xml");

DefaultPersonService person = (DefaultPersonService) ctx.getBean("personService");

person.getPerson("arg");

}

    @Before("execution(* DefaultPersonService.*(..)) && args(arg)" )

public void profile(String arg) {

System.out.println(arg);

}

以上是 spring 项目注解实现aop无效果 的全部内容, 来源链接: utcz.com/p/944360.html

回到顶部