1

Error: java.lang.IllegalArgumentException: No visible constructors in class org.springframework.hateoas.config.HypermediaSupportBeanDefinitionRegistrar$DefaultObjectMapperCustomizer

Mostly, I used example given in link, and the following code can be found at github repository

Annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface NeedTestClass {
}

Aspect:

@After("@args(NeedTestClass)")
public void afterReturningAtArgs() {
    log.info("aspect: after @args {}");
}

Service:

@Slf4j
@Component
public class BusinessService {

    public void logicWithAnnotatedArgs1(Child c) {
        log.info("service");
    }
}

Pojo (top class, not sub class):

@NoArgsConstructor // tried with or without
@NeedTestClass
public class Child {}

Test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
@SpringBootTest
public class AopTest {
    @Autowired
    private BusinessService myBusinessService;
    @Test
    public void testAtArgsPCD() {
        myBusinessService.logicWithAnnotatedArgs1(new Child());
    }

I attempted to examine aop and annotated class inheritance, but it seems the first step could not be ok. I have tried @annotation() and this() PCD both ok.

EDIT: So far I am wondering maybe the error is related with the bean loading sequence.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Tiina
  • 4,285
  • 7
  • 44
  • 73

1 Answers1

2

Your GitHub project does not even compile. Have you even tested it? First by trial and error I had to add all of these dependencies:

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
  <groupId>org.hibernate.javax.persistence</groupId>
  <artifactId>hibernate-jpa-2.0-api</artifactId>
  <version>1.0.1.Final</version>
</dependency>
<dependency>
  <groupId>com.alibaba.druid</groupId>
  <artifactId>druid-wrapper</artifactId>
  <version>0.2.9</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>6.0.6</version>
</dependency>

Next, I noticed that the Maven build does not seem to start the local (127.0.0.1) database because Spring Boot says this at start-up:

(...)
2018-01-02 17:57:18.882  INFO 14480 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
2018-01-02 17:57:20.007 ERROR 14480 --- [tionPool-Create] com.alibaba.druid.pool.DruidDataSource   : create connection error

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:590) ~[mysql-connector-java-6.0.6.jar:6.0.6]
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:57) ~[mysql-connector-java-6.0.6.jar:6.0.6]
(...)

Would you mind refactoring your GitHub project into an MCVE first before I can check on your actual problem? This way the error is not reproducible.

But having said this, I did notice something in your POM and Java files: Maybe the problem is not where you think it is. I can see that you want to use Lombok in combination with Spring AOP. According to my answer here, there are compatibility problems between AspectJ and Lombok. Maybe they also affect Spring AOP. So can you temporarily test without @Slf4j and other Lombok stuff? As soon as you will have fixed your project I can also test by myself.


Update after GitHub repo project has been repaired:

Now I can build and run your program, thanks. It seems that the parameter is somehow passed through to internal Spring classes you do not wish to target. So just modify your pointcut like this:

@After("@args(com.example.demosm.my.aop.NeedTestClass) && within(com.example.demosm..*)")
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • I fixed it on github as you said. Interesting, now the `@args` in `MyAspect.java` still causes the error, but it's different: `Cannot subclass final class org.springframework.boot.autoconfigure.AutoConfigurationPackages$BasePackages`. I commented `@args` in `MyAspect` so it could compile, you may uncomment it to see the error. – Tiina Jan 03 '18 at 01:10
  • I was thinking the same, it was targeted at a random place. As for the 1st day's `no constructor` error, maybe because it was in a larger demo projects, the random place happened at a different place. THX, `@args` better with a `within` or any more specific combined targeting form. – Tiina Jan 04 '18 at 03:36
  • About Lombok and Spring aop corruptions, I have not used Spring for that long, but the problem issued 3 years ago, maybe it is fixed? Besides, even though a spring user does not write aop in service, aop is used many places in spring framework itself, like transactional I guess, why Lombok + transactional rarely causes a problem? – Tiina Jan 04 '18 at 03:39
  • Then you might want to just re-introduce the Lombok annotations into your code in combination with the more specific `within(my.package..*) && @args(Blah)` pointcut and see what happens. I have never used Lombok myself, so I cannot tell you if it was "fixed". If you follow the link to the mailing list discussion in the answer I already pointed you to in previously, you can learn more about the problem's root cause. – kriegaex Jan 04 '18 at 15:45