АОП для аннотаций не работает в Spring BootJAVA

Программисты JAVA общаются здесь
Ответить
Гость
 АОП для аннотаций не работает в Spring Boot

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


I have myannotation and whenever my method(which has myannotation) is executed then AOP should be called but which is not working in my spring boot controller.But which is working for methods which has other annotations.Please help me to understand about what happens.

Update: MyAnnotation

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

 @Retention(RUNTIME)
@Target({ METHOD, CONSTRUCTOR })
public @interface MyAnnotation {
}

@Aspect
@Component
public class AnnotationAspect {
private static final String POINTCUT_METHOD1 = "@annotation(com.somepackage.MyAnnotation)";

@Around(POINTCUT_METHOD1)
public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
try {
System.out.println("Beforeee " + joinPoint);
joinPoint.proceed();
} finally {
System.out.println("Afterrr " + joinPoint);
}
return null;
}
}
Scenario 1:(Working)

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

@Controller
@RequestMapping("user")
public class ArticleController {

@GetMapping("article/{id}")
@MyAnnotation // here it is
public ResponseEntity getArticleById(@PathVariable("id") Integer id)
{
return new ResponseEntity(dummyMethod(), HttpStatus.OK);
}

public String dummyMethod() {
System.out.println("Dummy method with MyAnnotation");
return "HelloWorld!";
}
}
Log:(Working)

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

Beforeee execution(ResponseEntity com.mypackage.getArticleById(Integer))
Dummy method with MyAnnotation
Afterrr execution(ResponseEntity com.mypackage.getArticleById(Integer))
Scenario 2:(Not Working)

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

@Controller
@RequestMapping("user")
public class ArticleController {

@GetMapping("article/{id}")
public ResponseEntity getArticleById(@PathVariable("id") Integer id)
{
return new ResponseEntity(dummyMethod(), HttpStatus.OK);
}
@MyAnnotation //here it is
public String dummyMethod() {
System.out.println("Dummy method with MyAnnotation");
return "HelloWorld!";
}
}
Log:(Not Working)

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

Dummy method with MyAnnotation
Scenario 3: (Not Working)

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

@Service
public class ArticleService {
@MyAnnotation //here it is
public String dummyMethod() {
System.out.println("Dummy method with MyAnnotation");
return "HelloWorld!";
}
}


Источник: https://stackoverflow.com/questions/512 ... pring-boot
Ответить

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

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

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

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

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