Java 弹簧-我的自动接线有什么问题?
我有一个基本的spring应用程序,其中我的Java 弹簧-我的自动接线有什么问题?,java,spring,autowired,Java,Spring,Autowired,我有一个基本的spring应用程序,其中我的@Autowired字段的值在输出中为空。这里怎么了 package com.spring; import org.springframework.beans.factory.annotation.Autowired; public class HelloWorld { private String message; @Autowired private Double pi; public HelloWorld(Stri
@Autowired
字段的值在输出中为空。这里怎么了
package com.spring;
import org.springframework.beans.factory.annotation.Autowired;
public class HelloWorld {
private String message;
@Autowired
private Double pi;
public HelloWorld(String message){
this.message = message;
}
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
System.out.println("autowired: "+pi);
}
}
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-3.0.xsd">
<bean id="pi" name="pi" class="java.lang.Double" autowire="byName">
<constructor-arg value="3.14"/>
</bean>
<bean id="helloWorld" class="com.spring.HelloWorld">
<constructor-arg ref="msg" />
</bean>
<bean id="msg" class="java.lang.String" >
<constructor-arg value="Hello World"/>
</bean>
</beans>
输出为:
Your Message Hello World
autowired: null
Hello World
您需要配置上下文以允许自动关联。看看这个答案: 您需要在spring.xml文件中添加
,该文件将扫描java文件中的spring注释。希望这有帮助
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<bean id="pi" name="pi" class="java.lang.Double">
<constructor-arg value="3.14" />
</bean>
<bean id="helloWorld" class="com.clsa.test.HelloWorld">
<constructor-arg ref="msg" />
</bean>
<bean id="msg" class="java.lang.String">
<constructor-arg value="Hello World" />
</bean>
</beans>
尝试设置方法并检查。@KDP在这种情况下,类应该有一个适当的注释,不是吗?使用setterI不走运。我希望为Pi自动连接一个值纯粹是一个演示!否则,我肯定Spring的批评者会有一个现场日,因为在这个答案的解决方案之后,我尝试了一个注释
。因此,它回答了我的另一个问题,为什么它在我的Spring MVC应用程序中工作得如此完美。
you want to define @Quatifier
@Autowired(required=true)
@Qualifier("pi")
Double pi;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<bean id="pi" name="pi" class="java.lang.Double">
<constructor-arg value="3.14" />
</bean>
<bean id="helloWorld" class="com.clsa.test.HelloWorld">
<constructor-arg ref="msg" />
</bean>
<bean id="msg" class="java.lang.String">
<constructor-arg value="Hello World" />
</bean>
</beans>