我在一个叫做seedrecord的对象和一个名为affiliatelink的对象之间有很多关系。为了删除affiliatelink,我需要首先从每个seedrecord的affiliateList中删除对其的引用。之后,我使用spring Jparepository的delete方法删除对象。因为我的服务(AffiliateLinkService)中有不同的删除方法,所以我决定将这段代码放入AspectJ类中。但是,在调用delete方法时,在此之前不会调用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);
});
}
编辑:问题不是由jparepository方法引起的。因为当我使用称为deleteMe()的包装器方法更改delete方法并让Aspect方法在此包装器方法之前运行时,它也不起作用。
@Before("execution(* Services.AffiliateLinkService.deleteMe(..))" +
"&& args(entity)")
为您提供信息,我将方面声明为Bean,将配置类声明为@EnableAspectJAutoProxy
编辑2:当我在@Afterthrowing("execution(* *(..))")
中更改@Before时,事情变得更加奇怪。
然后突然在应用程序启动期间出现此错误。
说明:
bean'dataSourceInitializer'不能作为 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer' 因为它是实现以下功能的JDK动态代理: org.springframework.context.ApplicationListener
操作:
考虑将Bean作为其接口之一注入或强制 通过在上设置proxyTargetClass = true来使用基于CGLib的代理 @EnableAsync和/或@EnableCaching。
问题是我无处明确声明了一个名为dataSourceInitializer
的bean,所以我无法通过@Enable this或that对其进行注释...天哪,我讨厌Spring。
PS:在返回jparepository的save方法后,我有另一种方法被调用,它的作用是吸引人,所以我不明白为什么删除建议不起作用。这是工作方法的注释。
@AfterReturning(“执行(* org.springframework.data.jpa.repository.JpaRepository.save(..))” + “ &&!within(Services.SystemListenerService)&& args(entity)”)
编辑3:它与我使用的参数AffiliateLink有关。例如,当我将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);
}
}
答案 0 :(得分:0)
在春季论坛上,我通过这篇文章解决了问题。 http://forum.spring.io/forum/spring-projects/aop/100146-aspect-advice-is-never-called
方法应该是公共的而不是静态的,并在外部调用(这意味着该方法应该在另一个类中)。因此,我将deleteMe方法移至另一个类,并从那里调用它,现在@Before方面起作用了。