spring-aspects

how to access custom annotation values in spring aspect

一曲冷凌霜 提交于 2021-02-11 07:35:41
问题 I am trying to access the custom annotation values from jointCut. But I couldn't find a way. My sample code : @ComponentValidation(input1="input1", typeOfRule="validation", logger=Log.EXCEPTION) public boolean validator(Map<String,String> mapStr) { //blah blah } Trying to access @Aspect class. But, i didnt see any scope to access values. Way i am trying to access is below code CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature(); String[] names = codeSignature

Exception in Spring AOP Itself not getting handled through RestControllerAdvice

早过忘川 提交于 2021-01-29 13:42:32
问题 I need to validate the HttpServletRequest for certain attribtues in my service layer before proceeding. I created @Before advice, Please note that AOP method throws Exception. I want exception thrown by this method should be handled by RestControllerAdvice. I can see that AOP method is being executed but DataNotValidException is not being handled by RestControllerAdvice. RestConrollerAdvice is working fine if there is any exception on the validation of other parameters or any exception in

Get pathVariable in Aspect - Spring boot

荒凉一梦 提交于 2020-12-13 02:59:23
问题 I want implement an annotation in spring boot. It's required to get path variable and use in annotation. @PostMapping("/v1/{id}") @HasZone(id = "#id") @ResponseStatus(HttpStatus.NO_CONTENT) fun createCompany( @PathVariable id: String) { .... } @Target(AnnotationTarget.FUNCTION) annotation class HasZone(vararg val id: String) @Before("@annotation(hasZone)") fun checkZonePermissions(hasZone: HasZone) { println(hasZone.id) } I get the parameter in PreAuthorize like this: #id. But in this

How to intercept each method call within given method using Spring AOP or AspectJ

混江龙づ霸主 提交于 2020-01-22 15:37:28
问题 class Test { @override public String a(){ b(); d(); } private String b() { c(); } private String c(){ d(); } private String d(){} } I want to intercept each methods of class Test that is been called from overridden method A() and want to know how much time each method like b(), c() took while processing some business logic separately. How can I achieve it using Spring AOP or Aspectj? 回答1: In order to weave into private methods, handle self-invocation within one class, dynamically determine

Java AOP JoinPoint does not get parameter names

梦想的初衷 提交于 2020-01-01 06:18:44
问题 I'm using Java Spring Mvc and Spring AOP to find the parameter names from the user. I have a controller which get parameters from user and call a service. I have an aspect that running before the service. The aspect should check if username and apiKey parameters are exist. Here is my code : Controller : @RequestMapping(method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE) public @ResponseBody String getDomainWithFoundIn(@RequestParam (value="domain") String domain,

Can not build MockMvc (There is already handler of type X mapped)

北战南征 提交于 2019-12-23 16:28:10
问题 I have simple Controller class: @Controller public class UserController { @RequestMapping("/user") @ResponseBody @PostAuthorize("hasPermission(returnObject, 'VIEW')") public User getUser(Long id) { // fetch user from DB } } I want to integration-test my controllers with Spring Security. Based on this article I have created the following test: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes = WebappConfig.class)

problems with Native AspectJ with Spring

折月煮酒 提交于 2019-12-20 07:32:08
问题 I am testing AspectJ compile time weaving with Spring 4 (once I get it to work, I want to use it in my projects). I have the following service class: @Service public class HelloService { public String sayHello(){ return sayHello2(); } public String sayHello2(){ return "Hello from AOP2!"; } } And here's my AspectJ advice: @Component @Aspect public class ExecutionTimeAdvice { @Around("execution(* com.senyume.aop.service..*(..))") public Object doBasicProfiling(ProceedingJoinPoint pjp) throws

Applying custom annotation advice to spring data jpa repository

女生的网名这么多〃 提交于 2019-12-20 05:25:30
问题 I am working on a mysql master slave replication. I am using spring data jpa(spring boot). What I needed is all write operations to go to master server and read-only operations to be equally distributed among multiple read-only slaves. For that I need to: Use special JDBC driver: com.mysql.jdbc.ReplicationDriver Set replication: in the URL: spring: datasource: driverClassName: com.mysql.jdbc.ReplicationDriver url: jdbc:mysql:replication://127.0.0.1:3306,127.0.0.1:3307/MyForum?user=root

Dependency Injection into Spring non-managed beans

不羁的心 提交于 2019-12-14 03:48:57
问题 I have a JPA domain class that is non managed. It is instantiated via the new operator. UserAccount account = new UserAccount(); userRepository.save(account) In my UserAccount class, I have a beforeSave() method which is dependent on my SecurityService to hash encode a password. My questions is "How do I get spring DI to inject the security service into my entity?". Seems that AspectJ and LoadTimeWeaving is what I need. I've tried an array for configurations, but I can't seem to get any of

Get all exceptions in Java and send them remotely

蓝咒 提交于 2019-12-12 09:36:04
问题 I have a huge Java application. I want to intercept all Java Exceptions adn send them by e-mail. I can't add everywhere code for sending the code via try-catch so is it possible to use for example Aspect to intercept the exception into low level classes and get the exception content? Or is there some way to override some internal Java Class and get the exception payload? What is possible? 回答1: You can use the @AfterThrowing advice of spring-aop. @Aspect @Component public class