Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带Spring引导建议的AOP未触发_Java_Spring_Spring Boot_Annotations_Aop - Fatal编程技术网

Java 带Spring引导建议的AOP未触发

Java 带Spring引导建议的AOP未触发,java,spring,spring-boot,annotations,aop,Java,Spring,Spring Boot,Annotations,Aop,使用:Spring Boot::(v1.2.8.版本) 我已经在build.gradle中使用aop启动程序设置了一个Spring启动应用程序 compile("org.springframework.boot:spring-boot-starter-aop") 我已检查并获得依赖项: | | | | +--- org.springframework:spring-aop:4.1.9.RELEASE | | | | |

使用:Spring Boot::(v1.2.8.版本)

我已经在build.gradle中使用aop启动程序设置了一个Spring启动应用程序

compile("org.springframework.boot:spring-boot-starter-aop")
我已检查并获得依赖项:

|    |    |    |         +--- org.springframework:spring-aop:4.1.9.RELEASE
|    |    |    |         |    +--- aopalliance:aopalliance:1.0
这是AspectConfig:

@Configuration
@ComponentScan
@EnableAspectJAutoProxy
public class AspectConfig {
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}
我将配置类放在应用程序层次结构的基础上,这样组件扫描就可以覆盖整个应用程序。这是所有的原型代码,但它最终将形成一个启动模块的一部分,扫描所有区域的能力将是有益的

现在我定义了一个注释:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface AutowiredRestTemplate {
    String name();
    String methodUrl();
}
并有一个测试方法:

@Component(value = "testGateway")
public class TestGatewayImpl implements TestGateway {
    private static final Logger LOG = LoggerFactory.getLogger(TestGatewayImpl.class);

    AuspostRestTemplate restTemplate;

    @AutowiredRestTemplate(name = "locations", methodUrl = "/fishing")
    public Response doWork() {
        LOG.debug("Got into gateway with restTemplate {}", restTemplate);
        return restTemplate.getForObject(Response.class);
    }
}
现在的建议是:

@Aspect
@Component
public class AutowiredRestTemplateAspect {

    @Autowired
    Map<String, AuspostRestTemplate> restTemplateMap;

    @Autowired
    private ApplicationContext context;

    @Pointcut("execution(public * *(..))")
    public void anyPublicMethod(){}

    @Around("anyPublicMethod() && @annotation(autowiredRestTemplate)")
    public Object inAnyMethod(ProceedingJoinPoint pjp, AutowiredRestTemplate autowiredRestTemplate) throws Throwable{

        AuspostRestTemplate restTemplate = restTemplateMap.get(autowiredRestTemplate.name());
        restTemplate.setMethodUrl(autowiredRestTemplate.methodUrl());
            pjp.getTarget().getClass().getDeclaredField("restTemplate").set(pjp.getTarget(),restTemplate);
        return pjp.proceed();

    }
}
@方面
@组成部分
公共类AutowiredRestTemplateAspect{
@自动连线
地图模板地图;
@自动连线
私有应用程序上下文上下文;
@切入点(“执行(公共**(…)”)
public void anyPublicMethod(){}
@周围(“anyPublicMethod()&&&@注释(autowiredRestTemplate)”)
MyMethod中的公共对象(ProceedingJoinPoint pjp,AutowiredRestTemplate AutowiredRestTemplate)抛出可丢弃{
AuspostRestTemplate restTemplate=restTemplateMap.get(autowiredRestTemplate.name());
restTemplate.setMethodUrl(autowiredRestTemplate.methodUrl());
pjp.getTarget().getClass().getDeclaredField(“restTemplate”).set(pjp.getTarget(),restTemplate);
返回pjp.procedure();
}
}
问题是运行
doWork()
方法时,不会触发通知。从日志中甚至可以看出切入点甚至没有设置。有人能看出这里出了什么问题吗

编辑:我已经为我想要使用的注释添加了配置注释、保留注释和目标注释(在本问题的上面)。
EDIT2:更改了Configuration类上的ComponentScan,因为另一件事情很复杂,而且无论如何都不起作用。

您是否尝试放置
@enableSpectJautoproxy(proxyTargetClass=true)
在您的配置类上?

这是您的全部注释吗?您也知道您正在做的事情有潜在的危险吗?一个模板可以在多个请求上覆盖另一个模板。哦,我想我应该包括设置requestTemplates映射的所有配置。不会覆盖任何内容,只会从映射中选择模板(在应用程序启动时加载)。我只是没有包括所有这些东西,因为它似乎与这个问题无关,仍然有一个特定名称的单一实例。现在,如果您有多个线程需要单个实例,那么您正在更改一个已经在使用的实例。。。这不相关,我只是想指出这一点。正如我在第一篇评论中提到的,您的整个注释以及您是否可以添加您正在使用的SpringBoot版本。爪哇8。是的,这是整个注释。我的印象是运行时是默认的RetentionPolicy,而目标是默认的一切。我确实尝试设置了这两种方法(包括@Target({ElementType.METHOD}),但没有任何区别。所以,是的,这就是整个注释。是的,我做了。我刚刚将配置类添加到问题中,因为它看起来非常相关。