0

How will spring.xml treat a bean that is declared in it without any initializing values. ie, no properties are set for the beans in spring.xml for that specific bean id.

1) Will the spring container be able to create the bean?

2) If I have 2 beans(id= beanA and beanB) declared in spring.xml without any initialising values for either bean, would it be possible to inject say beanB into beanA. What is the equivalent spring.xml or annotations for the same.

ram kumar
  • 13
  • 7
  • Spring does involves a lot of magic, but if you do not set properties, they simply remain the same the class constructor initializes them (not initialized further). And how do you inject beanB into beanA without specifying any property in beanA? – Serge Ballesta Sep 19 '16 at 15:18

2 Answers2

0
  1. As long as the default constructor is available (or a one that is defined without any params), creating the bean is possible. (see Java default constructor)

  2. Sure, you can inject them programmatically in your code. Define beans like you usually do in xml. Get them from the context, and set them. But why would you want to do that when you can do it in you XML config. Or you could do this in Java DSL config.

Community
  • 1
  • 1
code4kix
  • 3,937
  • 5
  • 29
  • 44
0

1) Will the spring container be able to create the bean?

If you have a default constructor for that bean spring will use it to create that bean.

2) If I have 2 beans(id= beanA and beanB) declared in spring.xml without any initialising values for either bean, would it be possible to inject say beanB into beanA. What is the equivalent spring.xml or annotations for the same.

With XML you can inject beanB in beanA using <property name=xyz ref=beanBID> assuming xyz is field name in beanA that refers to beanB and beanBId refer to beanB in XML. Using annotation it is ‘@Resource/@Autowired/@Inject/etc ``