0

I want use an annotation to check specified parameters in method, code like this, but it not work.

because it does not print the id in the method aroundTenantCheck

I alse use springboot 2.2.6

pom.xml

<?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.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.gitbug</groupId>
    <artifactId>spring-one-datasource</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-one-datasource</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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
            <version>1.3.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.directory.api</groupId>
            <artifactId>api-util</artifactId>
            <version>1.0.0-M20</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Annotation:

@Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.PARAMETER)
    public @interface TCheck {
}

Aspect:

@Aspect
@Component
@org.springframework.core.annotation.Order()
public class ParamCheckAspect {
    @Before("execution(public * *(.., @cn.xxxx.xx.aspect.TCheck (*), ..)) && args(id, ..)")
    public void aroundTenantCheck(Long id) {
        System.out.println(id);
    }
}

Annotation use:

public void sb(@TCheck Long id) {
    int a = id.intValue();
    System.out.println("-------------sa" + a);
}

@PostConstruct
public void init() {
    sb(1L);
}

In IntelliJ IDEA, click the 'm', it can jump to the method use the @TCheck:

IntelliJ IDEA screenshot

hehe
  • 173
  • 3
  • 13
  • Please go through this [answer](https://stackoverflow.com/a/29714549/4214241) – R.G Apr 23 '20 at 09:42
  • Welcome to SO. I edited your question, fixing some wording, spelling, formatting. I also added syntax highlighting and fixed your image to be displayed inline. Please be advised to learn the markup syntax here and click "edit" in order to fix your own question next time. Please also learn what an [MCVE](https://stackoverflow.com/help/mcve) is. Thank you. – kriegaex Apr 23 '20 at 11:31
  • As for the question content, actually you do not explain what the problem is other than saying "it does not work", which is not very helpful. What exactly does not work? I tried your aspect and the pointcut works for me, the aspect gets triggered. Are you seeing an error message? A stack trace maybe? Is there some information inside the aspect you need to access but do not know how? Is the aspect just not triggered because you forgot to make your target class a `@Component` too? Are the components not picket up by component scan? So many possible problems → MCVE, please! – kriegaex Apr 23 '20 at 11:34
  • thanks for all of you, it is still not work, I guess it cause by springboot. – hehe Apr 23 '20 at 13:47
  • I found the solution. https://stackoverflow.com/a/30611671/3126580 – hehe Apr 24 '20 at 06:32

0 Answers0