相关疑难解决方法(0)

使用AspectJ模拟接口和方法的注释继承

人们经常会问AspectJ这样的问题,所以我想在以后可以轻松链接的地方回答.

我有这个标记注释:

package de.scrum_master.app;

import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface Marker {}
Run Code Online (Sandbox Code Playgroud)

现在我注释一个接口和/或方法,如下所示:

package de.scrum_master.app;

@Marker
public interface MyInterface {
  void one();
  @Marker void two();
}
Run Code Online (Sandbox Code Playgroud)

这是一个小驱动程序应用程序,它也实现了接口:

package de.scrum_master.app;

public class Application implements MyInterface {
  @Override
  public void one() {}

  @Override
  public void two() {}

  public static void main(String[] args) {
    Application application = new Application();
    application.one();
    application.two();
  }
}
Run Code Online (Sandbox Code Playgroud)

现在,当我定义这个方面时,我希望它被触发

  • 为每个构造函数执行带注释的类和
  • 每次执行带注释的方法.
package de.scrum_master.aspect;

import de.scrum_master.app.Marker;

public aspect MarkerAnnotationInterceptor {
  after() : execution((@Marker *).new(..)) && …
Run Code Online (Sandbox Code Playgroud)

java inheritance annotations aspectj

6
推荐指数
1
解决办法
2109
查看次数

Spring Data Repository 的方面建议不起作用

我正在尝试为存储库创建一些切入点和之前的建议,以便为 Spring Boot 中 Spring Data 中的某些存储库启用对 entitymanager 的过滤。我在项目中也有 Web 和服务层,并且 AspectLogging 对两者都适用。但是我不能对存储库做同样的事情。我已经挣扎了 2 天,我尝试了很多方法来修复它。我阅读了几乎所有关于此的文档、问题和线程(代理问题 CGlib 和 JDK 代理等)。我使用 jhipster 来创建项目。

除了@Pointcut 和 CrudRepository,我无法部署应用程序。甚至它部署的 @Before 也没有在 Repository 中调用方法调用。我想我有一个类似的问题,如以下问题。代理混淆

    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.xxx.zzz.business.repository.ApplyRepository com.xxx.zzz.web.rest.applyResource.ApplyRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'applyRepository': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy173]: Common causes of this problem include using a final class or a …
Run Code Online (Sandbox Code Playgroud)

java proxy aspectj spring-aop spring-data-jpa

5
推荐指数
1
解决办法
8658
查看次数

在Spring JPA中保存实体之前的业务逻辑

我正在使用Spring Boot 1.5.4,Spring Data REST,Spring JPA,Hibernate,并且正在开发使用REST API的Angular客户端。

Spring Data REST很有帮助,我正在尝试遵循最佳实践,因此存储库类似于:

@Transactional
@PreAuthorize("isAuthenticated()")
public interface CustomerRepository extends PagingAndSortingRepository<Customer, Long> {
}
Run Code Online (Sandbox Code Playgroud)

并自动地拥有了所有的save(),delete()和findXX()方法。那很棒。

现在,我想知道在保存实体之前是否需要自定义业务逻辑来执行。假设我需要进行某种复杂的验证(涉及对db的查询),以及其他后台活动(例如,保存相关实体,更新相关对象等)。我的目标是:

  1. 确保每次保存实体(通过REST调用或JPA调用)时,在保存对象之前都会调用我的业务逻辑
  2. 避免创建自定义存储库,因为开发人员可以调用标准存储库违反我的规则
  3. 找到一种简单的方法来保持应用程序易于维护

@RepositoryEventHandler对我来说还不够,因为我想确保始终对我的业务逻辑进行验证,即使对该方法的调用来自内部类也是如此。

您能建议我实现目标的最佳方法吗?

spring spring-data-jpa spring-data-rest spring-boot

5
推荐指数
1
解决办法
2924
查看次数