Results 1 to 3 of 3

Thread: Spring @Resource annotation with property-placeholder

  1. #1
    Join Date
    Feb 2006
    Location
    Hampshire
    Posts
    21

    Question 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. #2
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    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. #3
    Join Date
    Feb 2006
    Location
    Hampshire
    Posts
    21

    Red face

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •