12

Is it possible to do something like this :

<property name="template" value="file:/some/resource/path/myTemplate.txt" />

via annotations without creating custom annotation and populate the field via reflection ? It cannot be done with @Value, cause it works only with Strings...

If not, what is the best way to process Spring bean custom annotations and init the bean accordingly ?

lisak
  • 21,611
  • 40
  • 152
  • 243

3 Answers3

28

Use a Resource as the field.

@Value("file:/some/resource/path/myTemplate.txt")
Resource template;

You can use classpath: style URIs this way too.

Then if you need to get a File, use template.getFile()

Michael-O
  • 18,123
  • 6
  • 55
  • 121
sourcedelica
  • 23,940
  • 7
  • 66
  • 74
  • Cool, btw, do you know how to extend/use AutowiredAnnotationBeanPostProcessor to inject fields/methods or process them in some way, with custom annotations ? – lisak Jun 17 '11 at 23:45
  • Can you give me an example of what you want to do? – sourcedelica Jun 18 '11 at 01:20
  • You maybe use testng - Let say that you have a method abc(a,b,c) that needs be called with some values... I annotate it with @DataProvided annotation and create corresponding DataProvider method ... And I'd want AutowiredAnnotationBeanPostProcessor to grab the annotation, execute dataprovider method and initialize that abc(a,b,c); method .... It's a nonsense scenario, that can be done by spring normally ... I can't think of anything real now :-) The important point is, get the annotation and do something, whatever you want... – lisak Jun 18 '11 at 01:30
  • Check out this question: http://stackoverflow.com/questions/5337128/spring-framework-annotation-question – sourcedelica Jun 18 '11 at 02:30
  • You would need to create your own custom `BeanPostProcessor` and have it work like the other `*AnnotationBeanPostProcessor`s. – sourcedelica Jun 18 '11 at 02:46
  • Also see this http://stackoverflow.com/questions/259140/scanning-java-annotations-at-runtime – sourcedelica Jun 18 '11 at 03:02
  • 1
    What is the "templatePath" ? Btw in webapp, If I go like this @Value("${classpath:WEB-INF/classes/exc-msg.properties}") it is not searching on the classpath but from the root of web application context...do you know how to work around it ? – lisak Jul 21 '11 at 01:22
0

If I understand the question, you are trying to set the value of a field which is of type File? Could you just inject the file name with the @Value and then create the File object in code when needed?

digitaljoel
  • 26,265
  • 15
  • 89
  • 115
  • Not good, File constructor expects absolute or JVM execution path, you usually getting resources from classpath....I'd have to go like "new File(this.class.getClassLoader.getResource("file").toURI() ); " ... it's just ugly thing to do ... Spring has nice support like ClassPathResource(String path, ClassLoader classLoader) that could handle it behind the scene – lisak Jun 17 '11 at 22:34
-1

@Value("${file:/some/resource/path/myTemplate.txt}") Resource template;

Thanks for the wake up, the highly upvoted answer had "templatePath" without any references and now stands corrected - see comments.

The Javadoc or official docs for @Value resource usages was hard to find early 2011 and still is, to find how resources can be injected via @Value spel using classpath, file, url: SPEL expressions, File system resources, @Value, @PropertyResource

kisna
  • 2,869
  • 1
  • 25
  • 30