0

after watching various tutorials and doing some reading I understand how to use constructor arguments in spring. no issue there. my issue is can someone explain or point me to some material where it explains WHY they are used. what I mean by that, surely with syntax like this: <constructor-arg value="threading"></constructor-arg>, a hardcoded value is poor practice? i dont want an any articles on spring concerning how to do it. i just need a simple answer to explain why hardcoding is seemingly okay here OR better solutions. I am aware of the @Resource annotation which gets rid of the need for lines such as <property name="answer" ref="answer"></property> but I have always been taught hardcoding is bad idea and pulling from a DB for example is a better solution

<bean id="questions" class="main.Questions">
        <constructor-arg value="0000"></constructor-arg>
        <constructor-arg value="What is the question?"></constructor-arg>
        <constructor-arg value="threading"></constructor-arg>
        <property name="correctAnswer" value="deadlock"></property>

</bean>
  • 1
    Maybe the `Beans.xml` file **is** the DB? Or you're supposed to think of it that way? I'm not sure (I'm a beginner with Spring). – ajb Nov 13 '14 at 15:54

1 Answers1

0

This is not hard coding... It took me a bit to figure out what you meant, but check out this wiki article:

http://en.wikipedia.org/wiki/Hard_coding

You are not hard coding by providing constructor/property injection values as literals in an XML, because the XML serves as an externalized data source. Practically this is no different from a value in a field in a DB someplace. If you modify it in the XML, you dont need to recompile your code to make it work.

Mark W
  • 2,791
  • 1
  • 21
  • 44
  • let me be more explicit. look at my question now and see the bean i have put in there. the constructor args have values. now if i wanted to replace them with the @Resource annotation, where would I then put the values?? –  Nov 13 '14 at 16:23
  • I dont understand how that relates to your OP. Using different types of dependency injection is irrelevant to XML being an externalized data source, and therefore not hard coding. The point with the XML constructor args is that you can change the value in the XML, and reload the bean (either a restart, or via some other method) and you dont need to recompile your code. Changing this to use the @Resource annotations is a code change... sort of, apples and oranges. – Mark W Nov 13 '14 at 16:34
  • @RedBaron Use a SpEL expression for the value. – chrylis -cautiouslyoptimistic- Nov 13 '14 at 16:34
  • @MarkW i am at the stage at the moment where I am still new to Spring and working out its concepts and ideas. I know atResource is not part of Spring but I have figured out how constructor-args work and so now want to see how to get rid of constructor-args and still maintain the values which I believe is possible. and yes sorry for going on a tangent from the original question. also i have used atResource as stackoverflow will not let me use another at symbol –  Nov 13 '14 at 16:52