无法在Spring-Apache CXF服务中使用@AspectJ

时间:2017-03-11 12:10:10

标签: spring cxf aspectj

我是Spring的新手,正在使用Spring和Apache CXF与Java配置编写的休息服务。我有以下休息服务。

@Path("/release/")
@Component
@RestService
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public class ReleaseResource extends AbstractService implements IResource {


    @Override
    @CustomLogger
    @GET
    public Response get() {
        //Some Logic
        return Response.ok("Success!!").build();
    }

}

我使用@AspectJ创建了一个方面用于记录。但是,该方面不适用于使用CXF编写的服务。我在网上做了一些搜索,发现Spring需要代理bean才能使方面工作。然后我尝试了一些方法,如

  1. 使服务类实现接口
  2. 使用CGLIB库和范围代理模式TARGET_CLASS
  3. 使用方法

    扩展类
    @Override
    public void setMessageContext(MessageContext context) {
        this.context = context;
    }
    
  4. 但他们都没有奏效。 是否有可能围绕服务运行方面? 如果是,有人可以告诉我如何做。

    我已经读过这可以通过字节码手动编织aspectj而不是使用spring aspectj autoproxy来实现(不知道怎么做)。有人能告诉我这是不是一个好的选择以及怎么做?

    编辑:

    很抱歉提供的信息不完整。附加其他类

    @Aspect
    @Configuration
    public class LoggerAspect {
    
    
        @Pointcut(value = "execution(* *(..))")
        public void anyPublicMethod() {
        }
    
        @Around("anyPublicMethod() && @annotation(CustomLogger)")
        public Object logAction(ProceedingJoinPoint pjp, CustomLogger customLogger) throws Throwable {
    
            //Log Some Info
            return pjp.proceed();
    
        }
    }
    

    Web Initializer类:

    @Configuration
    public class WebInitializer implements WebApplicationInitializer {
    
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
    
            servletContext.addListener(new ContextLoaderListener(createWebAppContext()));
            addApacheCxfServlet(servletContext);
    
        }
    
        private void addApacheCxfServlet(ServletContext servletContext) {
            CXFServlet cxfServlet = new CXFServlet();
    
            ServletRegistration.Dynamic appServlet = servletContext.addServlet("CXFServlet", cxfServlet);
            appServlet.setLoadOnStartup(1);
            appServlet.addMapping("/*");
        }
    
        private WebApplicationContext createWebAppContext() {
            AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
            appContext.register(TestConfig.class);
    
            return appContext;
        }
    
    }
    

    配置类:

    @Configuration
    @ComponentScan(basePackages = "com.my.package")
    @EnableAspectJAutoProxy(proxyTargetClass = true)
    public class TestConfig {
        private static final String RESOURCES_PACKAGE = "com.my.package";
    
        @ApplicationPath("/")
        public class JaxRsApiApplication extends Application {
        }
    
        @Bean(destroyMethod = "shutdown")
        public SpringBus cxf() {
            return new SpringBus();
        }
    
        @Bean
        public JacksonJsonProvider jacksonJsonProvider() {
            return new JacksonJsonProvider();
        }
    
        @Bean
        public LoggerAspect getLoggerAspect() {
            return new LoggerAspect();
        }
    
        @Bean
        IResource getReleaseResource() {
            return new ReleaseResource();
        }
    
        @Bean
        @DependsOn("cxf")
        public Server jaxRsServer(ApplicationContext appContext) {
            JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance().createEndpoint(jaxRsApiApplication(),
                    JAXRSServerFactoryBean.class);
    
            factory.setServiceBeans(restServiceList(appContext));
            factory.setProvider(jacksonJsonProvider());
    
            return factory.create();
        }
    
        private List<Object> restServiceList(ApplicationContext appContext) {
            return RestServiceBeanScanner.scan(appContext, TestConfig.RESOURCES_PACKAGE);
        }
    
        @Bean
        public JaxRsApiApplication jaxRsApiApplication() {
            return new JaxRsApiApplication();
        }
    
    }
    

    RestServiceBeanScanner类

    public final class RestServiceBeanScanner {
    
        private RestServiceBeanScanner() {
        }
    
        public static List<Object> scan(ApplicationContext applicationContext, String... basePackages) {
            GenericApplicationContext genericAppContext = new GenericApplicationContext();
            ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(genericAppContext, false);
    
            scanner.addIncludeFilter(new AnnotationTypeFilter(RestService.class));
            scanner.scan(basePackages);
            genericAppContext.setParent(applicationContext);
            genericAppContext.refresh();
    
            List<Object> restResources = new ArrayList<>(
                    genericAppContext.getBeansWithAnnotation(RestService.class).values());
    
            return restResources;
        }
    
    }
    

0 个答案:

没有答案