3
votes

This is my Spring main class

public class AppConfigSpring {

private static Log logger = LogFactory.getLog(AppConfigSpring.class);

public static void main(String[] args) {
    logger.info("Log info message");
    logger.debug("Debug log");
    AbstractApplicationContext appContext = new ClassPathXmlApplicationContext("spring.xml");
    appContext.registerShutdownHook();

    Person beanSecurityPersonal = (Person) appContext.getBean("securityPersonal");

    beanSecurityPersonal.printingName();
    beanSecurityPersonal.officeAddress();
}}

Below is my spring bean class

public class SecurityPersonal implements Person {

private String firstName;
private String lastName;

@Autowired
@Qualifier("securityPersonal")
private Address officeAddressSecurity;

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public Address getOfficeAddressSecurity() {
    return officeAddressSecurity;
}

public void setOfficeAddressSecurity(Address officeAddressSecurity) {
    this.officeAddressSecurity = officeAddressSecurity;
}

@Override
public void printingName() {
    StringBuilder secName = new StringBuilder();
    secName.append("Security Personal Name : ").append(getFirstName()).append(" ").append(getLastName());
    System.out.println(secName.toString());
}

@Override
public void officeAddress() {
    StringBuilder secAddress = new StringBuilder();

    secAddress.append("Office Address : ").append(" State - ").append(getOfficeAddressSecurity().getState())
            .append(" City - ").append(getOfficeAddressSecurity().getCity()).append(" Pincode - ")
            .append(getOfficeAddressSecurity().getPincode()).append("\n");

    System.out.println(secAddress.toString());
}

@Override
public void shippingAddress() {

}

@Override
public void itemsListDetails() {

}}

And Below is my exception

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityPersonal': Unsatisfied dependency expressed through field 'officeAddressSecurity'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.sunil.javabrains.Address' available: expected single matching bean but found 2: officeAddress,shippingAddress Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityPersonal': Unsatisfied dependency expressed through field 'officeAddressSecurity'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.sunil.javabrains.Address' available: expected single matching bean but found 2: officeAddress,shippingAddress at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:144) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:85) at org.sunil.javabrains.AppConfigSpring.main(AppConfigSpring.java:20) Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.sunil.javabrains.Address' available: expected single matching bean but found 2: officeAddress,shippingAddress at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:220) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1265) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ... 15 more

Spring xml Config

<beans default-init-method="initMethod"
    default-destroy-method="destroyMethod">

            <bean id="officeAddress" class="org.sunil.javabrains.Address">
        <qualifier type="org.springframework.beans.factory.annotation.Qualifier" value="securityPersonal"></qualifier>
        <property name="state" value="Maharashtra"></property>
        <property name="city" value="Mumbai"></property>
        <property name="pincode" value="400083"></property>
    </bean>

    <bean id="shippingAddress" class="org.sunil.javabrains.Address">
        <property name="state" value="Maharashtra"></property>
        <property name="city" value="Pune"></property>
        <property name="pincode" value="411045"></property>
    </bean>

        <bean id="securityPersonal" class="org.sunil.javabrains.SecurityPersonal">
    </bean>

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>

    </beans>
2
either use XML mapping for the Bean or use annotation for the bean. Don't use both. I will suggest remove either one and then try.dassum

2 Answers

0
votes

Try to use following spring config (Spring - Annotation Based Configuration):

<context:annotation-config/>

And use the bean id as qualifier!

@Qualifier("officeAddress")

spring.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />

    <bean id="officeAddress" class="org.sunil.javabrains.Address">
        <property name="state" value="Maharashtra"></property>
        <property name="city" value="Mumbai"></property>
        <property name="pincode" value="400083"></property>
    </bean>

    <bean id="shippingAddress" class="org.sunil.javabrains.Address">
        <property name="state" value="Maharashtra"></property>
        <property name="city" value="Pune"></property>
        <property name="pincode" value="411045"></property>
    </bean>

    <bean id="securityPersonal" class="org.sunil.javabrains.SecurityPersonal" />

</beans>

SecurityPersonal.java

public class SecurityPersonal implements Person {

    private String firstName;
    private String lastName;

    @Autowired
    @Qualifier("officeAddress")
    private Address officeAddressSecurity;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Address getOfficeAddressSecurity() {
        return officeAddressSecurity;
    }

    public void setOfficeAddressSecurity(Address officeAddressSecurity) {
        this.officeAddressSecurity = officeAddressSecurity;
    }

    @Override
    public void printingName() {
        StringBuilder secName = new StringBuilder();
        secName.append("Security Personal Name : ").append(getFirstName()).append(" ").append(getLastName());
        System.out.println(secName.toString());
    }

    @Override
    public void officeAddress() {
        StringBuilder secAddress = new StringBuilder();

        secAddress.append("Office Address : ").append(" State - ").append(getOfficeAddressSecurity().getState())
                .append(" City - ").append(getOfficeAddressSecurity().getCity()).append(" Pincode - ")
                .append(getOfficeAddressSecurity().getPincode()).append("\n");

        System.out.println(secAddress.toString());
    }

    @Override
    public void shippingAddress() {

    }

    @Override
    public void itemsListDetails() {

    }
}

output

Security Personal Name : null null
Office Address :  State - Maharashtra City - Mumbai Pincode - 400083

EDIT

The problem is that your qualifier name is equal to the ID of the bean (id=securityPersonal class=org.sunil.javabrains.SecurityPersonal)! So you can't use this ID as qualifier. You can use another ID to define the address with the qualifier. e.g.

SecurityPersonal.java

@Autowired
@Qualifier("addressForSecurityPersonal")
private Address officeAddressSecurity;

spring.xml

<bean id="officeAddress" class="org.sunil.javabrains.Address">
    <qualifier type="org.springframework.beans.factory.annotation.Qualifier" value="addressForSecurityPersonal" />
    <property name="state" value="Maharashtra"></property>
    <property name="city" value="Mumbai"></property>
    <property name="pincode" value="400083"></property>
</bean>
0
votes

From the exception, it is clear that the below annotation used is incorrect

@Qualifier("securityPersonal")
private Address officeAddressSecurity;

Either use

@Qualifier("shippingAddress") 
OR 
@Qualifier("officeAddress") 

instead.