Java 尝试在spring框架中执行构造函数注入时出错

Java 尝试在spring框架中执行构造函数注入时出错,java,spring,spring-mvc,Java,Spring,Spring Mvc,嗨,我是spring框架的新手,我正在试用java brains视频中的示例,我在尝试进行构造函数注入时遇到了一些问题。错误似乎与查找默认构造函数有关。错误如下所示 Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'triangle' defined in class path resource [spring.xml

嗨,我是spring框架的新手,我正在试用java brains视频中的示例,我在尝试进行构造函数注入时遇到了一些问题。错误似乎与查找默认构造函数有关。错误如下所示

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'triangle' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [test.Triangle]: No default constructor found; nested exception is java.lang.NoSuchMethodException: test.Triangle.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at test.app.main(app.java:18)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [test.Triangle]: No default constructor found; nested exception is java.lang.NoSuchMethodException: test.Triangle.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:70)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958)
... 13 more
Caused by: java.lang.NoSuchMethodException: test.Triangle.<init>()
at java.lang.Class.getConstructor0(Class.java:2721)
at java.lang.Class.getDeclaredConstructor(Class.java:2002)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:65)
... 14 more
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="triangle" class="test.Triangle">
    <constructor-arg value="Right"/>
</bean>        
三角形类

package test;


public class Triangle {
private String type;
 public Triangle(String type) {        
    this.type = type;        
}
public String getType() {
    return type;
}
 public void draw(){
    System.out.println(getType()+" Triangle Drawn of height: ");
}
}

我的例子中的问题是,我将spring.xml复制粘贴到两个位置作为试用,但忘记了它,因此在创建实例时出现了问题。因此,请检查同一文件是否位于项目文件夹中的不同位置。

有什么东西正在触发不带参数的三角形类的实例化。您确定您显示的Spring配置是项目中唯一的配置吗?
Spring.xml
是有效的xml文件吗?结尾有标签吗?谢谢Jens Schauder这就是问题所在。。。我只是将文件复制粘贴到src文件夹中
package test;


public class Triangle {
private String type;
 public Triangle(String type) {        
    this.type = type;        
}
public String getType() {
    return type;
}
 public void draw(){
    System.out.println(getType()+" Triangle Drawn of height: ");
}
}