【问题标题】:Spring AOP No visible constructors in classSpring AOP 类中没有可见的构造函数
【发布时间】:2018-01-02 02:56:20
【问题描述】:

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

我主要使用link中给出的示例,下面的代码可以在github repository找到

注释:

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

方面:

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

服务:

@Slf4j
@Component
public class BusinessService {

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

Pojo(顶级,不是子类):

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

测试:

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

我试图检查 aop 和带注释的类继承,但第一步似乎不行。我试过 @annotation() 和 this() PCD 都可以。

编辑: 到目前为止,我想知道这个错误可能与 bean 加载顺序有关。

【问题讨论】:

    标签: spring-aop


    【解决方案1】:

    您的 GitHub 项目甚至无法编译。你甚至测试过它吗?首先通过反复试验,我必须添加所有这些依赖项:

    <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>
    

    接下来,我注意到 Maven 构建似乎没有启动本地 (127.0.0.1) 数据库,因为 Spring Boot 在启动时会这样说:

    (...)
    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]
    (...)
    

    您介意先将您的 GitHub 项目重构为 MCVE,然后我才能检查您的实际问题吗?这样错误就无法重现了。

    但话虽如此,我确实注意到您的 POM 和 Java 文件中的某些内容:也许问题不在您认为的位置。我可以看到您想将 Lombok 与 Spring AOP 结合使用。根据我的回答here,AspectJ 和 Lombok 之间存在兼容性问题。也许它们也会影响 Spring AOP。那么你可以在没有@Slf4j 和其他龙目岛的东西的情况下临时测试吗?一旦你修复了你的项目,我也可以自己测试。


    GitHub repo 项目修复后更新:

    现在我可以构建和运行您的程序了,谢谢。似乎该参数以某种方式传递给您不希望定位的内部 Spring 类。所以只需像这样修改你的切入点:

    @After("@args(com.example.demosm.my.aop.NeedTestClass) && within(com.example.demosm..*)")
    

    【讨论】:

    • 如你所说,我已将其修复在 github 上。有趣的是,现在MyAspect.java 中的@args 仍然会导致错误,但它有所不同:Cannot subclass final class org.springframework.boot.autoconfigure.AutoConfigurationPackages$BasePackages。我在MyAspect 中注释了@args,所以它可以编译,您可以取消注释以查看错误。
    • 我也是这么想的,它是针对一个随机的地方。至于第一天的no constructor错误,可能是因为在较大的demo项目中,随机出现在不同的地方。 THX、@args 最好使用 within 或任何更具体的组合定位表单。
    • 关于 Lombok 和 Spring aop 损坏,我很久没有使用 Spring,但是 3 年前的问题,也许已经解决了?另外,虽然spring用户没有在service中写aop,但是spring框架本身很多地方都用到了aop,比如事务性我猜,为什么Lombok+事务性很少出问题呢?
    • 那么您可能只想将 Lombok 注释与更具体的 within(my.package..*) &amp;&amp; @args(Blah) 切入点结合重新引入您的代码中,然后看看会发生什么。我自己从未使用过 Lombok,所以我不能告诉你它是否“固定”。如果您点击我之前已经指出您的答案中的邮件列表讨论的链接,您可以了解有关问题根本原因的更多信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多