使 Aspectj 在 Spring servlet bean 上工作
Making Aspectj work on a Spring servlet bean
我正在尝试让 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);
}
}
除了制作 Jersey 资源 类 Spring @Component
s(以及 @ComponentScan
ing 之外),您还需要制作 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>
另请参阅:
我正在尝试让 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);
}
}
除了制作 Jersey 资源 类 Spring @Component
s(以及 @ComponentScan
ing 之外),您还需要制作 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>
另请参阅: