Spring AOP用于提供宁静的Web服务

时间:2016-02-24 12:29:25

标签: java spring rest spring-aop

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

提到Spring AOP

尝试从main(即App.java)运行时,如上面链接所示,

我想将它集成到restful webservice中,我在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的例子相同。

1 个答案:

答案 0 :(得分:0)

有效!只需要将代码类从“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>

感谢大家的关注。

相关问题