Announcement Announcement Module
Collapse
No announcement yet.
Spring @Resource annotation with property-placeholder Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:annotation-config/>
    <context:component-scan base-package="blah"/>
    <context:property-placeholder location="classpath:application.properties"/>
    pointing to a properties file with
    HTML Code:
    application.propertyIWant=blahblah
    I have a groovy controller like this.

    Code:
    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)
    }
    
    @Controller
    @RequestMapping([ "/*.*" ])
    class FrontControllerImpl extends AbstractController implements FrontController{
    
    	@Resource(name="application.propertyIWant")
    	String propertyIWant;
    
        @RequestMapping(["/index.html"])
        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.

    Comment


    • #3
      Thanks for pointing that out.

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

      http://projects.kaare-nilsen.com/wiki/staged-spring


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

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

      Comment

      Working...
      X