使Aspectj在Spring servlet bean上工作 [英] Making Aspectj work on a Spring servlet bean
问题描述
我试图让一个AspectProfiler在Spring项目中注册的Jersey Servlet上工作. Aspectprofiler已加载,但在运行Jersey Servlet中的方法时不会注意到.
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);
}
}
推荐答案
在将Jersey资源类设为Spring @Component
s(并为其进行@ComponentScan
ing)的基础上,还需要使ResourceConfig
a春天@Component
也.您可以在
On top of making the Jersey resource classes Spring @Component
s (and @ComponentScan
ing 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.
还要注意的一件事是,它创建了自己的ServletRegistrationBean
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;
}
声明自己的名称时,您将覆盖此名称.您不会添加尚未提供的任何特殊功能,因此只需保留默认设置即可.可以在application.properties
文件中或通过代码配置添加任何特定于Jersey的配置.
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>
另请参见:
- spring-boot-sample-jersey - from project samples
- §26.2 JAX-RS and Jersey - from Spring Boot docs
这篇关于使Aspectj在Spring servlet bean上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!