如何为jax-rs编写spring AOP

时间:2016-04-19 07:17:02

标签: spring spring-mvc aop spring-aop

我是春季AOP的初学者。我的要求是在invokeTestServer方法执行之前记录。请在下面找到我的代码:

应用context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
                        http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/mvc 
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/util 
                        http://www.springframework.org/schema/util/spring-util.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop.xsd
                        http://www.springframework.org/schema/oxm 
                        http://www.springframework.org/schema/oxm/spring-oxm.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <aop:aspectj-autoproxy expose-proxy="true" />

    <context:component-scan base-package="mypackage.service" />

    <bean id="loggingAspect" class="mypackage.spring.InputAcceptorAspect" />

</beans>

当我试图将AOP与jax-rs集成时,我没有任何特定的bean要初始化

请在我的组件下面找到

package mypackage.service;

@Component
public class FirstProcess {

    public Response invokeTestServer() throws TestServerException{
            //Logic
        return response;
    }
}

我的观点如下 包mypackage.spring;

@Aspect

public class InputAcceptorAspect {

    @Before("execution(*mypackage.service.invokeTestServer(..))")
    public void logBeforeInvokingTestServer(JoinPoint joinpoint){
        logger.error("TestServer got invoked");
        System.out.println("TestServer got invoked");
    }
}

我正在尝试使用以下主要课程进行测试

package mypackage.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import mypackage.service.FirstProcess;

    public class TestAOP {

        public static void main(String args[])
        {
            ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
            FirstProcess manager = context.getBean(FirstProcess.class);
            manager.invokeTestServer();
        }
    }

执行上面的异常时,无法创建文件中定义的bean FirstProcess。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

在FirstProcess.java类中,您尝试使用注释创建bean,但在配置中不存在注释配置。在application-context.xml文件中添加此标记,以便使用注释配置

创建bean