首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使Aspectj在servlet bean上工作

使Aspectj在servlet bean上工作
EN

Stack Overflow用户
提问于 2015-11-10 08:45:20
回答 1查看 573关注 0票数 1

我正在尝试让aspectprofiler处理在spring项目中注册的Jersey。将加载aspectprofiler,但不要注意何时运行Jersey中的方法。

代码语言:javascript
运行
复制
@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();
    }
}

..。

代码语言:javascript
运行
复制
public class JerseyInitialization extends ResourceConfig {

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

..。

代码语言:javascript
运行
复制
package com.example.package;

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

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

..。

代码语言:javascript
运行
复制
@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);
    }   
}
EN

Stack Overflow用户

回答已采纳

发布于 2015-11-13 14:40:14

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

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

代码语言:javascript
运行
复制
public ServletRegistrationBean jerseyServletRegistration() {
    ServletRegistrationBean registration = new ServletRegistrationBean(
            new ServletContainer(this.config), this.path);
    addInitParameters(registration);
    registration.setName("jerseyServlet");
    return registration;
}

当你声明你自己的时候,你就是在推翻这一条。您没有添加任何尚未提供的特殊功能,所以只需保留默认值即可。任何特定于泽西岛的配置都可以添加到application.properties文件中或通过代码配置。

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

代码语言:javascript
运行
复制
<!-- 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>

还请参见:

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33625937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档