使Aspectj在Spring servlet bean上运行

时间:2015-11-10 08:45:21

标签: spring-boot aop aspectj jersey-2.0

我正在尝试让一个Aspectprofiler处理在spring项目中注册的Jersey servlet。加载了aspectprofiler,但是没有注意到Jersey servlet中的方法何时运行。

@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 个答案:

答案 0 :(得分:1)

除了使Jersey资源类为Spring @Component(以及@ComponentScan)之外,还需要使ResourceConfig成为Spring @Component。您可以在Spring Boot JerseyAutoConfigurer中看到它自动装配ResourceConfig,用于ServletContainer注册。

还要注意的一点是,它会创建自己的ServletRegistrationBean

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

当你宣布自己的时候,你就是压倒这个。您没有添加任何尚未提供的特殊功能,因此请保留默认值。可以在application.properties文件中或通过代码配置添加任何Jersey特定配置。

至于依赖关系,我假设您拥有所有正确的依赖关系。以下是我用来测试的内容

<!-- 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>

另见: