儘管向屬性autowire-candidate提供了bean,值爲false,但它們正在自動裝配。無法找到我缺少的東西。自動裝配未使用xml配置禁用
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我宣佈汽車作爲按名稱自動裝配,但兩者的相關性我已經聲明爲自動裝配候選假。所以這段代碼應該給我一個例外。但是當我運行它時,Car正在實例化。我在這裏錯過了什麼?
爲什麼它應該給出一個例外,事實上沒有任何東西可以自動佈線並不會自動導致異常。 –
要禁用autowire,可以在bean定義中放置autowire =「no」,或者在bean的bean定義中移除autowire屬性。 – Yogi
我不想刪除自動裝配。我期待,如果我已經給了一個autowire byName屬性的bean,並且我將它的所有依賴關係聲明爲autowire-candidate爲false。那麼它應該給出一個錯誤,因爲它的依賴不會被容器注入 – Manya