如何在春天使用两个豆类的自动接线?

时间:2014-03-19 05:52:40

标签: spring

我是新手为两个bean应用自动接线。请检查下面的代码

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;
}


}

Cat .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>

它工作正常但我想通过使用自动连线(byName)来编写applicationContext.xml文件的任何一个帮助来应用依赖

2 个答案:

答案 0 :(得分:1)

这是一个很好的示例网站(http://www.mkyong.com/spring/spring-autowiring-by-name/),请尝试使用该示例代码来了解这一点。

例如。您的猫豆可以如下自动连线,因此您的颜色和行为bean将自动连线。

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

答案 1 :(得分: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.properties文件

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。由于您有不同的类类型,因此无需按名称自动装配