当前位置: 首页 > 知识库问答 >
问题:

Spring Bean@autowired和@qauilifier name在我的情况下不起作用。有谁能给我一个确切的工作例子吗

屠锐
2023-03-14
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();
}}
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() {

}}

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>

共有1个答案

阚乐湛
2023-03-14

尝试使用以下spring config(基于Spring-Annotation的配置):

<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>
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>
 类似资料:
  • 问题内容: 我正在使用nodejs和expressjs框架从服务器下载文件’jsonFile.json’。 我正在使用以下代码 但这会导致对整个文件内容的响应。 我期望浏览器要我将文件保存在本地磁盘中。 如何将文件保存在本地磁盘中? 问题答案: 让Express设置正确的标头,然后执行以下操作: (doc) 编辑: 由于您是通过AJAX调用进行请求的,因此您必须更改设置,因为大多数(全部?)浏览器

  • 我有HTML+CSS页面,其中有两个按钮: 我想让“顶级评论”默认可见。所以在本页的最后,我将简单的JavaScript 自动点击“顶部评论”按钮。 在我的开发环境中,一切都运行良好。但当将代码上传到启用CloudFlare的生产服务器时。JavaScript click()函数不起作用。 我已经为这个问题给CloudFlare的支持写了信,但两天后仍然没有任何回应。 对于我如何调试为什么这个Ja

  • 在我的项目中,我用Spring Batch 2.2编写了一个Quartz调度器。 根据我的要求,我想运行一个调度程序来获取应用程序配置属性,以刷新所有GlassFish集群上的配置缓存。 所以我不需要ItemWriter和ItemReader,它们用于文件读/写操作。那么我可以从中删除ItemReader和ItemWriter吗? 我的工作配置如下所述: 我正在编写业务逻辑以刷新JobClass

  • 我在创建一个不需要Robolectric的单元测试时遇到了麻烦。我在代码中使用AndroidThreeten.init(this),当我运行测试时,如果禁用robolectric,我会得到一个错误: 还是因为这个我必须用robolectric?(附注:Log不是android的util.Log,而是我自己的类)(编辑过)

  • /Output: 课后,在主装载区内,糖果不能放口香糖。forName(“口香糖”)在创建Cooki后加载Cooki 这是我的代码和结果,找不到口香糖。 书中给出了一个java的Rtti示例,结果如下: 这似乎是一门课。forName()不起作用,无法初始化。

  • 我正在建立一个应用程序,并使用MariaDB作为我的数据库。我有一张桌子“kick_votes”。其主键包含三个字段: null