人们经常会问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) 我正在尝试为存储库创建一些切入点和之前的建议,以便为 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) 我正在使用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的查询),以及其他后台活动(例如,保存相关实体,更新相关对象等)。我的目标是:
这@RepositoryEventHandler对我来说还不够,因为我想确保始终对我的业务逻辑进行验证,即使对该方法的调用来自内部类也是如此。
您能建议我实现目标的最佳方法吗?
aspectj ×2
java ×2
annotations ×1
inheritance ×1
proxy ×1
spring ×1
spring-aop ×1
spring-boot ×1