• 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
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Three classes extends an interface then how we can autowire one of these class in another class?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

In a recent interview I was being asked a question which confused me ;

Three classes extends an interface then how we can autowire one of these class in another class?

Anybody can hep me on this.

Regards
Jang
 
Sheriff
Posts: 5557
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What was your answer to the question? What was your reasoning?
 
Vishal Bhadur Verma
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:What was your answer to the question? What was your reasoning?



I answered him I can autowire this normally as I do usually. No special case in this. For example:

public interface Interface {

public void print();

}

------------------------------

public class Country implements Interface{

public String country_Name;
Capital captial;



public String getCountry_Name() {
return country_Name;
}
public void setCountry_Name(String country_Name) {
this.country_Name = country_Name;
}
public Capital getCaptial() {
return captial;
}
public void setCaptial(Capital captial) {
this.captial = captial;
}
@Override
public void print() {
System.out.println("Msg in Country Class");

}



}

-------------------------------------------------------

ublic class Capital implements Interface {

public String capital_Name;

public String getCapital_Name() {
return capital_Name;
}

public void setCapital_Name(String capital_Name) {
this.capital_Name = capital_Name;
}

@Override
public void print() {
System.out.println("Msg print in Capital Class");

}



}

-----------------------------

Application Context XML

<bean id="country" class="com.test.autowiring.Country" autowire="byName">
<property name="country_Name" value="India"></property>
</bean>

<bean id="captial" class="com.test.autowiring.Capital">
<property name="capital_Name" value="Delhi"></property>
</bean>


-----------------------------------------

Main Class :


public class TestAutowireClass {

/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Country con = (Country) applicationContext.getBean("country");
System.out.println("The captial of :"+con.getCountry_Name()+" is :"+con.getCaptial().getCapital_Name());
con.print();

}

}


--------------------------------

Output is ::

The captial of :India is elhi
Msg in Country Class


But he said I should read more on this. I am not sure that I was right or not
 
Tim Cooke
Sheriff
Posts: 5557
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what your interviewer was getting at is more like this scenario:

When you want to populate the dependency in another class.

How do you use Spring's Autowire feature to specify what implementation of Country gets injected?
 
Vishal Bhadur Verma
Greenhorn
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:I think what your interviewer was getting at is more like this scenario:

When you want to populate the dependency in another class.

How do you use Spring's Autowire feature to specify what implementation of Country gets injected?



ooh Got it.. I can use @Qualifier tag in this case
But all three beans will declared individually in application context with different bean id .. Right ?
 
Tim Cooke
Sheriff
Posts: 5557
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct.
 
We can walk to school together. And we can both read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic