Spring restful 网络服务的 AOP

Spring AOP for restful web service

我指的是 Spring Mkyong 的 AOP http://www.mkyong.com/spring/spring-aop-example-pointcut-advisor/

如上 link,

所示,尝试从 main(即 App.java)运行 时有效

我想将它集成到 restful 网络服务中,在 mkyong 的示例中我有多个服务,例如 CutomerService。

例如,我有调用 CustomerService 的控制器,

@RestController
@RequestMapping(value = "/customer")
public class CustomerController{

@Autowired CustomerService customerService;

@RequestMapping(value = "/getCustomer", method = RequestMethod.GET")
    public ResponseEntity<CommonResponse> getService(){
        customerService.printName();
    }
}

没用。

我也试过这个:

@Autowired
private ProxyFactoryBean customerServiceProxy;

@RequestMapping(value = "/getCustomer", method = RequestMethod.GET")
    public ResponseEntity<CommonResponse> getService(){
    CustomerService customerService = (CustomerService) customerServiceProxy
                    .getTargetSource().getTarget();
        customerService.printName();
    }
}

这个剂量也有效。

有什么解决办法吗?

我的bean-config.xml和mkyong的例子一样

成功了!只需要将代理 class 从 "org.springframework.aop.framework.ProxyFactoryBean" 更改为 "org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"

自动代理创建器通过名称列表识别要代理的 bean。所以我们不需要指定目标 bean,而是可以指定 bean 列表,它会自动拦截。 我的配置如下所示:

<bean id="adviceAround" class="com.tdg.ess.semantic.event.log.AdviceAround" />
    <!-- Event logging for Service -->
    <bean id="testServicePointCut" class="org.springframework.aop.support.NameMatchMethodPointcut">
        <property name="mappedNames">
            <list>
                <value>process</value>
            </list>
        </property>
    </bean>

    <bean id="testServiceAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <property name="pointcut" ref="testServicePointCut" />
        <property name="advice" ref="adviceAround" />
    </bean>

    <bean
        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
            <list>
            <!-- Differnt beans which have certain service defined like CustomerService-->
                <value>testService1</value>
                <value>testService2</value>
            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>testServiceAdvisor</value>
            </list>
        </property>
    </bean>

感谢大家的关注