我已经看到了这个问题,我无法让 Groovy 在我的 Spring MVC 项目中工作。我应该把它放在哪里,完整的路径应该是什么?请原谅我的基本问题,但我必须开始在 Spring MVC 中使用 Groovy。Groovy 在 Spring 4.0 Snapshot 中可用,根据官方网站,最终版本应该在 12 月上线。但是现在,是否建议使用 4.0 版本?(它内置了对 Groovy 的支持)
参考代码:
<beans .........
...
...
<bean class="full.qualified.name.of.ProxyAwareAnnotationMethodHandlerAdapter" />
...
...
<lang:groovy script-source="classpath:com/example/mysample.groovy refresh-check-delay="1000" />
</beans>
Java类如下:
//ProxyAwareAnnotationMethodHandlerAdapter.java
package name.assafberg.spring;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.Advised;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
/**
* Add proxy awareness to <code>AnnotationMethodHandlerAdapter</code>.
*
* @author assaf
*/
public class ProxyAwareAnnotationMethodHandlerAdapter extends AnnotationMethodHandlerAdapter {
/**
* @param request
* @param response
* @param handler
* @return
* @throws Exception
* @see org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
*/
@Override
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
handler = unwrapHandler(handler);
return super.handle(request, response, handler);
}
/**
* @param handler
* @return
* @see org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#supports(java.lang.Object)
*/
@Override
public boolean supports(Object handler) {
handler = unwrapHandler(handler);
return super.supports(handler);
}
/**
* Attempt to unwrap the given handler in case it is an AOP proxy
*
* @param handler
* @return Object
*/
private Object unwrapHandler(Object handler) {
if (handler instanceof Advised) {
try {
TargetSource targetSource = ((Advised) handler).getTargetSource();
return targetSource.getTarget();
} catch (Exception x) {
throw new RuntimeException(x);
}
} else {
return handler;
}
}
}
编辑
无需处理上述文件。我唯一要做的就是处理 Groovy 的版本。从命令行确保 Groovy 的版本与类路径添加的版本相同。它奏效了。几乎不需要任何配置。刚刚创建了一个 .groovy 类而不是 Java 类(感谢 +kunal 的启发。)
@Controller
class TestController {
@Autowired
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
String People() {
return "people"
}
}