当前位置: 首页 > 知识库问答 >
问题:

在JpaRepository上调用delete方法时未调用AspectJ方法

夏骏
2023-03-14

我在一个名为seedrecord的对象和一个名为FielliateLink的对象之间有一个manytomany关系。为了删除FielliateLink,我需要首先从每个SeedRecord的FielliateList中删除对它的引用。之后,我使用spring JParepository的delete方法删除对象。因为我的服务(FiniliateLinkService)中有不同的删除方法,所以我决定将这段代码放在AspectJ类中。但是,当调用deletemethod时,aspectJ方法不会在此之前调用。谁能看看我的代码,告诉我哪里错了?

@Before("execution(* org.springframework.data.jpa.repository.JpaRepository.delete(..))" +
        "&& within(Services.AffiliateLinkService) && args(entity)")
private void deleteAffiliateLinkFromSeedRecordAffiliateLists(Object entity){
    log.info("hailing from SystemListenerService!");
    ((AffiliateLink)entity).getSeedRecords()
            .forEach(seedRecord -> {
                seedRecord.getAffiliateLinks()
                        .remove(seedRecord);
                seedRecordDao.save(seedRecord);
            });
}
@Before("execution(* Services.AffiliateLinkService.deleteMe(..))" +
        "&& args(entity)")

另外,为了便于您了解,我已经将方面声明为bean,并用@enableaspectJAutoProxy声明了配置类

编辑2:当我在@afterthrough(“execution(**(..))”)中更改@before时,事情变得更加奇怪。

然后在应用程序启动过程中突然出现这个错误。

行动:

考虑将bean作为接口之一注入,或者通过在@enableAsync和/或@enableCaching上设置ProxyTargetClass=true,强制使用基于CGlib的代理。

问题是我没有在哪里显式声明名为DataSourceInitializer的bean,所以我不能用@enable this或that来注释它...天哪,我多么讨厌Spring。

编辑3:它与我使用的参数AffiliateLink有关。例如,当我用long替换AffiliateLink参数时,before方法被调用…这就是AffiliateLink POJO。但我不认为pojo有任何不寻常的地方会导致这种情况:

@Entity
public class AffiliateLink implements Serializable {
    @Id
    @GeneratedValue(generator = "ID_GENERATOR")
    private Long id;

    @URL
    private String affiliateUrl;

    @URL
    private String affiliateImageUrl;

    private String title;

    private String description;

    private Double productValue;

    private boolean general;

    private byte rank;

    private boolean linkBroken;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(updatable = false)
    @CreationTimestamp
    private Date creationDate;


    @ElementCollection @CollectionTable( name = "affiliate_keywords", joinColumns=@JoinColumn(name = "id", referencedColumnName = "id") ) @Column(name="keyword")
    private Set<String> keywords;

    @ManyToMany(mappedBy = "affiliateLinks")
    private Set<SeedRecord> seedRecords = new HashSet<>();

    @Enumerated(EnumType.STRING)
    private LocalizedStorefront localizedStorefront;

    private long seedId;

    private boolean plantClimbs;

    private boolean spicy;

    private boolean teaPlant;

    public AffiliateLink() { }

    public AffiliateLink(AffiliateLinkCreateDTO affiliateLinkCreateDTO) {
        this.title = affiliateLinkCreateDTO.getTitle();
        this.description = affiliateLinkCreateDTO.getDescription();
        this.general = affiliateLinkCreateDTO.isGeneral();
        this.rank = affiliateLinkCreateDTO.getRank();
        this.localizedStorefront = affiliateLinkCreateDTO.getLocalizedStorefront();
        this.productValue = affiliateLinkCreateDTO.getProductValue();
        this.keywords = affiliateLinkCreateDTO.getKeywords();
        this.seedId = affiliateLinkCreateDTO.getSeedId() != null ? affiliateLinkCreateDTO.getSeedId() : 0;
    }

