wood burning stoves 2.0*
The moose likes Spring and the fly likes getting same result in AutoWiring byType and byName Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "getting same result in AutoWiring byType and byName " Watch "getting same result in AutoWiring byType and byName " New topic

getting same result in AutoWiring byType and byName

prasad chowdary

Joined: Feb 18, 2012
Posts: 18
hi experts,

In my application im getting same results in autowiring(byname and byType) .please see my code.


package info.spring.test;

public class Address {
private String city;
public String getCity() {
return city;
public void setCity(String city) {
this.city = city;
public String getState() {
return state;
public void setState(String state) {
this.state = state;
private String state;


package info.spring.test;

public class Student {

private String name;
private Address address1;
private Address address2;
private int roll_No;

public int getRoll_No() {
return roll_No;

public void setRoll_No(int rollNo) {
roll_No = rollNo;

public String getName() {
return name;

public void setName(String name) {
this.name = name;

public Address getAddress1() {
return address1;

public void setAddress1(Address address1) {
this.address1 = address1;

public Address getAddress2() {
return address2;

public void setAddress2(Address address2) {
this.address2 = address2;

public void details(){

System.out.println("student roll_no:"+roll_No);
System.out.println("Student name:"+name);
System.out.println("Temparary address:"+address1.getCity()+'\t'+address1.getState());
System.out.println("paramenent Address:"+address2.getCity()+'\t'+address2.getState());



<bean id="std" class="info.spring.test.Student" autowire="byType">
<property name="name" value="sachin"></property>
<property name="roll_No" value="99"></property>


<bean id="address1" class="info.spring.test.Address" >
<property name="city" value="hyd"></property>
<property name="state" value="ap"></property>

<bean id="address2" class="info.spring.test.Address">
<property name="city" value="banglr"></property>
<property name="state" value="karnataka"></property>


getting both address objects

Thanks in Advance.

Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
Can you post the complete code. You should have got an exception (see below from spring documentation) as there are more than one Address type bean defined.

byType Allows a property to be autowired if there is exactly one bean of the property type in the container. If there is more than one, a fatal exception is thrown, and this indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set. If this is not desirable, setting the dependency-check="objects" attribute value specifies that an error should be thrown in this case.
prasad chowdary

Joined: Feb 18, 2012
Posts: 18
thanks piyush,

iam not getting any exception.
this is my main program go through it.


package info.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyApp {

public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student)context.getBean("std");


Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
I tried running your code and getting the following expected exception.

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'std' defined in class path resource [config.xml]: Unsatisfied dependency expressed through bean property 'address1': : No unique bean of type [info.spring.test.Address] is defined: expected single matching bean but found 2: [address1, address2]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [info.spring.test.Address] is defined: expected single matching bean but found 2: [address1, address2]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1167)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1059)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at info.spring.test.MyApp.main(MyApp.java:10)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [info.spring.test.Address] is defined: expected single matching bean but found 2: [address1, address2]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:796)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1152)
... 13 more

Where is your applicationContext.xml located?
subject: getting same result in AutoWiring byType and byName
Similar Threads
Foreign key must have same number of columns
Generating user details in form as ReadOnly
got an exception in configuration file
Annotations exception
Websphere Studio Error : MQJMS1061: Unable to deserialize object