Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to configure a class with Annotation based configuration in Spring 3, which takes primitive values as its constructor arguments:

@Component
class MyBean {
  MyBean(String arg1, String arg2) {
    // ...
  }
}

And an application context like this:

<beans [...]>
  <context:component-scan base-package="com.example" />
  <context:property-override location="/WEB-INF/example.properties" />
</beans>

I'm trying to find some way to specify that the constructor arguments should be taken from the properties file. Apparently this does work with constructors that take regular beans (e.g. MyClass(Bean bean1, OtherBean bean2)), but just properties?

I have also tried annotating the constructor arguments with Spring 3's @Value annotation and an EL expression for the value, like @Value("#{prop.Prop1}") arg1, but that doesn't seem to work either.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
254 views
Welcome To Ask or Share your Answers For Others

1 Answer

The following code works fine with <context:property-placeholder .../>:

@Component 
public class MyBean { 
    @Autowired
    public MyBean(@Value("${prop1}") String arg1, @Value("${prop2}") String arg2) { 
        // ... 
    } 
} 

But <context:property-override .../> is a very specific thing, it's not suitable here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...