【问题标题】:Spring Dependency Injection ExampleSpring 依赖注入示例
【发布时间】:2017-12-13 22:24:36
【问题描述】:

我正在研究 Spring 依赖注入。

我想使用弹簧计算矩形和圆形的面积并使用依赖注入。

到目前为止,我已经完成了这个:

界面:

public interface Shape {
   double calculateArea();
}

圈子类

public class Circle implements Shape{

    @Autowired
    public double radius;

    @Override
    public double calculateArea() {
        double area = (Math.PI)*radius*radius;
        return area;
    }

}

矩形类:

public class Rectangle implements Shape {

    @Autowired
    public double length;

    @Autowired
    public double breadth;

    @Override
    public double calculateArea() {
        double area = length*breadth;
        return area;
    }
}

bean xml:

<bean id="circle" class="org.package.test.Circle">
    <property name="radius" value="12"/>
</bean>
<bean id="rectangle" class="org.package.test.Rectangle">
    <property name="length" value="12"/>
    <property name="breadth" value="10"/>
</bean>

<bean id="geometricalShape" class="org.package.test.GeometricalShape">
    <constructor-arg ref="circle"/>
    <constructor-arg ref="rectangle"/>
</bean>

GeometricalShape.java

private Shape shape;

private Shape shape1;
   /**
    * Inject circle object via Constructor
    */
   public GeometricalShape(Shape shape,Shape shape1) {
      this.shape = shape;
      this.shape1 = shape1;
   }

   public void calculate() {
      shape.calculateArea();
      shape1.calculateArea();
   }
}

Main.Java

public class Main {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");

        GeometricalShape bean1=context.getBean("geometricalShape", GeometricalShape.class);
        geometryBean1.calculate();           
    }
}

这里我直接提供半径/长度或宽度作为静态值。我如何将它作为动态值提供(这是我想要通过在 Spring 中更改它的任何值?)有没有更好的方法使用 Spring 依赖注入来做到这一点?请问有什么建议吗?

【问题讨论】:

  • 您通常不会使用 Spring 注入可变(变化的、动态的)数据。您更有可能注入数据提供者(它们不会更改,但提供动态数据)或数据处理器。 Spring 的连接发生在创建上下文时。
  • 当然你可以使用FactoryBeans来动态提供值在注入的时候,但是这个要谨慎使用。

标签: java spring spring-mvc dependency-injection


【解决方案1】:

您正在尝试将标量值自动连接到您的 CircleRectangle 类中。这行不通。正确的解决方案是使用 Spring 的 @Value 注释,该注释将由 BeanPostProcessor 执行并为您注入。

<bean
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:config.properties</value>
        </list>
    </property>
    <property name="properties">
        <value>
            circle.radius=12
            rectangle.length=12
            rectangle.breadth=10
        </value>
    </property>
</bean>

然后你可以做的是:

public class Circle implements Shape{

    @Value("#{circle.radius}")
    public double radius;

    @Override
    public double calculateArea() {
        double area = (Math.PI)*radius*radius;
        return area;
    }

}

甚至在你的 bean 定义中你也可以这样定义它

<bean id="circle" class="org.package.test.Circle">
    <property name="radius" value="#{circle.radius}"/>
</bean>

您可以在我的示例中看到 PropertyPlaceholderConfigurer 也具有 locations 属性,因此您可以将此值移动到文件中。 Spring 也可以从环境变量或系统属性中读取它。我会推荐阅读Spring's reference documentation.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 2012-02-26
    • 2010-10-19
    • 1970-01-01
    • 2011-12-05
    • 2018-11-21
    • 2012-12-23
    相关资源
    最近更新 更多