简体   繁体   English

Spring中的构造函数注入如何促进不变性?

[英]how constructor injection in spring promotes immutability?

I am new to spring and reading the on line spring reference. 我是Spring的新手,正在阅读在线Spring参考。 One of the advantages of using constructor injection(CI) is because CI is immutable?I know it means that it cannot be modified. 使用构造函数注入(CI)的优点之一是因为CI是不可变的?我知道这意味着它不能被修改。 But how does that happen, any simple example please to show how CI promote immutability? 但是,这是如何发生的,请通过任何简单的例子来说明CI如何促进不变性? thanks 谢谢

this is an immutable bean, thanking to CI 感谢CI,这是一个不变的bean

class B1 {
    private final int x;

    B1(int x) {
        this.x = x;
    }
}

context.xml 的context.xml

<bean id="b1" class="test.B1">
    <constructor-arg value="1"/>
</bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM