首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring IDREF用法

Spring IDREF用法
EN

Stack Overflow用户
提问于 2013-01-30 23:07:35
回答 5查看 22.8K关注 0票数 14

我有一个定义如下的spring.xml:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="triangle" class="org.tutorial.spring.Triangle">
    <property name="pointA">
        <idref bean="pointA"/>
    </property>
    <property name="pointB" ref="pointB"/>
    <property name="pointC" ref="pointC"/>
</bean>
<bean id="pointA" class="org.tutorial.spring.Point">
    <property name="x" value="0"/>
    <property name="y" value="0"/>
</bean>
<bean id="pointB" class="org.tutorial.spring.Point">
    <property name="x" value="100"/>
    <property name="y" value="200"/>
</bean>
<bean id="pointC" class="org.tutorial.spring.Point">
    <property name="x" value="-100"/>
    <property name="y" value="-200"/>
</bean>
</beans>

Point类基本上是一个具有2个私有int成员的类。我的问题是我在IDREF上得到的错误如下:

代码语言:javascript
运行
复制
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.tutorial.spring.Point' for property 'pointA'; 
nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.tutorial.spring.Point] for property 'pointA': no matching editors or conversion strategy found

据我所知,IDREF (在上面的例子中)的目的是为了bean三角形而存在bean PointA (错误检查)。因此,我在IDREF元素中提供了bean PointA(string)的名称。为什么会出现上面的错误?

当我认为它只是通过提供名称来检查bean (PointA)的存在时,为什么它要尝试将字符串转换为Point?

我真的很困惑。请帮帮忙。谢谢。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-01-31 00:18:25

idref用于传递bean (即字符串)的名称(标识符)。

<idref bean="pointA">与字符串值pointA完全相同,不同之处在于如果没有定义这样的bean,Spring会报错。

详情请参见the Spring documentation

要传递实际的bean,只需使用ref,就像对pointBpointC所做的那样。

票数 12
EN

Stack Overflow用户

发布于 2013-08-31 02:17:29

idref元素只是将容器中另一个bean的id (字符串值-不是引用)传递给或元素的一种防错方法。

简单地说,idref元素用于传递字符串值,使用idref标记允许容器在部署时验证引用的命名bean是否实际存在。

考虑下面的例子

注意控制台中的输出,当我们调用secondBean.getSecondMessage()时,它的值是firstBean,它是使用idref属性设置的。

注意:元素带来价值的一个常见地方是在ProxyFactoryBean bean定义中AOP拦截器的配置中。在指定拦截器名称时使用元素可以防止拦截器id拼写错误。

票数 12
EN

Stack Overflow用户

发布于 2016-10-21 19:42:11

通过使用‘idref’标记,您可以在部署时验证引用的命名bean是否实际存在。

例如,

代码语言:javascript
运行
复制
<bean id="paulo" class="com.sample.pojo.Author">
    <property name="firstName" value="Paulo" />
    <property name="lastName" value="Coelho" />
    <property name="dateOfBirth" value="24 August 1947" />
    <property name="country" value="India" />
</bean>

如果您像下面这样定义验证器bean,Spring将在部署时验证ids为osho和Paulo的bean。如果在配置文件中没有找到任何bean,spring就会抛出BeanDefinitionStoreException。

代码语言:javascript
运行
复制
<bean id="validatorBean" class="com.sample.test.BeansValidator">
    <property name="author1">
        <idref bean="osho" />
    </property>

    <property name="author2">
        <idref bean="paulo" />
    </property>
</bean>

以下是完整的工作应用程序。

代码语言:javascript
运行
复制
package com.sample.pojo;

public class Author {
    private String firstName;
    private String lastName;
    private String dateOfBirth;
    private String country;

    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 String getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Author [firstName=").append(firstName).append(", lastName=").append(lastName)
                .append(", dateOfBirth=").append(dateOfBirth).append(", country=").append(country).append("]");
        return builder.toString();
    }

}

BeansValidator.java

代码语言:javascript
运行
复制
package com.sample.test;

    public class BeansValidator {
        private String author1;
        private String author2;

        public String getAuthor1() {
            return author1;
        }

        public void setAuthor1(String author1) {
            this.author1 = author1;
        }

        public String getAuthor2() {
            return author2;
        }

        public void setAuthor2(String author2) {
            this.author2 = author2;
        }

    }

myConfiguration.xml

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="osho" class="com.sample.pojo.Author">
        <property name="firstName" value="Osho" />
        <property name="lastName" value="Jain" />
        <property name="dateOfBirth" value="11 December 1931" />
        <property name="country" value="India" />
    </bean>

    <bean id="paulo" class="com.sample.pojo.Author">
        <property name="firstName" value="Paulo" />
        <property name="lastName" value="Coelho" />
        <property name="dateOfBirth" value="24 August 1947" />
        <property name="country" value="India" />
    </bean>

    <bean id="validatorBean" class="com.sample.test.BeansValidator">
        <property name="author1">
            <idref bean="osho" />
        </property>

        <property name="author2">
            <idref bean="paulo" />
        </property>
    </bean>

</beans>

运行HelloWorld.java,你不会得到任何异常。

代码语言:javascript
运行
复制
package com.sample.test;

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

    public class HelloWorld {
        public static void main(String args[]) {
            ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "myConfiguration.xml" });

            ((ClassPathXmlApplicationContext) context).close();
        }
    }

现在像下面这样更新myConfiguration.xml。

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="osho" class="com.sample.pojo.Author">
        <property name="firstName" value="Osho" />
        <property name="lastName" value="Jain" />
        <property name="dateOfBirth" value="11 December 1931" />
        <property name="country" value="India" />
    </bean>

    <bean id="paulo" class="com.sample.pojo.Author">
        <property name="firstName" value="Paulo" />
        <property name="lastName" value="Coelho" />
        <property name="dateOfBirth" value="24 August 1947" />
        <property name="country" value="India" />
    </bean>

    <bean id="validatorBean" class="com.sample.test.BeansValidator">
        <property name="author1">
            <idref bean="Krishna" />
        </property>

        <property name="author2">
            <idref bean="paulo" />
        </property>
    </bean>

</beans>

正如您看到的配置文件一样,validatorBean检查id为‘Krishna’的bean。

代码语言:javascript
运行
复制
<bean id="validatorBean" class="com.sample.test.BeansValidator">
        <property name="author1">
            <idref bean="Krishna" />
        </property>

        <property name="author2">
            <idref bean="paulo" />
        </property>
    </bean>

由于id为‘Krishna’的bean不存在,您将在下面的错误中结束。

代码语言:javascript
运行
复制
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'validatorBean' defined in class path resource [myConfiguration.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean name 'Krishna' in bean reference for bean property 'author1'
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    at com.sample.test.HelloWorld.main(HelloWorld.java:8)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14607142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档