0

I want to get value of systemType variable from login token everytime I access to API, so I've written an AOP class :

@Aspect
@Configuration
public class AspectDatabaseService {
    Logger logger = LoggerFactory.getLogger(AspectDatabaseService.class);
    public int systemType;
    public int getSystemType() {
        return systemType;
    }
    
    public void setSystemType(int systemType) {
        this.systemType = systemType;
    }
    
    @Before("execution(* com.toshiba.mwcloud.gs.dbaas.service.impl.DatabaseServiceImpl.*(..))")
    public void before(JoinPoint joinPoint) {
.....
        systemType = getSystemTypeFromToken(token);
        logger.info("before called {0}", joinPoint.toString());
    }
    
    private HttpServletRequest getHttpServletRequest() {
           ......
    }
    
    private int getSystemTypeFromToken(String token) {
    ......
    }
}

And I have a class with constructor like this

public DatabaseServiceImpl(SingleDatabaseServiceImpl singleDatabaseService,
            MultiDatabaseServiceImpl multiDatabaseServiceImpl, OperatorDatabaseServiceImpl operatorDatabaseServiceImpl,
            AspectDatabaseService aspectDatabaseService) {
        this.aspectDatabaseService = aspectDatabaseService;
        this.systemType = aspectDatabaseService.getSystemType();
        if (systemType == SystemType.SINGLE.getCode()) {
            this.databaseAbstractService = singleDatabaseService;
        } else if (systemType == SystemType.MULTI.getCode()) {
            this.databaseAbstractService = multiDatabaseServiceImpl;
        } else {
            this.databaseAbstractService = operatorDatabaseServiceImpl;
        }
    }

But my AOP class is not worked, value of systemType in constructor of class DatabaseServiceImpl always returns 0 because Constructor still init before run AOP class

What should I do to change value in constructor ? Thanks a lot

  • Does this answer your question? [Spring - AspectJ pointcut for constructor object with annotation](https://stackoverflow.com/questions/27527495/spring-aspectj-pointcut-for-constructor-object-with-annotation) – user2340612 Aug 15 '23 at 09:56
  • I've tried, but it's not worked – nghihoang2207 Aug 15 '23 at 10:21
  • what have you tried specifically? Because the accepted answer says "Spring AOP does not support constructor interception, you do need full AspectJ for it" – user2340612 Aug 15 '23 at 10:23
  • i want to change value of systemType variable in constructor of DatabaseServiceImpl. This value will be changed base on the token in function "Before()" of AOP class – nghihoang2207 Aug 15 '23 at 10:31
  • yeah I get that, but according to the accepted answer I linked above, this isn't possible with plain Spring AOP and you need to use the full AspectJ capabilities instead – user2340612 Aug 15 '23 at 10:51
  • _"I've tried, but it's not worked"_ - What have you tried? You did not show. Please post a minimal, complete reproducer (ideally a small Maven project) on GitHub, if you are unable to explain. Just let your code speak. Then, I can help you. – kriegaex Aug 17 '23 at 02:43
  • And please, use `@Aspect @Component` for your aspects, not `@Configuration`. An aspect is **not** a configuration. Config classes are wired differently from regular components. I really wonder why I see this anti-pattern so often. There must be one bogus source, which everybody is copying the same faulty code from. This will not solve this particular problem here, because you need native AspectJ, but it shall help you to avoid problems with Spring AOP aspects. – kriegaex Aug 17 '23 at 02:46

0 Answers0