用sbt编译spring的问题

最近在用sbt开发springmvc应用,其实主要是为了能用scala,因为对spring还不是很熟,资料里都是用Java写的,所以暂时倒还没有用scala写。今天配置aop,大概代码如下:


@Around(value = "execution(* get(..)) && args(bookId, session) && @annotation(m)")
	public Object doGetBook(ProceedingJoinPoint pjp, long bookId, HttpSession session, MonitorPerformance m)
			throws Throwable {}



@RequestMapping(value = "/1", method = RequestMethod.GET)
	@ResponseBody
	@MonitorPerformance(2)
	public String get(@RequestParam(value = "bookId", required = false) long bookId, HttpSession session) {}



不过container:restart的时候总是报异常:


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'config': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.aop.aspectj.AspectJAdviceParameterNameDiscoverer$AmbiguousBindingException: Found 2 candidate variable names but only one candidate binding slot when matching primitive args
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:471)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)



以为是不能用long,于是改为Long,还有异常如下:



org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'config': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.aop.aspectj.AspectJAdviceParameterNameDiscoverer$AmbiguousBindingException: Still 2 unbound args at this(),target(),args() binding stage, with no way to determine between them
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:471)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)



但是发现,这次在eclipse里面是可以单独跑起来的,但是用sbt编译过后就是不行!后来想到是不是因为sbt编译没有保留debug信息的缘故,因为eclipse默认是保留的,于是把eclipse的去掉,果然不行!然后在sbt里面加上保留debug信息的配置:



javacOptions ++= Seq("-g")



但是其实不是在任何环境下都可以保留debug信息的,难道spring没有提供这种场景下的使用机会?!肯定不对!google之后发现,原来是可以@Around(@Before, @After等也可以)中配置argNames的。如下:
@Around(value = "execution(* get(..)) && args(bookId, session) && @annotation(m)", argNames="pjp, bookId, session, m")
	public Object doGetBook(ProceedingJoinPoint pjp, Long bookId, HttpSession session, MonitorPerformance m)
			throws Throwable {}




这样就可以把sbt里javaOptions去掉!然后访问的时候又异常!


nested exception is java.lang.IllegalStateException: No parameter name specified for argument of type [java.lang.Long], and no parameter name information found in class file either



有了刚刚的经验,很容易知道,应该还是没有debug信息的问题。spring需要指定@RequestParam,可以这样配置:



@RequestMapping(value = "/1", method = RequestMethod.GET)
	@ResponseBody
	@MonitorPerformance(2)
	public String get(@RequestParam(value = "bookId", required = false) Long bookId, HttpSession session) {}


required默认为true,不修改的话,不带参数会报错的,为了测试简单我修改为false。


总结就是配置的时候尽量全面,不要依赖编译时候会有debug信息。因为method的参数,如果没有保留debug信息,是不知道名字的。对于class类型的话,到可以通过Class信息判断,但是对于primitive以及对于的wrapper类,还有String,都是不行的。


转载于:https://my.oschina.net/magicly007/blog/266167

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值