Making Aspectj work on a Spring servlet bean

£可爱£侵袭症+ 提交于 2019-12-13 01:05:33

问题


I am trying to get an aspectprofiler working on a Jersey servlet registered in a spring project. The aspectprofiler is loaded, but don't notice when methods within the Jersey servlet are run.

@EnableAutoConfiguration
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class App {
    public static void main(final String[] args) {
        final SpringApplicationBuilder sab = new SpringApplicationBuilder(ConsolidatedCustomerMasterApp.class);
        sab.run(args);
    }

    @Bean
    public ServletRegistrationBean jerseyServlet() {
        final ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/*");
        registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyInitialization.class.getName());
        return registration;
    }

    @Bean
    public AspectProfiler profiler() {
        return new AspectProfiler();
    }
}

...

public class JerseyInitialization extends ResourceConfig {

    public JerseyInitialization() {
        packages("com.example.package");
    }

...

package com.example.package;

//imports
@Path("/test")
public class RestService {

    @GET
    @Path("test")
    @Produces(MediaType.TEXT_PLAIN)
    public String test() {
        return "Something";
    }
}

...

@Aspect
public class AspectProfiler {
    private static final DefaultApplicationProfiler PROFILER = new DefaultApplicationProfiler(
        Arrays.<ProfilerOperator> asList(
            new StatsdProfilerOperator(),
            new LoggingProfilerOperator())
            );

    private static final String REST_MATCHER =
            "execution(* com.example.package..*.*(..))";

    @Around(REST_MATCHER)
    public Object around(final ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("test");
        return PROFILER.around(joinPoint);
    }   
}

回答1:


On top of making the Jersey resource classes Spring @Components (and @ComponentScaning for them), you also need to make the ResourceConfig a Spring @Component also. You can see in the Spring Boot JerseyAutoConfigurer that it autowires the ResourceConfig, which it uses for the ServletContainer registration.

One thing to also note is that it creates its own ServletRegistrationBean

public ServletRegistrationBean jerseyServletRegistration() {
    ServletRegistrationBean registration = new ServletRegistrationBean(
            new ServletContainer(this.config), this.path);
    addInitParameters(registration);
    registration.setName("jerseyServlet");
    return registration;
}

When you declare your own, you are overriding this one. You are not adding any special functionality that is not already provided, so just leave the default. Any Jersey specific configurations can be added in the application.properties file or through code configurations.

As for dependencies, I'll just assume you have all the right dependencies. The following are what I used to test

<!-- all 1.2.7.RELEASE -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

See Also:

  • spring-boot-sample-jersey - from project samples
  • §26.2 JAX-RS and Jersey - from Spring Boot docs


来源:https://stackoverflow.com/questions/33625937/making-aspectj-work-on-a-spring-servlet-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!