为什么即使提供了参数,spring bean 也会运行默认构造函数?

Why does spring beans runs default constructors even when parameters are provided?

我是 Spring 的新手。我创建了一个带有默认构造函数和两个参数的 class。我在 bean xml 文件中定义了它。但是当我 运行 应用程序时,它 运行 是默认构造函数而不是带参数的构造函数。我在这里找不到我做错了什么。这是代码:

ConstructorArgsApp.java

package com.kirancyrusken.springDemo;

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

public class ConstructorArgsApp {

    public static void main(String[] args) {
            //1. Create ApplicationContext
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-cp.xml");

            //2 Create the bean
        Organization org = (Organization) ctx.getBean("myorg");

            //3. invoke the company slogan the bean
        org.corporateSlogan();

        System.out.println(org);

            //4. Close the application Context (container)
        ((ClassPathXmlApplicationContext) ctx).close();
    }
}

Organization.java

package com.kirancyrusken.springDemo;

public class Organization {

    private String companyName;
    private int yearOfIncorporation;

    public Organization() {
    }

    public Organization(String companyName, int yearOfIncorporation) {
        this.companyName = companyName;
        this.yearOfIncorporation = yearOfIncorporation;
    }


    public void corporateSlogan() {
        String slogan = "We build the ultimate driving machines";
        System.out.println(slogan);
    }


    @Override
    public String toString() {
        return "Organization [companyName=" + companyName + ", yearOfIncorporation=" + yearOfIncorporation + "]";
    }

}

豆子-cp.xml

<?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="myorg" 
    class="com.kirancyrusken.springDemo.Organization">
    <constructor-arg value="BMW"></constructor-arg>
    <constructor-arg value="1929"></constructor-arg>
</bean>

使用ClassPathXmlApplicationContext时,beans.xml目录下的资源被加载。如果该文件存在于其他任何地方,则不会加载它