我正在尝试让aspectprofiler处理在spring项目中注册的Jersey。将加载aspectprofiler,但不要注意何时运行Jersey中的方法。
@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);
}
}发布于 2015-11-13 14:40:14
除了让泽西资源类Spring@Component(以及@ComponentScan)之外,您还需要使ResourceConfig成为Spring @Component。您可以在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文件中或通过代码配置。
至于依赖项,我将假设您拥有所有正确的依赖项。下面是我用来测试的内容
<!-- 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>还请参见:
https://stackoverflow.com/questions/33625937
复制相似问题