Spring 没有解析适当的构造函数
Spring isn't resolving the appropriate constructor
感恩:感谢您的支持。
问题描述:
- 我在尝试 运行 以下代码时遇到异常。一点头绪都没有。
- 如何编写 xml 定义以使用带有 int 参数的 @Autowired 构造函数来启动 Dependent class。
异常消息:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dependent' defined in class path
resource [simple-context.xml]:
Could not resolve matching constructor (hint: specify index/type/name arguments
for simple parameters to avoid type ambiguities)
文件名:spring-context.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"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.learning.spring.constinjection" />
<bean id="dependency" class="com.learning.spring.constinjection.Dependency"></bean>
<bean id="dependent" class="com.learning.spring.constinjection.Dependent">
<constructor-arg ref="dependency" name="dpcy" />
<constructor-arg name="mesg" value="Hi this is a test mesg" />
</bean>
<bean id="dependent2" class="com.learning.spring.constinjection.Dependent"
c:mesg="Hi, this is another test mesg" />
</beans>
文件名:Dependent.java
public class Dependent {
Dependency dpcy;
Dependent(Dependency dpcy, String mesg){
this.dpcy = dpcy;
System.out.println("Message is: " + mesg);
}
Dependent(String mesg){
System.out.println("Only Message is: " + mesg);
}
@Autowired
Dependent(Integer ticketno){
System.out.println("Ticket no is: " + ticketno);
}
}
文件名:Dependency.java
public class Dependency {
Dependency(){
System.out.println("Dependency loaded successfully");
}
}
文件名:ConstructorInjectionApp
public class ConstructorInjectionApp {
public static void main(String[] args) {
GenericXmlApplicationContext gen = new GenericXmlApplicationContext();
gen.load("classpath:simplespringconstinjection.xml");
gen.refresh();
Dependent d = (Dependent) gen.getBean("dependent");
Dependent d2 = (Dependent) gen.getBean("dependent2");
}
}
通过改变这个:
<bean id="dependent" class="com.learning.spring.constinjection.Dependent">
<constructor-arg ref="dependency" name="dpcy" />
<constructor-arg name="mesg" value="Hi this is a test mesg" />
</bean>
对此:
<bean id="dependent" class="com.learning.spring.constinjection.Dependent"
c:dpcy-ref="dependency" c:mesg="Hi, this is another test mesg" />
我能够让 ApplicationContext
正确初始化,没有错误。
有关 "c" 命名空间的信息,请参阅 this Spring doc。
带有@Autowired 注释的构造函数仍然存在问题。
感恩:感谢您的支持。
问题描述:
- 我在尝试 运行 以下代码时遇到异常。一点头绪都没有。
- 如何编写 xml 定义以使用带有 int 参数的 @Autowired 构造函数来启动 Dependent class。
异常消息:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dependent' defined in class path
resource [simple-context.xml]:
Could not resolve matching constructor (hint: specify index/type/name arguments
for simple parameters to avoid type ambiguities)
文件名:spring-context.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"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.learning.spring.constinjection" />
<bean id="dependency" class="com.learning.spring.constinjection.Dependency"></bean>
<bean id="dependent" class="com.learning.spring.constinjection.Dependent">
<constructor-arg ref="dependency" name="dpcy" />
<constructor-arg name="mesg" value="Hi this is a test mesg" />
</bean>
<bean id="dependent2" class="com.learning.spring.constinjection.Dependent"
c:mesg="Hi, this is another test mesg" />
</beans>
文件名:Dependent.java
public class Dependent {
Dependency dpcy;
Dependent(Dependency dpcy, String mesg){
this.dpcy = dpcy;
System.out.println("Message is: " + mesg);
}
Dependent(String mesg){
System.out.println("Only Message is: " + mesg);
}
@Autowired
Dependent(Integer ticketno){
System.out.println("Ticket no is: " + ticketno);
}
}
文件名:Dependency.java
public class Dependency {
Dependency(){
System.out.println("Dependency loaded successfully");
}
}
文件名:ConstructorInjectionApp
public class ConstructorInjectionApp {
public static void main(String[] args) {
GenericXmlApplicationContext gen = new GenericXmlApplicationContext();
gen.load("classpath:simplespringconstinjection.xml");
gen.refresh();
Dependent d = (Dependent) gen.getBean("dependent");
Dependent d2 = (Dependent) gen.getBean("dependent2");
}
}
通过改变这个:
<bean id="dependent" class="com.learning.spring.constinjection.Dependent">
<constructor-arg ref="dependency" name="dpcy" />
<constructor-arg name="mesg" value="Hi this is a test mesg" />
</bean>
对此:
<bean id="dependent" class="com.learning.spring.constinjection.Dependent"
c:dpcy-ref="dependency" c:mesg="Hi, this is another test mesg" />
我能够让 ApplicationContext
正确初始化,没有错误。
有关 "c" 命名空间的信息,请参阅 this Spring doc。
带有@Autowired 注释的构造函数仍然存在问题。