2014-03-19 43 views
0

我是新申請自動佈線兩beans.please檢查以下如何在兩個豆的春天使用自動佈線?

我的代碼Color.java

public class Color { 

private String baseColor; 

private String textureColor; 

public String getBaseColor() { 
    return baseColor; 
} 

public void setBaseColor(String baseColor) { 
    this.baseColor = baseColor; 
} 

public String getTextureColor() { 
    return textureColor; 
} 

public void setTextureColor(String textureColor) { 
    this.textureColor = textureColor; 
} 

@Override 
public String toString() { 
    return baseColor + " base skin color and " + textureColor + " texture color." ; 

} 

}

Behaviour.java

public class Behaviour{ 

private String behaviourType; 



public String getBehaviourType() { 
    return behaviourType; 
} 

public void setBehaviourType(String behaviourType) { 
    this.behaviourType = behaviourType; 
} 


} 

貓.java

public class Cat { 

private String name; 

private Color color; 
    private Behaviour behaviour; 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public Color getColor() { 
    return color; 
} 

public void setColor(Color color) { 
    this.color= color; 
} 
public Color getBehaviour() { 
    return behaviour; 
} 

public void setBehaviour(Behaviour behaviour) { 
    this.behaviour= behaviour; 
} 
@Override 
public String toString() { 
    return "The " + name + " has " + color.toString(); 

} 
} 

的applicationContext.xml

<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" 
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:task="http://www.springframework.org/schema/task" 
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> 

    <bean id="color" class="com.javacodegeeks.snippets.enterprise.Color"> 
     <property name="baseColor" value="white" /> 
     <property name="textureColor" value="grey" /> 
    </bean> 

    <bean id="cat" class="com.javacodegeeks.snippets.enterprise.Cat"> 
     <property name="name" value="cat" /> 
     <property name="color" ref="color" /> 
       <property name="behaviour" ref="behaviour" /> 
    </bean> 
      <bean id="behaviour" class="com.javacodegeeks.snippets.enterprise.Behaviour"> 
     <property name="behaviourType" value="Somebevhaviour" /> 

    </bean> 
    </beans> 

它工作正常,但我想通過使用自動佈線(綽號)用於寫入applicationContext.xml文件

回答

1

任意一個幫助申請依賴這裏是一個很好的示例站點(http://www.mkyong.com/spring/spring-autowiring-by-name/),請嘗試使用示例代碼來了解這一點。

例如。你的貓豆可以如下所示的自動連線,所以你的顏色和行爲豆將自動連線。

<bean id="cat" class="com.javacodegeeks.snippets.enterprise.Cat" autowire="byName"> 
     <property name="name" value="cat" /> 
</bean> 
1

改變你的班級,如下所示。

Cat.java

@Component 
public class Cat { 

    @Value("${cat.name}") 
    private String name; 

    @Autowired 
    private Color color; 

    @Autowired 
    private Behaviour behaviour; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public Color getColor() { 
     return color; 
    } 

    public void setColor(Color color) { 
     this.color = color; 
    } 

    public Behaviour getBehaviour() { 
     return behaviour; 
    } 

    public void setBehaviour(Behaviour behaviour) { 
     this.behaviour = behaviour; 
    } 

    @Override 
    public String toString() { 
     return "The " + name + " has " + color.toString(); 

    } 
} 

Behaviour.java

@Component 
public class Behaviour { 

    @Value("${behaviour.behaviourType}") 
    private String behaviourType; 

    public String getBehaviourType() { 
     return behaviourType; 
    } 

    public void setBehaviourType(String behaviourType) { 
     this.behaviourType = behaviourType; 
    } 

} 

Color.java

@Component 
public class Color { 

    @Value("${color.basecolor}") 
    private String baseColor; 

    @Value("${color.textureColor}") 
    private String textureColor; 

    public String getBaseColor() { 
     return baseColor; 
    } 

    public void setBaseColor(String baseColor) { 
     this.baseColor = baseColor; 
    } 

    public String getTextureColor() { 
     return textureColor; 
    } 

    public void setTextureColor(String textureColor) { 
     this.textureColor = textureColor; 
    } 

    @Override 
    public String toString() { 
     return baseColor + " base skin color and " + textureColor 
       + " texture color."; 

    } 
} 

現在創建app.properti es文件具有以下屬性

cat.name=billy 
behaviour.behaviourType=sobor 
color.basecolor=white 
color.textureColor=black 

最後修改您的應用程序上下文文件,如下所示。

<context:annotation-config/> 
<context:component-scan base-package="com.kp"></context:component-scan> 

<context:property-placeholder location="classpath*:app.properties"/> 

並刪除您定義的bean。由於您有不同的班級類型,因此不需要按姓名自動填寫