Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Spring Autowiring

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the spring application context , I've

<context:component-scan base-package="com.howtodoinjava" />

and a bean as below


In service class

Service implementation class has

And the test class is as follows

The output is "nullnull". Is it possible to reinitialize the bean with some properties in TEST class? Is there a way to instruct the spring not to use default contructor?
 
Saloon Keeper
Posts: 23449
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've reformatted your example using our editor's Code Tags (use the Code button on the message editor). See how much prettier it is?

Autowiring has certain constraints. When you need to override those constraints, use XML settings in the spring application XML file. The XML overrides any annotated items it defines.

To construct a bean using something other than the basic no-arguments constructor, you can likewise define the bean in the spring application XML file. Some good examples of this sort of work are illiustrated in the Manning "Spring in Action" book by Walls and Briedenbach,
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you expected the BeanD object that you created in line 29 would be injected in the TestSpringServiceImpl bean. But that's not how autowiring works.

First of all, Spring doesn't know anything about the BeanD object that you created in line 29 with "new", so it can't inject this object anywhere. It's going to create a new instance of BeanD itself, using the no-args constructor, and that's what's going to be injected. The point with Spring beans is, that you should let Spring create the beans, and not create them yourself at arbitrary points in your code - Spring is not automatically going to know about any object that you create wherever in your code.

To make it work, you can do this:

1. Remove the @Component annotation from class BeanD, so that Spring isn't automatically going to create a singleton instance of BeanD.


2. Add @Configuration to class TestSpringAutowire, and add a @Bean method to it, which is the factory method to create the BeanD instance that Spring is going to use:

The @Configuration annotation tells Spring that it should look for factory methods in that class to create beans. You should annotate those factory methods with @Bean.
 
Tim Holloway
Saloon Keeper
Posts: 23449
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It bears repeating:

Jesper de Jong wrote:
First of all, Spring doesn't know anything about the BeanD object that you created in line 29 with "new", so it can't inject this object anywhere. It's going to create a new instance of BeanD itself, using the no-args constructor, and that's what's going to be injected. The point with Spring beans is, that you should let Spring create the beans, and not create them yourself at arbitrary points in your code - Spring is not automatically going to know about any object that you create wherever in your code.

 
Sanju Manc
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for not formatting the code.
Thank you so much for replies Tim and Jesper. I've modified the code to use @Configuration and @Bean annotations and it works. As Tim mentioned, I could declare the bean(BeanD) with property values for constructor, so it can be used in TestSpringAutowire class which I've not tested though.
There are two ways to populate the bean instance variables:
1. create a bean using default constructor and populate the instance variables in calling class
2. use the overridden constructor with arguments to populate the instance variables, so the calling class can invoke the bean with the overridden constructor
The later can be achieved with Spring using either @component and @bean or with XML declaration, in this case how does spring initialize bean with dynamic constructor arguments?

Thanks
 
No holds barred. And no bars holed. Except this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic