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>