Announcement Announcement Module
No announcement yet.
Spring @Resource annotation with property-placeholder Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring @Resource annotation with property-placeholder

    Been searching around for clues how to do this, without writing custom implementations.

    I have application xml like this:

    HTML Code:
    <context:component-scan base-package="blah"/>
    <context:property-placeholder location=""/>
    pointing to a properties file with
    HTML Code:
    I have a groovy controller like this.

    package blah
    import javax.annotation.Resource;
    import org.springframework.stereotype.*
    import org.springframework.beans.factory.annotation.*
    import org.springframework.ui.ModelMap
    import org.springframework.web.bind.annotation.RequestMapping
    import org.springframework.web.bind.annotation.RequestParam
    interface FrontController {
    	def landingPage(ModelMap model)
    @RequestMapping([ "/*.*" ])
    class FrontControllerImpl extends AbstractController implements FrontController{
    	String propertyIWant;
        def landingPage(ModelMap model){
    	return "index"

    But it wont work , as it wont be able to resolve the Resource of type or name application.propertyIWant

    Any ideas how to inject Resource configurations from property placeholders?

    Without write a bean xml for the class?

  • #2
    You are not allowed to do so. The first reason is that string properties can't be autowired - 3.3.5. Autowiring collaborators:
    Please also note that it is not currently possible to autowire so-called simple properties such as primitives, Strings, and Classes (and arrays of such simple properties). (This is by-design and should be considered a feature.)
    The second reason is that PreferencesPlaceholderConfigurer doesn't introduce new beans to the bean factory, it just modifies registered bean definition properties. So, even if it was possible to autowire string properties, there wouldn't be separate string object with the target resource id.


    • #3
      Thanks for pointing that out.

      So it looks like I could look at something like what is explained here:

      Using Environment Aware Properties bean and annotation like:
      @Configuration(key="aKey") private String aPropWithoutDefault;

      However I may rethink my component architecture, and inject the properties in xml in a more generic way.