    public AffiliateLink(String affiliateUrl,
                         String affiliateImageUrl,
                         String title,
                         String description,
                         Double productValue,
                         boolean general,
                         byte rank,
                         boolean linkBroken,
                         Set<String> keywords,
                         LocalizedStorefront localizedStorefront,
                         long seedId,
                         boolean plantClimbs,
                         boolean spicy,
                         boolean teaPlant) {
        this.affiliateUrl = affiliateUrl;
        this.affiliateImageUrl = affiliateImageUrl;
        this.title = title;
        this.description = description;
        this.productValue = productValue;
        this.general = general;
        this.rank = rank;
        this.linkBroken = linkBroken;
        this.keywords = keywords;
        this.localizedStorefront = localizedStorefront;
        this.seedId = seedId;
        this.plantClimbs = plantClimbs;
        this.spicy = spicy;
        this.teaPlant = teaPlant;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public boolean isPlantClimbs() {
        return plantClimbs;
    }

    public void setPlantClimbs(boolean plantClimbs) {
        this.plantClimbs = plantClimbs;
    }

    public boolean isSpicy() {
        return spicy;
    }

    public void setSpicy(boolean spicy) {
        this.spicy = spicy;
    }

    public boolean isTeaPlant() {
        return teaPlant;
    }

    public void setTeaPlant(boolean teaPlant) {
        this.teaPlant = teaPlant;
    }

    public long getSeedId() {
        return seedId;
    }

    public void setSeedId(long seedId) {
        this.seedId = seedId;
    }

    public Double getProductValue() {
        return productValue;
    }

    public void setProductValue(Double productValue) {
        this.productValue = productValue;
    }

    public String getAffiliateImageUrl() {
        return affiliateImageUrl;
    }

    public boolean isLinkBroken() {
        return linkBroken;
    }

    public void setLinkBroken(boolean linkBroken) {
        this.linkBroken = linkBroken;
    }

    public void setAffiliateImageUrl(String affiliateImageUrl) {
        this.affiliateImageUrl = affiliateImageUrl;
    }

    public String getAffiliateUrl() {
        return affiliateUrl;
    }

    public void setAffiliateUrl(String affiliateUrl) {
        this.affiliateUrl = affiliateUrl;
    }

    public String getTitle() {
        return title;
    }

    public Set<String> getKeywords() {
        return keywords;
    }

    public void setKeywords(Set<String> keywords) {
        this.keywords = keywords;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public boolean isGeneral() {
        return general;
    }

    public void setGeneral(boolean general) {
        this.general = general;
    }

    public byte getRank() {
        return rank;
    }

    public void setRank(byte rank) {
        this.rank = rank;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set<SeedRecord> getSeedRecords() {
        return seedRecords;
    }

    public void setSeedRecords(Set<SeedRecord> seedRecords) {
        this.seedRecords = seedRecords;
    }

    public LocalizedStorefront getLocalizedStorefront() {
        return localizedStorefront;
    }

    public void setLocalizedStorefront(LocalizedStorefront localizedStorefront) {
        this.localizedStorefront = localizedStorefront;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        AffiliateLink that = (AffiliateLink) o;
        return general == that.general &&
                rank == that.rank &&
                Objects.equals(id, that.id) &&
                Objects.equals(affiliateUrl, that.affiliateUrl) &&
                Objects.equals(affiliateImageUrl, that.affiliateImageUrl) &&
                Objects.equals(title, that.title) &&
                Objects.equals(description, that.description) &&
                Objects.equals(productValue, that.productValue) &&
                localizedStorefront == that.localizedStorefront;
    }

    @Override
    public int hashCode() {

        return Objects.hash(id, affiliateUrl, affiliateImageUrl, title, description, productValue, general, rank, localizedStorefront);
    }
}

共有1个答案

经福
2023-03-14

我在Spring论坛上用这个帖子解决了这个问题。http://forum.spring.io/forum/spring-projects/aop/100146-aspect-advision-in-neve-called

一个方法应该是公共的,而不是静态的,并且应该在外部调用(这意味着该方法应该在另一个类中)。因此,我将deleteMe方法移到另一个类中,并从那里调用它,现在@before方面开始工作。

 类似资料:
  • 如果程序员返回的是Arraylist而不是List,我会尝试生成一个警告。我使用Spring Boot,Spring Data JPA。 Pojo示例 我的存储库:

  • 所以我试图实现类似于Kotlin协程的东西在Java 目前,我试图拦截方法时,一个方法调用Waitable.waitFor()调用和所有的方法主体后,Waitable.waitFor()应调度 但在我当前的代码中,它只拦截方法调用,之后的方法体不包括在内 所以我想知道我的切入点表达式代码是错的吗?或者有什么更好的方法来实现这一点?

  • 我正在使用JSF 2.0创建页面: 我期待在单击复选框时被调用。但是,加载页面时调用一次setMyCheckboxValue()。 如果我写 每次点击都会收到警报。 我的问题是:当我收到警报时,为什么每次onclick事件都不调用? 注意:我也尝试过AJAX,但复选框保持不变。

  • 要在实例上调用方法,您必须获取实例的引用并调用该方法。该示例说明了如何获取引用和调用方法,检查 API 以获取可用方法的列表。 // 3 ways of doing the same thing $('#jstree').jstree(true) .select_node('mn1'); $('#jstree') .jstree('select_node', 'mn2'); $.

  • 问题内容: 我只是实现了继承JPanel的类,如下所示 Orpanel类正在加载图像并调整其自身大小。 这是问题。 调用JFrame的setContentpane(Orpanel的实例)使其工作正常,但是当我将Orpanel附加到JFrame时,调用add()方法而不是setContentpane(我知道setcontentpane并不意味着attach ..反正),它就行不通了。 终于弄清楚了当

  • 我已经尝试了这个平台上提供的所有答案,但没有成功。我正在执行这个命令行运行程序,但是run方法没有调用。 我感谢你的帮助。 谢谢 我尝试了以下解决方案,但得到了这个例外。 第一:在您的引导类(具有SpringApplication.run(…,args)的主方法的类)中,您可以在@SpringBootApplication注释处添加属性scanBasePackages: @SpringBootAp