有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java方面在春季没有被调用

当调用MyBean的save方法时,不会调用aspect

我的豆子。爪哇

package com.crm.web.beans;
public class MyBean {
   public String save() {
       System.out.pringln("Save is called");
   }
}

AppConfig。爪哇

@Configuration
@EnableAspectJAutoProxy
public class AppConfig {

@Bean
  public MyLogger myAspect() {
    return new MyLogger();
}

}

麦洛格。爪哇

@Configuration
@Aspect
public class MyLogger {
private Logger log = Logger.getAnonymousLogger();


@Around("execution(* com.crm.web.beans.*(..))")
public void log(JoinPoint point) {
    System.out.println("This is calledddddddddd");

    log.info(point.getSignature().getName() + " called...");
}

}

应用程序上下文。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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

<context:annotation-config />
<context:spring-configured />

</beans>

Iam仅调用save方法“save is called”会被打印出来。 提前谢谢


共 (3) 个答案

  1. # 1 楼答案

    你不是

    • 失踪 应用程序类路径上的两个AspectJ库:aspectjweaver.jaraspectjrt.jar(@seeSpring Aspectj documentation)或
    • 您不是在调用增强的代理,而是在MyBean本身内
  2. # 2 楼答案

    好吧,我想我成功了,试试这个:

    @Configuration
    @EnableAspectJAutoProxy
    public class AppConfig {
    
    @Bean
      public MyLogger myAspect() {
        return new MyLogger();
    }
    
    @Bean
      public MyBean myBean() {
        System.out.println("MyBean is called");
        return new MyBean();
    }
    
    }
    

    然后:

    public class MainApp {
    
        public static void main(String[] args) {
            ApplicationContext context = 
              new AnnotationConfigApplicationContext(AppConfig.class);
            MyBean mybean = context.getBean(MyBean.class);
            mybean.save();
        }
    
    }
    

    这样可能会出现“无法解决的循环引用”错误,我认为这是因为它试图记录logger类。你可以修改你的Around表达式,不管怎样,C没有一个“*”,改成这样:

    @Around("execution(* com.crm.web.beans.MyBean.*(..))")
    

    还可以查看flob和JavaBond的答案

  3. # 3 楼答案

    你的切入点表达式不正确。应该是下面的样子

    @Around("execution(* com.crm.web.beans..(..))")
    

    参见参考文档here(参考点-服务包中定义的任何方法的执行)

    还可以将@Configuration注释更正为@Componenton MyLogger,如下所示

    @Component
    @Aspect
    public class MyLogger { .. }
    

    见解释here。粘贴下面的相关部分

    您可以在Spring XML配置中将方面类注册为常规bean,或者通过类路径扫描自动检测它们,就像其他Spring托管bean一样。但是,请注意,@Aspect注释不足以在类路径中自动检测:为此,您需要添加一个单独的@Component注释(或者根据Spring的组件扫描器规则,添加一个符合条件的自定义原型注释)