You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring新手遇BeanDefinitionParsingException:多property定义异常,需创建Item多实例

Fixing BeanDefinitionParsingException & Creating Multiple Item Instances in Spring

Hey there! Let's work through this issue step by step. That org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Multiple 'property' definitions error almost always means you've accidentally added duplicate <property> tags for the same property within a single bean definition in your Spring XML config. Let's fix that and get your multiple Item instances up and running.

First: Understand the Root Cause

For example, if your XML had something like this (which is wrong):

<bean id="item1" class="com.yourpackage.Item">
    <property name="itemID" value="1"/>
    <property name="itemID" value="1"/> <!-- Duplicate! This triggers the error -->
    <property name="itemName" value="Laptop"/>
</bean>

Spring throws the exception because you've defined the itemID property twice for the same bean. Each property should only be set once per bean.

Solution 1: Correct XML Configuration for Multiple Instances

To create multiple Item beans, define separate <bean> entries for each instance, with unique id values, and no duplicate properties inside each bean. Here's the correct setup:

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

    <!-- First Item instance -->
    <bean id="item1" class="com.yourpackage.Item">
        <property name="itemID" value="1"/>
        <property name="itemName" value="Wireless Mouse"/>
    </bean>

    <!-- Second Item instance -->
    <bean id="item2" class="com.yourpackage.Item">
        <property name="itemID" value="2"/>
        <property name="itemName" value="Mechanical Keyboard"/>
    </bean>

    <!-- Add as many unique instances as you need -->
</beans>

Solution 2: Use Annotation-Based Configuration (Modern Approach)

If you prefer annotation-driven Spring (which is more common these days), you can create multiple instances without XML:

Step 1: Mark your Item class as a component (optional but flexible)

import org.springframework.stereotype.Component;

@Component // Makes it eligible for Spring's component scanning
public class Item {
    // Your existing code (getters, setters, toString) stays the same
}

Step 2: Create a Configuration class to define multiple instances

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ItemConfig {

    @Bean(name = "item1")
    public Item createFirstItem() {
        Item item = new Item();
        item.setItemID(1);
        item.setItemName("Bluetooth Headphones");
        return item;
    }

    @Bean(name = "item2")
    public Item createSecondItem() {
        Item item = new Item();
        item.setItemID(2);
        item.setItemName("Portable Charger");
        return item;
    }
}

Verify Your Setup

To test if it works, write a simple main class to fetch the beans:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
// Or for annotation config:
// import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        // For XML config:
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        
        // For annotation config:
        // ApplicationContext context = new AnnotationConfigApplicationContext(ItemConfig.class);

        Item item1 = (Item) context.getBean("item1");
        Item item2 = (Item) context.getBean("item2");

        System.out.println("Item 1: " + item1);
        System.out.println("Item 2: " + item2);
    }
}

This should print out your two Item instances without any exceptions.


内容的提问来源于stack exchange,提问作者Manasa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:21:40