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.