Spring AOP pointcut не запускается при вызове метода объекта, если он доступен внутри списка - PullRequest
1 голос
/ 07 мая 2020

У меня есть приложение весенней загрузки с парой классов, классом конфигурации и аспектом, как показано ниже. Приведенный ниже пример иллюстрирует проблему, с которой я столкнулся.

У меня есть офисный класс, в котором есть список принтеров в качестве зависимости, созданной с использованием конфигурации внешнего файла свойств. Я хотел бы выполнять аспект всякий раз, когда вызывается метод Printer.getFilename. Это не запускает аспект Если у меня есть список принтеров, но он работает, когда у меня есть один объект Printer без списка.

package com.example

public class Office {
   private final List<Printer> printersList;

   public Office(Printer printersList){
     this.printersList = printersList;
   }

   public void printFiles(){
      for(Printer printer: printersList)
        printer.getFileName();
   }
}
package com.example

public class Printer {
  private deviceId;

  public String getFileName(){
     return "fileName";
  }
}
@Configuration
public class ApplicationConfiguration{
  @Bean
  public Office office(){
    List<Printer> printerList = new ArrayList<>();
    // Adding to the list based on printer id based on some external property file configuration
    printerList.add(new Printer());
    printerList.add(new Printer());
    return new Office(printerList);
  }
}
@Aspect
@Component
public class PrinterFileNameAspect {
    @Pointcut("execution(* com.example.Printer.getFileName())")
    private void getFileNameJp() {}

    @Around("getFileNameJp()")
    public String returnFileName(ProceedingJoinPoint pjp) {
        return "Modified File Name";
    }
} 

Я обнаружил, что список beans не зарегистрирован в контейнере Spring. Поэтому я изменил класс конфигурации, чтобы зарегистрировать bean-компонент

@Configuration
public class ApplicationConfiguration{
  @Autowired
  private GenericWebApplicationContext context;

  @Bean
  public Office office(){
    List<Printer> printerList = new ArrayList<>();
    // Adding to the list based on printer id
    Printer colorPrinter = new Printer();
    context.registerBean("colorPrinter", Printer.class, () -> colorPrinter);
    printerList.add(colorPrinter);
    Printer specialPrinter = new Printer();
    context.registerBean("specialPrinter", Printer.class, () -> specialPrinter);
    printerList.add(specialPrinter);
    return new Office(printerList);
  }
}

. Вышеуказанные изменения конфигурации не помогают. Я думаю, что упускаю кое-что в основах Spring Aop. Я хочу реализовать spring aop со списком принтеров, так как я не могу изменить logi генерации списка c (logi генерации списка c сложный и должен быть динамическим c).

Ответы [ 3 ]

2 голосов
/ 10 мая 2020

Я добавляю альтернативный ответ, потому что вы, кажется, хотите узнать, как использовать новый метод GenericApplicationContext.registerBean(..), представленный в Spring 5. Поскольку я не являюсь пользователем Spring, я также хотел узнать, о чем он, и пришел с этим решением.

Опять же, я предоставляю полные определения классов. Они похожи, но немного отличаются от моего первого ответа. В частности, Printer больше не является прототипом @Component, а является POJO. Я все же оставил Office как одноэлементный компонент для удобства. Если вам также нужно несколько экземпляров, вы всегда можете настроить код в соответствии с вашими потребностями.

Теперь важно и решает вашу проблему следующее: после программной регистрации bean-компонентов вы должны получить их из контекста приложения через getBean(), а не просто добавлять созданные вручную экземпляры POJO в список принтеров. Только если вы получаете bean-компоненты из контекста приложения, Spring позаботится и о создании прокси AOP там, где это необходимо. *

(...)
18:20:54.169 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'office'
18:20:54.177 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'colorPrinter'
18:20:54.178 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'specialPrinter'
colorPrinter.pdf
specialPrinter.pdf
(...)
1 голос
/ 10 мая 2020

Как насчет этого простого решения, основанного на прототипе bean-компонентов?

package de.scrum_master.spring.q61661740;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("prototype")
public class Printer {
  public String getFileName() {
    return "fileName";
  }

