Good afternoon, the question is as follows: There is a simple application with a java config file, there is an aspect that when the method is triggered should just output the message to the console, but the "@Pointcut" slice does not work during the method operation.

Aspect class:

package ***.aspects; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Aspect @Component("statisticsAspect") public class StatisticsAspect { public StatisticsAspect() { } @Before("fileEventLoggerMethod()") public void logBefore(JoinPoint joinPoint) { System.out.println("BEFORE : " + joinPoint.getTarget().getClass().getSimpleName() + " " + joinPoint.getSignature().getName() ); } @Pointcut("execution(* *.logEvent(..))") public void fileEventLoggerMethod() { System.out.println("Выполнение файлового логгера"); } @Pointcut("execution(* *.logEvent(..))") public void allEventLoggerMethod() { System.out.println("Выполнение любого логгера"); } 

}

Configuration class:

 package core.config; import core.beans.App; import core.loggers.EventLogger; importcore.loggers.impl.FileEventLogger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.*; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; @Configuration @ComponentScan("spring.core") @PropertySource("classpath:file.event.logger.properties") @EnableAspectJAutoProxy public class AppConfig { @Autowired @Qualifier(value ="combinedLogger") private EventLogger combinedLogger; @Autowired @Qualifier(value = "fileEventLogger") private EventLogger fileEventLogger; @Autowired @Qualifier(value ="consoleEventLogger") private EventLogger consoleEventLogger; @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } @Bean public static AutowiredAnnotationBeanPostProcessor autowiredAnnotationBeanPostProcessor() { return new AutowiredAnnotationBeanPostProcessor(); } @Bean(name = "app") public App getApp() { System.out.println(fileEventLogger + " this logger"); return new App(fileEventLogger); } 

}

Simple logger class where the method itself works:

 @Component("fileEventLogger") public class FileEventLogger implements EventLogger { protected String fileName; protected File file; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public FileEventLogger() { } @Autowired public FileEventLogger(@Value("${fileName}")String fileName) { this.fileName = fileName; } @PostConstruct public void init() throws IOException{ this.file = new File(fileName); } @Override public void logEvent(Event event) { try { System.out.println(event + " This event"); FileUtils.writeStringToFile(file, event.toString(), true); } catch (IOException e) { e.printStackTrace(); System.out.println("Невозможно записать данные в файл " + e.getMessage()); } } @Override public String toString() { return "FileEventLogger{" + "fileName='" + fileName + '\'' + '}'; } } 

X class with main method:

 package core.beans; import core.config.AppConfig; import core.loggers.EventLogger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.stereotype.Component; @Component public class App { @Autowired @Qualifier("client") private Client client; @Autowired @Qualifier("Event") private Event event; @Autowired @Qualifier("holliday") private Holliday holliday; @Autowired @Qualifier(value = "consoleEventLogger") private EventLogger consoleEventLogger; @Autowired @Qualifier(value = "fileEventLogger") private EventLogger defaultLogger; private EventLogger logger; public App() { } @Autowired public App(@Value("#{ T(core.beans.Event).isDay() ? fileEventLogger : consoleEventLogger }") EventLogger logger) { this.logger = logger; } public String logEvent(String msg, EventType type) { System.out.println(logger); if (logger == null) { logger = defaultLogger; } holliday.congradulate(); System.out.println(client); String message = msg.replaceAll( String.valueOf(client.getId()), client.getFullName()); System.out.println(message); logger.logEvent(event); return("This return value from logEvent() !!" ); } public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); App app = (App) ctx.getBean("app"); app.logEvent("Some event for user 1", EventType.INFO); app.logEvent("Some event for user 2", EventType.ERROR); } 

}

When calling the method itself in fileEventLogger, I specifically get the message output to the console, but the pointcut is silent as a partisan at this time, I tried to enter another class name, i.e. if entered incorrectly, an exception is thrown immediately. The configuration is all completely on annotations, and in the examples that were in the internet, xml was mainly used, so I created a new topic.

  • You have the '@Component ("statisticsAspect")' component set, but I do not see the qualifier with the name 'statisticsAspect'. Check if you have everything correctly, or if you have posted an up-to-date code. - Evgeny Lazarev
  • yes qualification in aspect is not obligatory, i.e. in app. I don’t need to get the configuration explicitly, I specified the name of the component for future convenience it can be removed and nothing will change - go2bed
  • And yet, I would advise you to first run this configuration on the 1st named bean. Thus, it would become obvious where the problem lies - in the naming / injection of beans or something else .. - Evgeny Lazarev
  • let's say this - I removed the component name in the aspect, I launch - the @Before advice works as before, if I create another advice, for example: @AfterReturning (pointcut = "fileEventLoggerMethod ()", returning = "retVal") public void logAfter (Object retVal) {System.out.println ("Returned value:" + retVal); } then it also works without any reason, it displays a message that the method did not return anything, or did you mean that I inject a bin into an aspect of a particular class? - go2bed
  • I suggest you temporarily remove all unnecessary. 1 - name and component of the aspect. 2 - in the configuration, specifically inject your logger (for now, remove other possible implementations). (in fact, here you can simply make sure that it is exactly what you need to inject). In general, I propose to look carefully at the debugging fact that you will have runtime loaded, it will be much easier to do this if you remove the extra logger implementations for now. - Evgeny Lazarev

0