Простой Spring AOP, но Aspect не запускаетсяJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Гость
 Простой Spring AOP, но Aspect не запускается

Сообщение Гость »


I am trying to Practice Spring AOP. The Problem is, My program is not what I expect.
It should Look something like this when Aspect works.

Код: Выделить всё

Sep 27, 2020 1:11:11 PM aspects.LoggingAspect log
INFO: Method will execute
Sep 27, 2020 1:11:11 PM com.cms.services.CommentService publishComment
INFO: Publishing comment:Demo comment
Sep 27, 2020 1:11:11 PM aspects.LoggingAspect log
INFO: Method executed
However, It doesnt' log. Like Aspects haven't work at all.

Код: Выделить всё

com.cms.services.CommentService publishComment
INFO: Publishing Comment: Demo Comment
Main.java executes overall code Using Instance spring Spring Context
and also uses CommentService.java.
CommentService.java is nothing but of printing one lined log.
It is placed under services package.
Then there is LoggingAspect.java For Aspect Programming.
Finally, CommentConfig.java is for configuring EnableAspectJAutoProxy and registering Beans for Aspect class.

Here is my code.

Main.java

Код: Выделить всё

public class Main {
public static void main(String[] args) {
var c = new AnnotationConfigApplicationContext(CommentConfig.class);
var service = c.getBean(CommentService.class);

Comment comment = new Comment();
comment.setText("Demo Comment");
comment.setAuthor("Natasha");

service.publishComment(comment);
}
}
CommentService.java

Код: Выделить всё

@Service
public class CommentService {

private Logger logger = Logger.getLogger(CommentService.class.getName());
public void publishComment(Comment comment) {
logger.info("Publishing Comment: " + comment.getText());
}
}
LoggingAspect.java

Код: Выделить всё

@Aspect
@Component
public class LoggingAspect {

private Logger logger = Logger.getLogger(LoggingAspect.class.getName());

@Around("execution(* services.*.*(..))")
public void log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("method will execute");
logger.info("Method will execute");
joinPoint.proceed();
logger.info("Method has executed");
System.out.println("method has executed");
}
}
CommentConfig.java

Код: Выделить всё

@Configuration
@ComponentScan(basePackages = {"com.cms"})
@EnableAspectJAutoProxy
public class CommentConfig {
@Bean
public LoggingAspect aspect() {
return new LoggingAspect();
}
}
Also, this is my dependency.

Код: Выделить всё

    
17
17
UTF-8





org.springframework
spring-context
5.3.23




org.springframework
spring-aspects
5.3.23




org.springframework
spring-aop
5.3.24


Can't figure out what is missing...


Источник: https://stackoverflow.com/questions/781 ... -triggered
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Аннотация пользовательского метода Spring Boot не работает при использовании @Aspect
    Гость » » в форуме JAVA
    0 Ответы
    50 Просмотры
    Последнее сообщение Гость
  • Получение внутренних методов приводит к переменным данным с использованием Spring Boot Aspect @After Advice
    Anonymous » » в форуме JAVA
    0 Ответы
    29 Просмотры
    Последнее сообщение Anonymous
  • Aop @aspept не запускается в @springboottest
    Anonymous » » в форуме JAVA
    0 Ответы
    9 Просмотры
    Последнее сообщение Anonymous
  • Почему Spring AOP не может использовать фильтр безопасности Spring?
    Anonymous » » в форуме JAVA
    0 Ответы
    72 Просмотры
    Последнее сообщение Anonymous
  • Почему Spring AOP не может использовать фильтр безопасности Spring?
    Anonymous » » в форуме JAVA
    0 Ответы
    18 Просмотры
    Последнее сообщение Anonymous

Вернуться в «JAVA»