  public void configureIndividually(String whatever) {
    System.out.println("printer being configured individually: " + whatever);
  }
}
package de.scrum_master.spring.q61661740;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class Office {
  private final List<Printer> printersList = new ArrayList<>();

  public void addPrinter(Printer printer) {
    printersList.add(printer);
  }

  public void printFiles() {
    for (Printer printer : printersList)
      System.out.println(printer.getFileName());
  }
}
package de.scrum_master.spring.q61661740;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class PrinterFileNameAspect {
  // Package name is optional if aspect is in same name as Printer
  @Pointcut("execution(* de.scrum_master.spring.q61661740.Printer.getFileName())")
  private void getFileNameJp() {}

  @Around("getFileNameJp()")
  public String returnFileName(ProceedingJoinPoint pjp) {
    return "modified file name";
  }
}
package de.scrum_master.spring.q61661740;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@SpringBootApplication
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class Application {
  public static void main(String[] args) {
    try (ConfigurableApplicationContext appContext = SpringApplication.run(Application.class, args)) {
      doStuff(appContext);
    }
  }

  private static void doStuff(ConfigurableApplicationContext appContext) {
    Printer colorPrinter = appContext.getBean(Printer.class);
    colorPrinter.configureIndividually("my color config");
    Printer specialPrinter = appContext.getBean(Printer.class);
    specialPrinter.configureIndividually("my special config");

    Office office = appContext.getBean(Office.class);
    office.addPrinter(colorPrinter);
    office.addPrinter(specialPrinter);
    office.printFiles();
  }
}

Теперь вы можете позволить контейнеру позаботиться о создании экземпляров bean-компонентов, но вы все равно можете настроить их индивидуально. Я не понимаю, почему вам нужно вручную регистрировать bean-компоненты в этом случае.

Журнал консоли будет:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)

2020-05-10 15:52:24.817  INFO 10404 --- [           main] d.s.spring.q61661740.Application         : Starting Application on Xander-Ultrabook with PID 10404 (C:\Users\alexa\Documents\java-src\spring-aop-playground\target\classes started by alexa in C:\Users\alexa\Documents\java-src\spring-aop-playground)
2020-05-10 15:52:24.821  INFO 10404 --- [           main] d.s.spring.q61661740.Application         : No active profile set, falling back to default profiles: default
2020-05-10 15:52:25.895  INFO 10404 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2020-05-10 15:52:25.918  INFO 10404 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 17ms. Found 0 repository interfaces.
2020-05-10 15:52:26.454  INFO 10404 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$377fd151] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-05-10 15:52:27.148  INFO 10404 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-05-10 15:52:27.189  INFO 10404 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-05-10 15:52:27.190  INFO 10404 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
2020-05-10 15:52:27.375  INFO 10404 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-05-10 15:52:27.376  INFO 10404 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2486 ms
2020-05-10 15:52:27.681  INFO 10404 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-05-10 15:52:28.005  INFO 10404 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-10 15:52:28.007  INFO 10404 --- [           main] d.s.spring.q61661740.Application         : Started Application in 3.735 seconds (JVM running for 5.395)
printer being configured individually: my color config
printer being configured individually: my special config
modified file name
modified file name
2020-05-10 15:52:28.135  INFO 10404 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
1 голос
/ 07 мая 2020

Как насчет добавления пакета, содержащего класс Printer, в класс приложения SpringBoot и объявления их в @SpringBootApplication

@Aspect
@Component
public class PrinterAspect
{
    @Around( "getFileNameJp()" )
    private String returnFileName( ProceedingJoinPoint joinPoint ) throws Throwable
    {
        return "Modified File Name"; // This will always return this name
    }
}

И, наконец, включите AspectJAutoproxy для приложения

@SpringBootApplication(scanBasePackages = "com.example.*" )
@EnableAspectJAutoProxy( proxyTargetClass = true )
public class Application
{
    public static void main( String[] args )
    {
        SpringApplication.run( Application.class, args );
    }
}

Вы также пропустили @Component в классах Printer и Office. Итак, в заключение ваш класс Office должен выглядеть так:

@Component
public class Office
{
    private final List<Printer> printer;

    public Office( List<Printer> printer )
    {
        this.printer = printer;
    }
    public void printFiles()
    {
        // my code logic ...
        // Demo logic
        for( Printer printer1 : printer )
        {
            System.out.println( printer1.getFileName() );
        }
        // my code logic ...
    }
}

И класс принтера должен выглядеть как

@Component
public class Printer
{
    private int deviceId;
    private String fileName;

    public String getFileName()
    {
        // my code logic here ...
        fileName = String.valueOf( System.nanoTime() ); // demo logic
        return fileName;
    }
}

И использование должно выглядеть так:

@Autowired
private Office office;

@GetMapping( "/demo" )
public List<String> demo()
{
    office.printFiles();
    return fileNames(); // To be implemented
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...