gpt4 book ai didi

java - Spring Bean @Autowired 和@Qauilifier 名称在我的情况下不起作用。任何人都可以帮助我提供确切的工作示例

转载 作者:行者123 更新时间:2023-12-02 04:21:26 27 4
gpt4 key购买 nike

这是我的Spring主类

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();
}}

下面是我的spring bean类

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() {

}}

下面是我的异常(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配置

<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>

最佳答案

尝试使用以下 spring 配置(Spring - Annotation Based Configuration):

<context:annotation-config/>

并使用 bean id 作为限定符!

@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() {

}
}

输出

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

编辑

问题是您的限定符名称等于 bean 的 ID(id=securityPersonal class=org.sunil.javabrains.SecurityPersonal)!所以你不能使用这个 ID 作为限定符。您可以使用另一个 ID 来定义带有限定符的地址。例如

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>

关于java - Spring Bean @Autowired 和@Qauilifier 名称在我的情况下不起作用。任何人都可以帮助我提供确切的工作示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59554986/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com