用sbt編譯spring的問題

最近在用sbt開發springmvc應用,其實主要是爲了能用scala,由於對spring還不是很熟,資料裏都是用Java寫的,因此暫時倒尚未用scala寫。今天配置aop,大概代碼以下: java


@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的時候老是報異常: spring


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去掉!而後訪問的時候又異常! session


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。 mvc


總結就是配置的時候儘可能全面,不要依賴編譯時候會有debug信息。由於method的參數,若是沒有保留debug信息,是不知道名字的。對於class類型的話,到能夠經過Class信息判斷,可是對於primitive以及對於的wrapper類,還有String,都是不行的。 app

相關文章
相關標籤/搜索