Java restfulweb服务的springaop
我指的是Mkyong的Spring AOP 如上面链接所示,当尝试从main(即App.java)运行时,它起作用 我想将它集成到RESTfulWebService中,在这里我有多个服务,比如mkyong示例中的CutomerService 例如,我有一个调用CustomerService的控制器Java restfulweb服务的springaop,java,spring,rest,spring-aop,Java,Spring,Rest,Spring Aop,我指的是Mkyong的Spring AOP 如上面链接所示,当尝试从main(即App.java)运行时,它起作用 我想将它集成到RESTfulWebService中,在这里我有多个服务,比如mkyong示例中的CutomerService 例如,我有一个调用CustomerService的控制器 @RestController @RequestMapping(value = "/customer") public class CustomerController{ @Autowired Cu
@RestController
@RequestMapping(value = "/customer")
public class CustomerController{
@Autowired CustomerService customerService;
@RequestMapping(value = "/getCustomer", method = RequestMethod.GET")
public ResponseEntity<CommonResponse> getService(){
customerService.printName();
}
}
@RestController
@请求映射(value=“/customer”)
公共类客户控制器{
@自动连线客户服务客户服务;
@RequestMapping(value=“/getCustomer”,method=RequestMethod.GET”)
公共响应服务(){
customerService.printName();
}
}
它不起作用
我也试过:
@Autowired
private ProxyFactoryBean customerServiceProxy;
@RequestMapping(value = "/getCustomer", method = RequestMethod.GET")
public ResponseEntity<CommonResponse> getService(){
CustomerService customerService = (CustomerService) customerServiceProxy
.getTargetSource().getTarget();
customerService.printName();
}
}
@Autowired
私有ProxyFactoryBean customerServiceProxy;
@RequestMapping(value=“/getCustomer”,method=RequestMethod.GET”)
公共响应服务(){
CustomerService CustomerService=(CustomerService)CustomerService Proxy
.getTargetSource().getTarget();
customerService.printName();
}
}
这也不管用
有什么解决办法吗
我的bean-config.xml与mkyong的示例相同。它成功了!只需要将代理类从“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>
过程
测试服务1
测试服务2
测试服务顾问
感谢大家的关注。您能否澄清一下:您是否希望在某个应用程序服务器上而不是在主方法上运行它?什么不起作用?请带上堆栈跟踪。我正在tomcat上运行它,服务将作为rest端点调用。好的,您是如何在tomcat服务器中配置Spring上下文的?它不工作是因为上下文未启动,或者注入的bean为null或其他原因吗?它在更改配置后工作,谢谢!