-1

I am trying to handle all cross cutting concerns e.g exception, authorization etc. in a project/repo and inject in more than one separate and independent spring-webflux project i.e handling cross-cutting concerns by building a reusable microservice platform.

Can anyone suggest how to accomplish this?

For example:- light4j handle all cross cutting concerns as middleware just need to add as plugin. But it's not compatible with SpringWebflux. Even using AspectJ we can't use same handlers for different projects until or unless they are under same parent project.

I tried to use load-time weaving feature of AspectJ. I defined aspects in different project and add a plugin of that in current project (in which I want to use) but when exception occurred aspectJ part isn't invoked

Below is my pom.xml for current project

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dummy</groupId>
    <artifactId>dummydmanagement</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>DummyManagement</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.4.Final</version>
        </dependency>
        <dependency>
            <groupId>com.jaway.blog.aspectj</groupId>
            <artifactId>annotations-element-value-pair-without-main-class</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>system</scope>
            <systemPath>/home/mmt8281/codebase/annotations-element-value-pair-without-main-class/target/annotations-element-value-pair-without-main-class-1.0-SNAPSHOT.jar</systemPath>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

  

Below is pom.xml of aspect project

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.jaway.blog.aspectj</groupId>
    <version>1.0-SNAPSHOT</version>
    <artifactId>annotations-element-value-pair-without-main-class</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.7</version>
                <configuration>
                    <complianceLevel>1.8</complianceLevel>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Attaching gitHub links:-

AspectJ Code
DummyMgmt Code

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • 1
    You need to be more specific with a reproducible example. Question is too open ended, voted to close. – Toerktumlare Jun 14 '21 at 16:33
  • 1
    Probably nobody can answer that, the question is too abstract and you ignored the hint concerning a reprodicible example. Please learn how to ask questions here, start by reading about [MCVE](https://stackoverflow.com/help/mcve). That should help you understand. Welcome to SO. – kriegaex Jun 15 '21 at 04:39
  • If you edit a question or an answer, nobody will be notified, I just noticed by chance. It is better to write a quick comment after a substantial update. – kriegaex Jun 16 '21 at 01:23
  • Thanks for the POMs, but POMs without code are not very helpful. Was anything unclear about the term MCVE? Did you read the article? You say you want to use LTW. For that you do not need AspectJ Maven plugin, that one is for compile-time or post-compile binary weaving. BTW, are you sure you are really talking about native AspectJ LTW and not simply proxy-based Spring AOP? Many beginners mistake one for the other. Without code, it is impossible for me to say. An MCVE on GitHub would be ideal. – kriegaex Jun 16 '21 at 01:24
  • I am confused. Why are you expecting an aspect intercepting methods with `@YourAnnotation` to kick in if in your target application not a single method contains that annotation? And if you use Spring anyway and want to intercept Spring components, why don't you simply use Spring AOP? I see no signs of you trying to intercept any non-Spring components, so native AspectJ should not be necessary. – kriegaex Jun 16 '21 at 07:29
  • @ShashwatSingh, your statement is false, and that is good news for you: Both Spring AOP and AspectJ aspects can be used in multiple projects. You simply need to configure those projects correctly. This is simpler for Spring AOP and somewhat more complex for AspectJ, which is why I chose the former for my answer below. – kriegaex Jun 17 '21 at 02:42
  • OK, I don't know why you deleted more than 90% of your question, leaving nothing behind than a generic question without any detail, after I already answered your question and you had accepted it yesterday. There was absolutely no proprietary or confidential code, neither here nor on GitHub. So I have reverted your information purge and restored the question to the content at the time I answered it and successfully convinced another user here to retract his close vote. Unfortunately, I cannot restore your deliberately deleted comments. – kriegaex Jun 18 '21 at 07:06

1 Answers1

1

The problem with "copy & paste programming" is that you use something you do not understand. Maybe it would be better to learn the basics first.

Having said that, there are two ways to solve your problem, given the fact that the aspect module you copied from the blog post is native AspectJ:

  1. Configure all applications which want to use the aspect to also use native AspectJ, either by post-compile binary weaving or by load-time weaving. The Spring manual describes how to use LTW.

  2. Trivially convert the aspect module to a Spring component and make all client projects use Spring AOP.

The latter is what I did, because I suppose that you only want to use the aspect in Spring projects and that you only annotate public methods in Spring components.

The native AspectJ solution would be more powerful, because it would also work with static and protected/private methods, even for non-Spring code and even outside of Spring completely. But why use a cannon if a pistol is sufficient?

I sent you two pull requests:

What you need to change in the aspect module, is basically this:

  • Remove AspectJ Maven plugin
  • Add org.springframework:spring-context in order to be able to add @Component to the aspect class.
  • I also optimised a few more things, such as update the AspectJ version.

Then, in the application module you need to:

  • Add org.aspectj:aspectjweaver
  • Add the aspect module's base package name to the component scan
  • I also removed the ugly system dependency with the fixed path to the aspect module. If you simply run mvn clean install on the aspect module before you build the application, the library will be found in the local Maven repository. (I think, you need to urgently learn some Maven basics.)

Now when running this application

@SpringBootApplication
@ComponentScan(basePackages = {"com.dummy.dummydmanagement", "com.jayway.blog"})
public class DummyManagementApplication {
    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext context = SpringApplication.run(DummyManagementApplication.class, args);
        context.getBean(ItemService.class).getAllItemsService();
    }
    // (...)
}

the console log will be:

YourAspect's aroundAdvice's body is now executed Before yourMethodAround is called.

Exception occured
YourAspect's aroundAdvice's body is now executed After yourMethodAround is called.

Of course, annotating the static main method does not work with Spring AOP, which is why I removed the annotation. That would only work with native AspectJ. Please let me know if you need that, but I think you should keep it simple, because you clearly do not understand the tools you are using.


Update: Because for whatever reason the OP is angry with me now, because I told him to stop texting me directly on Telegram, he also deleted his own repositories. For anyone interested, here are my clones, containing both the original code and my modifications fixing the problem, as described above:

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • And why did you delete all your previous comments, making my replies to them now meaningless and out of context? **Update:** You even deleted your two GitHub repositories, i.e. nobody can understand and reproduce your problem anymore. OK, I am going to edit my answer to point to my forks from which I created the pull requests for your convenience. – kriegaex Jun 18 '21 at 06:54