3

OK, so if I need to put some primitive values in the constructor, how do I do that?

    @Autowired
public CustomBean(String name, @Qualifier("SuperBean") SuperBean superBean) {
    super();
    this.superBean = superBean;
    this.name = name;
}

For instance here I am defining that the superBean has the Qualifier "SuperBean", but I'd also like to know how is it possible to use annotations to set the name value here?

I know it's possible with xml configuration, but I want to know how to do this with annotations too:

<bean id="CustomXmlBean" class="org.arturas.summerfav.beans.CustomXmlBean">
        <constructor-arg name="name" type="String" value="The Big Custom XML Bean" />
        <constructor-arg>
            <bean id="SuperBean" class="org.arturas.summerfav.beans.SuperBean" />
        </constructor-arg>
    </bean>

Well how do I put in values for String, int and other generic types?

Arturas M
  • 4,120
  • 18
  • 50
  • 80

1 Answers1

7

Here is one way to do this:

@Component 
public class YourBean { 
    @Autowired
    public YourBean(@Value("${prop1}") String arg1, @Value("${prop2}") String arg2) { 
        // rest of the code
    } 
} 
Santosh
  • 17,667
  • 4
  • 54
  • 79
  • Thank you very much, I searched in google and seeked in the whole chapter 3 IoC of Spring, but didn't manage to find that @Value, I wonder why they haven't included it there, that's just thumbs down for Spring docs... I used this with @Value("some text"), can you explain me in more detail what "${prop1}" is? is it some value or variable defined somewhere, can you tell me how to use it too? – Arturas M Aug 08 '13 at 04:19
  • 1
    @ArturasM, in `${prop1}`, `prop1` is the key defined in a property file ([loaded via standard spring mechanism](http://bharatonjava.wordpress.com/2013/01/25/configuring-and-using-properties-file-in-spring-3/)). – Santosh Aug 08 '13 at 05:19
  • Thanks, I think I got it now. – Arturas M Aug 08 '13 at 06:03