尽管将bean的值设置为autowire候选属性,但它们正在自动装配。无法找到我所缺少的东西。
Color.java
package org.manya.autowire;
public class Color {
private String color;
public void setColor(String color)
{
this.color = color;
}
public String getColor()
{
return this.color;
}
public String toString()
{
return this.color;
}
}
Engine.java
package org.manya.autowire;
public class Engine {
private String engine;
public void setEngine(String engine)
{
this.engine = engine;
}
public String getEngine()
{
return engine;
}
public String toString()
{
return this.engine;
}
}
Car.java
package org.manya.autowire;
public class Car {
private Color color;
private Engine engine;
public Color getColor() {
return color;
}
public void setColor(Color color) {
System.out.println("From the setter of Color in org.manya.innerBean.Car");
this.color = color;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
System.out.println("From the setter of Engine in org.manya.innerBean.Car");
this.engine = engine;
}
public String toString()
{
return "This car has, engine : " + this.engine + ", color : " + this.color;
}
}
autowire.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="car" class="org.manya.autowire.Car" autowire="byName"
/>
<bean name="color" class="org.manya.autowire.Color" autowire-candidate="false">
<property name="color">
<value>Grey</value>
</property>
</bean>
<bean id="engine" class="org.manya.autowire.Engine" autowire-candidate="false"
p:engine="v10"
/>
</beans>
在autowire.xml中,我已将汽车声称为按名称自动装配,但其两个依赖关系我已声明为 autowire-candidate为false 。所以这段代码应该给我一个例外。但是当我运行它时,汽车正在实例化。我在这里缺少什么?
答案 0 :(得分:1)
根据此Spring JIRA ticket autowire-candidate="false"
仅影响基于类型的自动装配尝试,而不影响名称的直接引用...而不是autowire =“byName”。
虽然后者可能有争议,但我不打算在此时改变它,因为
autowire="byName"
是一个过时的机制。因此,我将此转变为文档问题。