春季自定义注释处理器(Custom annotation processor in spring)
我写了这个注释
@Retention(RetentionPolicy.SOURCE) public @interface Encrypt { }
和它的处理器......
@SupportedAnnotationTypes("it.trecube.annotation.Encrypt") @SupportedSourceVersion(SourceVersion.RELEASE_7) public class EncryptProcessor extends AbstractProcessor{ public EncryptProcessor(){ super(); } @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { String className = null; String packageName = null; String fqClassName = null; for (Element elem : roundEnv.getElementsAnnotatedWith(Encrypt.class)) { if (elem.getKind() == ElementKind.CLASS) { // Encrypt encrypt = elem.getAnnotation(Encrypt.class); // String message = "annotation found in " + elem.getSimpleName(); // processingEnv.getMessager().printMessage(Kind.NOTE, message); TypeElement classElement = (TypeElement) elem; PackageElement packageElement = (PackageElement) classElement.getEnclosingElement(); processingEnv.getMessager().printMessage( Diagnostic.Kind.NOTE, "annotated class: @Encrypt" , elem); className = classElement.getSimpleName().toString(); packageName = packageElement.getQualifiedName().toString(); fqClassName = classElement.getQualifiedName().toString(); if (fqClassName != null) { processingEnv.getMessager().printMessage( Diagnostic.Kind.NOTE, "fqClassName: "+fqClassName , elem); Properties props = new Properties(); URL url = this.getClass().getClassLoader().getResource("velocity.properties"); try { props.load(url.openStream()); } catch (IOException e) { processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, "annotated class: " + classElement.getQualifiedName()+"->\n"+ e.getMessage(), elem); e.printStackTrace(); return true; } VelocityEngine ve = new VelocityEngine(props); ve.init(); VelocityContext vc = new VelocityContext(); vc.put("className", className); vc.put("packageName", packageName); Template vt = ve.getTemplate("encrypt.vm"); File file = new File("src/main/java/"+fqClassName.replace(".", "/")+"_Encrypt.aj"); try { BufferedWriter bw = new BufferedWriter(new FileWriter(file)); processingEnv.getMessager().printMessage( Diagnostic.Kind.NOTE, "creating source file: " + file.getAbsolutePath()); processingEnv.getMessager().printMessage( Diagnostic.Kind.NOTE, "applying velocity template: " + vt.getName()); vt.merge(vc, bw); bw.close(); } catch (IOException e) { processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, "applying velocity error: " + vt.getName()+"->\n"+e.getMessage()); e.printStackTrace(); } } } } return true; // no further processing of this annotation type }
我已经测试了所有maven客户端项目和所有工作,但当我尝试将它用于现有的春天(完整的maven构建)项目不工作...有人可以帮助我? TNX
i've written thi annotation
@Retention(RetentionPolicy.SOURCE) public @interface Encrypt { }
and its processor...
@SupportedAnnotationTypes("it.trecube.annotation.Encrypt") @SupportedSourceVersion(SourceVersion.RELEASE_7) public class EncryptProcessor extends AbstractProcessor{ public EncryptProcessor(){ super(); } @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { String className = null; String packageName = null; String fqClassName = null; for (Element elem : roundEnv.getElementsAnnotatedWith(Encrypt.class)) { if (elem.getKind() == ElementKind.CLASS) { // Encrypt encrypt = elem.getAnnotation(Encrypt.class); // String message = "annotation found in " + elem.getSimpleName(); // processingEnv.getMessager().printMessage(Kind.NOTE, message); TypeElement classElement = (TypeElement) elem; PackageElement packageElement = (PackageElement) classElement.getEnclosingElement(); processingEnv.getMessager().printMessage( Diagnostic.Kind.NOTE, "annotated class: @Encrypt" , elem); className = classElement.getSimpleName().toString(); packageName = packageElement.getQualifiedName().toString(); fqClassName = classElement.getQualifiedName().toString(); if (fqClassName != null) { processingEnv.getMessager().printMessage( Diagnostic.Kind.NOTE, "fqClassName: "+fqClassName , elem); Properties props = new Properties(); URL url = this.getClass().getClassLoader().getResource("velocity.properties"); try { props.load(url.openStream()); } catch (IOException e) { processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, "annotated class: " + classElement.getQualifiedName()+"->\n"+ e.getMessage(), elem); e.printStackTrace(); return true; } VelocityEngine ve = new VelocityEngine(props); ve.init(); VelocityContext vc = new VelocityContext(); vc.put("className", className); vc.put("packageName", packageName); Template vt = ve.getTemplate("encrypt.vm"); File file = new File("src/main/java/"+fqClassName.replace(".", "/")+"_Encrypt.aj"); try { BufferedWriter bw = new BufferedWriter(new FileWriter(file)); processingEnv.getMessager().printMessage( Diagnostic.Kind.NOTE, "creating source file: " + file.getAbsolutePath()); processingEnv.getMessager().printMessage( Diagnostic.Kind.NOTE, "applying velocity template: " + vt.getName()); vt.merge(vc, bw); bw.close(); } catch (IOException e) { processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, "applying velocity error: " + vt.getName()+"->\n"+e.getMessage()); e.printStackTrace(); } } } } return true; // no further processing of this annotation type }
I've tested all into a maven client project and all work, but when i try to use it into an existing spring (full maven build) project not work... Can someone help me?? Tnx
原文:https://stackoverflow.com/questions/15706812
更新时间:2022-06-18 09:06
最满意答案
我会采用以下方法:
www.example.com/update/client_version
您的代码应如下所示:
import webapp2 class UpdateHandler(webapp2.RequestHandler): def get(self, version): # Do something for version app = webapp2.WSGIApplication( [(r'/update/(\d+)', UpdateHandler)], debug=True)
I would go with the following approach:
www.example.com/update/client_version
Your code should look like this:
import webapp2 class UpdateHandler(webapp2.RequestHandler): def get(self, version): # Do something for version app = webapp2.WSGIApplication( [(r'/update/(\d+)', UpdateHandler)], debug=True)
相关问答
更多-
不,没有。 Google App Engine (GAE)对于Python应用程序使用沙盒式Python 2.7运行时。 这是普通的App Engine托管 。 但是,在GAE您可以使用托管VM主机 。 托管VM托管可让您在可配置的Google Compute Engine虚拟机上运行GAE应用程序。 给你更多的灵活性。 在Alpha阶段, 受管理的虚拟机只支持Java 7 , Python 2.7和Go 1.4运行时环境。 要获得其他运行时(如Python 3或node.js ),您可以创建用户可配置的自 ...
-
我如何开始在python中编写简单的Json Rest启用服务器应用程序(how do I start writing simple Json Rest enable server app in python)[2023-07-27]
我将这个项目用于GAE休息应用程序: http://code.google.com/p/appengine-rest-server/ 文档很好,并且有一些例子可以了解它的工作原理。 非常容易使用。 I used this project for a GAE rest application : http://code.google.com/p/appengine-rest-server/ The documentation is good and there are some examples to und ... -
Google Apps Engine上的Python Flask REST身份验证(Python Flask REST authentication on Google Apps Engine)[2022-10-22]
您可以包含其他纯python第三方软件包,您可以通过设置vendoring来实现。 供应允许您将包安装到项目的子目录中,并将它们包含在您的代码中。 - 供应第三方包 cd在项目的根目录中创建或修改appengine_config.py以包含以下内容: from google.appengine.ext import vendor vendor.add('lib') pip install -r requirements.txt -t ... -
通常python文件以py结尾。 处理程序应该说 script: template-123.py 首先在本地测试您的cron作业。 您应该能够访问localhost:8000 / _ah / admin,然后单击左侧的Cron Jobs链接。 它应该列出您的所有cron作业,并且您应该能够使用页面上的链接对它们进行测试。 首先在dev_appserver上进行调试。 Usually python files end in py. the handler should say script: templat ...
-
Google App Engine模块间通信授权(python)(Google App Engine inter module communication authorization (python))[2022-06-26]
正如这里所写,这是谷歌appengine公共问题跟踪器上的跟踪问题。 所以每个人都可以去那里检查更新。 同时我解决了从app.yaml删除login: admin的问题,在我的服务处理程序中,我已经手动检查了标头X-Appengine-Inbound-Appid及其值的存在。 As written here this is a tracked issue now on google appengine public issue tracker. So everyone can go there to che ... -
我会采用以下方法: www.example.com/update/client_version 您的代码应如下所示: import webapp2 class UpdateHandler(webapp2.RequestHandler): def get(self, version): # Do something for version app = webapp2.WSGIApplication( [(r'/update/(\d+)', UpdateHandler)], ...
-
您可以使用香草DjangGAE来完成您提到的所有事情。 一个很好的起点 值得一提的是,有一些值得注意的局限性。 这些也列在我链接的网站上。 You can use vanilla DjangGAE to do everything you'd mentioned. A good starting point It's worth mentioning that there are a few notable limitations. Those are also listed in the site I'd ...
-
混乱的奇怪事情发生在这里。 首先, app.yaml我必须在root设置之前放置我的/cron处理程序: handlers: - url: /cron script: assets/backup/main.py - url: / static_files: assets/index.html upload: assets/index.html 否则我会因为无法找到该文件而感到疯狂。 这一点实际上是有道理的。 接下来是Python代码。 不确定这里发生了什么,但最终我设法通过这样做: #!/u ...
-
根据文档, Alloc字段显示已分配且仍在使用的字节。 但是,在垃圾收集语言中,当GC释放内存时,它不会立即返回系统(可以很快再次请求,所以为什么还要将它返回?)。 所以你真正需要监视的是Sys字段,它计算从系统获得的字节数。 您可能对本文感兴趣,并提供了一些如何最大限度地减少内存使用量的见解。 According to the docs Alloc field shows bytes allocated and still in use. In garbage-collected language, ho ...
-
在main.py文件(即main模块)中, application是main()函数内的变量,而不是main模块的属性。 基本上你不需要main()函数。 GAE对使用Django有一些特定的支持,我强烈建议浏览Django支持文档和Django App示例 。 In your main.py file (i.e. the main module) application is a variable inside the main() function, not an attribute of the ma ...